resample函数报错及解决办法
来源:2-6 使用resample函数转化时间序列

weixin_慕慕6090427
2022-01-11
自问自答一下,给大家提供一些参考
resample函数报错:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of ‘Index’
原因:虽然已经指定date列为index,把这个index却是字符串来的,要把字符串的index转为时间类型的,才能resample
解决办法:
加一行代码
data = data.set_index(pd.DatetimeIndex(pd.to_datetime(data.index)))
写回答
2回答
-
好赞好赞,谢谢分享
012022-01-12 -
GnosMgod
2022-07-10
resample报错,大概率是因为调用get_price函数时,没有使用filed=['cols']明确字段名,返回的索引是数字,导致自己又用time字段排序导致的,只要用
df = get_price('002677.XSHE', end_date='2022-07-08',count=20, frequency='daily', fields=['open','close','high','low','volume','money'])
替换为这段代码,索引就是时间了。
00
相似问题