python - TypeError : float() argument must be a string or a number, 不是 'Period'

标签 python pandas matplotlib

我有一个包含如下列的 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/

相关文章:

python - Linux 上 Python 中的随机数生成器

python - 减去索引 - TypeError : cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index' >

python-3.x - Matplotlib 通过循环为 y 刻度标签着色

python - matplotlib 保存每 n 个步骤的动画

python-3.x - Matplotlib:使用跟踪保存的动画

python - Tensorflow feed_dict 值错误 : setting an array element with a sequence

python - Conda 在哪里保存与 Conda 环境相关的编译库?

python - 如何正确测试模式中的有效字符,可能使用字符数组或其他一些方法(正则表达式)

python - Pandas Dataframe - 根据变量添加新行

python - 从不同大小的 numpy 数组创建 pandas 数据框