有效年份值的“ ValueError:年份超出范围”

时间:2019-06-15 07:57:54

标签: python pandas matplotlib

我正在使用一个包含日期,温度和湿度数据的csv文件,然后使用pandas和matplotlib pyplot加载它以创建数据图。

对于某些日期时间值,我得到带有有效值的错误“ ValueError:年份超出范围”,没有明显的规律。

这是针对Raspberry Pi(通过apt-get完全更新),Python 2.7.13,pandas 0.19.2和matplotlib 2.0.0。我已经通过Google搜索查看了无数页面,但还没有任何工作。

请注意,我使用的是日/月/年格式(我在新西兰)。

好的行和坏的行之间没有明显的区别,即它们看起来都具有有效的日期和时间值以及相同的行尾(CR LF)。

显示问题的代码:

    #!/usr/bin/env python2

    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates

    filename = 'tempfile.csv'

    df = pd.read_csv(filename, names=['DateTime','Air Temperature','Humidity','CPU Temperature'], header = 0)

    df['DateTime'] =  pd.to_datetime(df['DateTime'], format='%d/%m/%Y %H:%M', utc=None)

    df.plot(kind='line',x='DateTime',y='Air Temperature')
    ax = plt.gca()

    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

    plt.savefig('graph.png')
    plt.close()

通常,这会导致生成图形,但是某些值会导致错误。

示例数据文件:

16/06/2019 02:15,13.7,90.6,30.0
16/06/2019 02:30,13.7,92.2,30.0
16/06/2019 02:45,13.6,92.2,30.0
16/06/2019 03:00,13.4,92.0,30.0
16/06/2019 03:15,13.5,91.9,28.9
16/06/2019 03:30,13.5,91.9,28.9

导致错误的行:

16/06/2019 03:15,13.5,91.9,28.9

“修复”是通过将分钟值减小1来修改时间分量,因此将问题行更改为以下结果可以避免错误。

16/06/2019 03:14,13.5,91.9,28.9

另一个“解决方法”是只保存csv数据的前三行或后三行。

另一个“修复程序”正在注释掉以下行,这虽然使x轴格式丢失,但也会导致代码正常工作。

ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

这有效的事实使我认为错误消息错误地指出了一年的问题。

完整的错误消息是:

    Traceback (most recent call last):
      File "./max.py", line 18, in <module> plt.savefig('graph.png')
      File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 697, in savefig res = fig.savefig(*args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1572, in savefig self.canvas.print_figure(*args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2244, in print_figure **kwargs)
      File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 545, in print_png FigureCanvasAgg.draw(self)
      File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw self.figure.draw(self.renderer)
      File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1143, in draw renderer, self, dsu, self.suppressComposite)
      File "/usr/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images a.draw(renderer)
      File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2409, in draw mimage._draw_list_compositing_images(renderer, self, dsu)
      File "/usr/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images a.draw(renderer)
      File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/matplotlib/axis.py", line 1136, in draw ticks_to_draw = self._update_ticks(renderer)
      File "/usr/lib/python2.7/dist-packages/matplotlib/axis.py", line 969, in _update_ticks tick_tups = [t for t in self.iter_ticks()]
      File "/usr/lib/python2.7/dist-packages/matplotlib/axis.py", line 916, in iter_ticks for i, val in enumerate(majorLocs)]
      File "/usr/lib/python2.7/dist-packages/matplotlib/dates.py", line 466, in __call__ dt = num2date(x, self.tz)
      File "/usr/lib/python2.7/dist-packages/matplotlib/dates.py", line 401, in num2date return _from_ordinalf(x, tz)
      File "/usr/lib/python2.7/dist-packages/matplotlib/dates.py", line 254, in _from_ordinalf dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
    ValueError: year is out of range

已更新为仅显示最小测试用例。

统计函数是用于计算均值和标准差的函数,由于不影响结果,因此已从上述代码中删除。

0 个答案:

没有答案