JavaEar 专注于收集分享传播有价值的技术资料

TypeError:float()参数必须是字符串或数字,而不是'Period' (TypeError: float() argument must be a string or a number, not 'Period')

I have a pandas dataframe with columns like this:

df.columns = pd.to_datetime(list(df)) #list(df) = ["2017-01", "2016-01", ...]

Then I performed an interpolation in each row of the dataset because I have some NaNs that I want to get rid off. Here is the result printed:

ORIGINAL  
2007-12-01     NaN 
2008-12-01     NaN 
2009-12-01     NaN 
2010-12-01   -0.35 
2011-12-01    0.67 
2012-12-01     NaN 
2013-12-01     NaN 
2014-12-01    1.03 
2015-12-01    0.37 
2016-12-01     NaN 
2017-12-01     NaN 
Name: row1, dtype: float64 

INTERPOLATION  
2007-12-01   -0.350000 
2008-12-01   -0.350000 
2009-12-01   -0.350000 
2010-12-01   -0.350000 
2011-12-01    0.670000 
2012-12-01    0.790219 
2013-12-01    0.910109 
2014-12-01    1.030000 
2015-12-01    0.370000 
2016-12-01    0.370000 
2017-12-01    0.370000 
Name: row1, dtype: float64

Then I try to plot the interpolated row and get:

TypeError: float() argument must be a string or a number, not 'Period' 

The whole code:

print("ORIGINAL\n", series)
interpolation = series.interpolate(method=func, limit=10, limit_direction='both')
interpolation.plot()
print("INTERPOLATION\n",interpolation)

It seems to me that the error is in the time values in the series, but I think matplotlib should be hable to handle it, so I'm doing something wrong for sure. Thanks in advance.

3个回答

    最佳答案

  1. 英文原文

    This is a bug in Pandas, and will be fixed by the next major release by August 31, 2018 if everything goes swimmingly.

    For now, @J63's workaround have to do. That, or install an earlier version of pandas, such as 0.20.2.


    中文翻译

    这是Pandas中的一个错误,将由下一个主要版本修复2018年8月31日如果一切顺利的话。

    目前,@ J63的解决方法必须要做。那,或者安装早期版本的pandas,例如0.20.2。

    This is a bug in Pandas, and will be fixed by the next major release by August 31, 2018 if everything goes swimmingly.

    For now, @J63's workaround have to do. That, or install an earlier version of pandas, such as 0.20.2.

    这是Pandas中的一个错误,将由下一个主要版本修复2018年8月31日如果一切顺利的话。

    目前,@ J63的解决方法必须要做。那,或者安装早期版本的pandas,例如0.20.2。

  2. 参考答案2
  3. It works if I do:

    plt.plot(row.index, row.values)
    plt.show()
    

    I don't know why though...

  4. 参考答案3
  5. Copied your Interpolation results

    df = pd.read_clipboard(header=None)
    df.columns = ['Period','Value']
    df['Period'] = pd.to_datetime(df['Period'])
    df  = df.set_index('Period')
    print(df)
    
                   Value
    Period              
    2007-12-01 -0.350000
    2008-12-01 -0.350000
    2009-12-01 -0.350000
    2010-12-01 -0.350000
    2011-12-01  0.670000
    2012-12-01  0.790219
    2013-12-01  0.910109
    2014-12-01  1.030000
    2015-12-01  0.370000
    2016-12-01  0.370000
    2017-12-01  0.370000
    
    
    df.plot()
    

    enter image description here