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