我正在尝试对从quandl下载的数据进行重新采样,但是即使看起来是TypeError
类型的,也总是抛出datetime64[ns]
。
我尝试运行pd.to_datetime(df['Date'])
以确保其日期时间类型没有运气。
df = pd.read_pickle('data1')
print(df.Close.head())
df = df.reset_index()
df = df[['Date', 'Close']]
print(df.Date.head())
df = df.resample('W').mean
Date
2004-08-19 100.335
2004-08-20 108.310
2004-08-23 109.400
2004-08-24 104.870
2004-08-25 106.000
Name: Close, dtype: float64
0 2004-08-19
1 2004-08-20
2 2004-08-23
3 2004-08-24
4 2004-08-25
Name: Date, dtype: datetime64[ns]
Traceback (most recent call last):
File "/Users/raymond.devries/PycharmProjects/untitled2/datatry.py", line 15, in <module>
df = df.resample('W').mean
File "/Users/raymond.devries/PycharmProjects/untitled2/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 8155, in resample
base=base, key=on, level=level)
File "/Users/raymond.devries/PycharmProjects/untitled2/venv/lib/python3.7/site-packages/pandas/core/resample.py", line 1250, in resample
return tg._get_resampler(obj, kind=kind)
File "/Users/raymond.devries/PycharmProjects/untitled2/venv/lib/python3.7/site-packages/pandas/core/resample.py", line 1380, in _get_resampler
"but got an instance of %r" % type(ax).__name__)
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
答案 0 :(得分:0)
错误清楚地表明,重新采样仅对日期索引格式有效,另一方面,您获得了范围索引,例如数据框,首先将索引从范围转换为日期类型数据,删除日期列,然后重新采样将起作用。 / p>
答案 1 :(得分:0)
我没有意识到需要将日期时间数据时间设置为索引。重采样功能读取数据帧的索引。数据帧的索引已经设置为日期时间索引,因此不需要运行df = df.reset_index()
,因此我将索引变成了范围对象。
工作代码:
df = pd.read_pickle('data1')
df = df[['Close']]
df = df.resample('W').mean()
print(df.head())
输出:
Close
Date
2004-08-22 104.3225
2004-08-29 106.8660
2004-09-05 101.2300
2004-09-12 102.8800
2004-09-19 112.4900
Process finished with exit code 0