Argument TypeError: float () must be a string or a number, not a "Period"

I have a pandas framework with columns like this:

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

Then I interpolated in each row of the dataset because I have some NaNs that I want to get rid of. 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 build an interpolated string and get:

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

All 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 there is an error in the time values ​​in the series, but I think that matplotlib should be smoothed to deal with this, so I am doing something wrong for sure. Thanks in advance.

+8
python matplotlib pandas
source share
5 answers

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

Right now the workaround is @ J63. This or install an earlier version of Pandas, for example 0.20.2.

+4
source share

It works if I do:

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

I don’t know why, though ...

+3
source share

Copied 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

+1
source share
0
source share

Here is the simplest answer, there is no need to update or lower pandas.

 pd.plotting.register_matplotlib_converters() 

source: link

0
source share

All Articles