Python 为什么带句点的重采样会转换为熊猫中的DatetimeIndex?
考虑这个例子:Python 为什么带句点的重采样会转换为熊猫中的DatetimeIndex?,python,pandas,Python,Pandas,考虑这个例子: import pandas as pd import numpy as np frame = pd.DataFrame(np.random.randn(24, 4), index=pd.period_range('1-2000', '12-2001', freq='M'), columns=['Colorado', 'Texas', 'New York', 'Ohio']) frame.resample('Q',
import pandas as pd
import numpy as np
frame = pd.DataFrame(np.random.randn(24, 4),
index=pd.period_range('1-2000', '12-2001', freq='M'),
columns=['Colorado', 'Texas', 'New York', 'Ohio'])
frame.resample('Q', how='mean').index
这会产生一个周期索引
PeriodIndex(['2000Q1', '2000Q2', '2000Q3', '2000Q4', '2001Q1', '2001Q2',
'2001Q3', '2001Q4'],
dtype='int64', freq='Q-DEC')
但是,当我想使用基本频率的倍数进行重采样时
frame.resample('2Q', how='mean').index
我得到一个DatetimeIndex
:
DatetimeIndex(['2000-03-31', '2000-09-30', '2001-03-31', '2001-09-30',
'2002-03-31'],
dtype='datetime64[ns]', freq='2Q-DEC')
在这种情况下,为什么使用句点重新采样会转换为熊猫中的DatetimeIndex?这种行为是意料之中的吗?我做错什么了吗?
这很不方便,因为那样我就得写信了
df = frame.resample('2Q', how='mean')
df.index = df.index.to_period('2Q')
df.index
得到
PeriodIndex(['2000Q1', '2000Q3', '2001Q1', '2001Q3', '2002Q1'],
dtype='int64', freq='2Q-DEC')
如果使用
kind='period'
参数,则会得到警告:频率倍数->时间戳
,因此这是当前的预期行为。查看resample.py,有一条评论说,#不能有多个句点,转换为时间戳
。我不清楚为什么。