J63*_*J63 12 python matplotlib pandas
我有一个像这样的列的pandas数据框:
df.columns = pd.to_datetime(list(df)) #list(df) = ["2017-01", "2016-01", ...]
Run Code Online (Sandbox Code Playgroud)
然后我在数据集的每一行中执行插值,因为我有一些我想要摆脱的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
Run Code Online (Sandbox Code Playgroud)
然后我尝试绘制插值行并得到:
TypeError: float() argument must be a string or a number, not 'Period'
Run Code Online (Sandbox Code Playgroud)
整个代码:
print("ORIGINAL\n", series)
interpolation = series.interpolate(method=func, limit=10, limit_direction='both')
interpolation.plot()
print("INTERPOLATION\n",interpolation)
Run Code Online (Sandbox Code Playgroud)
在我看来,错误是在系列中的时间值,但我认为matplotlib应该很容易处理它,所以我肯定做错了.提前致谢.
Muh*_*nus 14
这是最简单的答案,无需升级或降级熊猫。
pd.plotting.register_matplotlib_converters()
Run Code Online (Sandbox Code Playgroud)
有时注册会导致另一个错误,例如compute.use_bottleneck,use_numexpr错误,该错误会消除该调用注销:P
喜欢: pd.plotting.deregister_matplotlib_converters()
来源:链接
如果我这样做,它将起作用:
plt.plot(row.index, row.values)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我不知道为什么
复制您的插值结果
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13529 次 |
| 最近记录: |