TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'


这个问题说的很清楚,就是类型不对,需要转化类型,首先讲一下这个问题是在使用pandas的resample函数激发的,官方文档解释的较为清楚,如下:

Convenience method for frequency conversion and resampling of time series. Object must have a datetime-like index (DatetimeIndex, PeriodIndex, or TimedeltaIndex), or pass datetime-like values to the on or level keyword.

这个函数的目的主要是重新选择时间频率的,一组数据的频率既可以变大也可以变小,完全根据需求来,没有的数据就会变成NaN。问题是resample的数据必须是datetime-like index,而pandas的数据是DateFrame类型的,这个时候啊,该数据结构类似一种词典类型,给提供了index,values,columns等基本的属性。此时,只需要将数据的索引变成datetime类型的即可。如下:

y.index = pd.to_datetime(y.index)

这个时候我们看一下数据类型从DateFrame变成了Series,可以说是解决了大问题,其实之前的博客写过,Series是很方便的,但也不是说DateFrame不方便,稍稍处理一下功能更多。

另外还有一个iterrows的迭代器,也很方便,官方介绍是这样的:

DataFrame.iterrows() Iterate over DataFrame rows as (index, Series) pairs.

这个也很好啊,这样对值的访问就更加方便了,因为Series支持label索引式的访问。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 TypeError: only integer scalar arrays can be converted to a scalar index TypeError at /post/ render_to_response() got an unexpected keyword argument 'context_instance' TypeError: The 'compilation' argument must be an instance of Compilation TypeError: to_csv() got an unexpected keyword argument 'cols' TypeError: test() got multiple values for keyword argument 'key' TypeError: __init__() got an unexpected keyword argument 'serialized_options' TypeError: expected np.ndarray (got numpy.ndarray) TypeError at /index.html/ 'method' object is not subscriptable TypeError: can only concatenate str (not bytes) to str(10) Uncaught TypeError: Cannot assign to read only property 'exports' of object '#'
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM