获取“TypeError:float()参数必须是字符串或数字”与pandas plot()

时间:2017-04-01 13:10:10

标签: python python-2.7 pandas matplotlib

我要通过简单的熊猫教程。我试图绘制由dtype ='datetime64 [ns]'索引的DataFrame,但是,当我尝试绘制时,我假设matplotlib尝试将日期转换为float,这会引发异常。

>>> df.index
DatetimeIndex(['2012-01-01', '2012-01-02', '2012-01-03', '2012-01-04',
           '2012-01-05', '2012-01-06', '2012-01-07', '2012-01-08',
           '2012-01-09', '2012-01-10',
           ...
           '2012-12-22', '2012-12-23', '2012-12-24', '2012-12-25',
           '2012-12-26', '2012-12-27', '2012-12-28', '2012-12-29',
           '2012-12-30', '2012-12-31'],
          dtype='datetime64[ns]', name=u'Date', length=366, freq=None)

>>> df.plot()
Traceback (most recent call last):
  ...
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 676, in recache
    x = np.asarray(xconv, np.float_)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number

我做错了什么?

注意:我在这里遵循非常简单的教程:http://nbviewer.jupyter.org/github/jvns/pandas-cookbook/blob/v0.1/cookbook/Chapter%201%20-%20Reading%20from%20a%20CSV.ipynb

1 个答案:

答案 0 :(得分:1)

本教程正在绘制colmn 2中的数字,而不是第1列中的日期:

日期

2012-01-01 35

2012-01-02 83

2012-01-03 135

...

因此你的情节遗漏了一些东西,再次检查你的输入......