我有一个包含如下列的 pandas 数据框:
df.columns = pd.to_datetime(list(df)) #list(df) = ["2017-01", "2016-01", ...]
然后我在数据集的每一行中执行了一个插值,因为我有一些我想摆脱的 NaN。这是打印的结果:
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
然后我尝试绘制插值行并得到:
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 应该能够处理它,所以我肯定做错了什么。提前致谢。
最佳答案
这里是最简单的答案,无需升级或降级pandas。
pd.plotting.register_matplotlib_converters()
有时注册会导致另一个错误,例如 compute.use_bottleneck, use_numexpr error 以摆脱该调用注销 :P
喜欢:pd.plotting.deregister_matplotlib_converters()
来源:Link
关于python - TypeError : float() argument must be a string or a number, 不是 'Period',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43206554/