Python 类型错误:float() 参数必须是字符串或数字,而不是“Period”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43206554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: float() argument must be a string or a number, not 'Period'
提问by J63
I have a pandas dataframe with columns like this:
我有一个带有如下列的熊猫数据框:
df.columns = pd.to_datetime(list(df)) #list(df) = ["2017-01", "2016-01", ...]
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:
然后我在数据集的每一行中执行了一个插值,因为我有一些想要摆脱的 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
Then I try to plot the interpolated row and get:
然后我尝试绘制插值行并得到:
TypeError: float() argument must be a string or a number, not 'Period'
The whole code:
整个代码:
print("ORIGINAL\n", series)
interpolation = series.interpolate(method=func, limit=10, limit_direction='both')
interpolation.plot()
print("INTERPOLATION\n",interpolation)
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.
在我看来,错误在于系列中的时间值,但我认为 matplotlib 应该能够处理它,所以我肯定做错了什么。提前致谢。
回答by Muhammad Younus
Here is the simplest answer,No need to upgrade or downgrade the pandas.
这是最简单的答案,无需升级或降级熊猫。
pd.plotting.register_matplotlib_converters()
sometime registering causing another error which is like compute.use_bottleneck, use_numexpr errorfor getting rid of that call deregister :P
有时注册会导致另一个错误,例如compute.use_bottleneck, use_numexpr 错误,用于摆脱该调用取消注册:P
Like: pd.plotting.deregister_matplotlib_converters()
喜欢: pd.plotting.deregister_matplotlib_converters()
source:Link
来源:链接
回答by J63
It works if I do:
如果我这样做,它会起作用:
plt.plot(row.index, row.values)
plt.show()
I don't know why though...
虽然我不知道为什么...
回答by Xavier Ho
This is a bug in Pandas, and will be fixed by the next major release by August 31, 2018if everything goes swimmingly.
这是 Pandas 中的一个错误,如果一切顺利,将在 2018 年 8 月 31 日之前的下一个主要版本中修复。
For now, @J63's workaround have to do. That, or install an earlier version of pandas, such as 0.20.2.
目前,@J63 的解决方法必须做。那,或者安装一个早期版本的pandas,比如0.20.2。
回答by Scott Boston
Copied your Interpolation results
复制您的插值结果
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()