TypeError:float()参数必须是字符串或数字,而不是“Period”

2024-06-16 12:47:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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应该能够处理它,所以我肯定做错了什么。提前谢谢。


Tags: 数据namedfnanlistseries插值row1
3条回答

这是熊猫身上的一个虫子,如果一切顺利的话,它会被next major release by August 31, 2018修正。

现在,@J63的解决方案必须要做。或者安装较早版本的pandas,比如0.20.2。

如果我这样做,它就会工作:

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

我不知道为什么。。。

这是最简单的答案,不需要升级或降级熊猫。

pd.plotting.register_matplotlib_converters()

有时注册会导致另一个错误,如compute.use_瓶颈,使用_numexpr error来删除该调用注销程序:p

例如:pd.plotting.deregister_matplotlib_converters()

来源:Link

相关问题 更多 >