TypeError:float()参数必须是字符串或数字,而不是'Period' [英] TypeError: float() argument must be a string or a number, not 'Period'
问题描述
我有一个带有以下列的pandas数据框:
I have a pandas dataframe with columns like this:
df.columns = pd.to_datetime(list(df)) #list(df) = ["2017-01", "2016-01", ...]
然后,我对数据集的每一行进行了插值,因为我想摆脱一些NaN.这是打印的结果:
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'
整个代码:
print("ORIGINAL\n", series)
interpolation = series.interpolate(method=func, limit=10, limit_direction='both')
interpolation.plot()
print("INTERPOLATION\n",interpolation)
在我看来,该错误在于该系列中的时间值,但我认为matplotlib应该可以处理它,因此,我肯定做错了.预先感谢.
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.
推荐答案
这是最简单的答案,无需升级或降级熊猫.
Here is the simplest answer,No need to upgrade or downgrade the pandas.
pd.plotting.register_matplotlib_converters()
有时注册会导致另一个错误,例如 compute.use_bottleneck,use_numexpr错误 ,该错误会导致调用取消注册:P
sometime registering causing another error which is like compute.use_bottleneck, use_numexpr error for getting rid of that call deregister :P
赞:pd.plotting.deregister_matplotlib_converters()
来源: 查看全文