pandas 绘制数据帧会引发序数值的误差必须 >= 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27150972/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:42:56  来源:igfitidea点击:

Plotting dataframe raises error of ordinal value must be >= 1

pythonmatplotlibpandas

提问by David Michael Gang

I follow the tutorial at http://nbviewer.ipython.org/github/jvns/pandas-cookbook/blob/v0.1/cookbook/Chapter%205%20-%20Combining%20dataframes%20and%20scraping%20Canadian%20weather%20data.ipynb

我按照http://nbviewer.ipython.org/github/jvns/pandas-cookbook/blob/v0.1/cookbook/Chapter%205%20-%20Combining%20dataframes%20and%20scraping%20Canadian%20weather% 上的教程进行操作20data.ipynb

I have a pandas dataframe

我有一个Pandas数据框

weather_mar2012['Temp (?°C)']

weather_mar2012['Temp (?°C)']

Out[30]:

Date/Time
2012-03-01 00:00:00   -5.5
2012-03-01 01:00:00   -5.7
2012-03-01 02:00:00   -5.4

When trying to plot it i get an error

尝试绘制时出现错误

weather_mar2012['Temp (?°C)'].plot(figsize=(15, 5))



--------------------------------------------------------------------------- 
ValueError                                Traceback (most recent call last) <ipython-input-31-21c79ba7d5ef> in <module>()
----> 1 weather_mar2012['Temp (?°C)'].plot(figsize=(15, 5))

/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)    2486                  yerr=yerr, xerr=xerr,    2487                  label=label, secondary_y=secondary_y,
-> 2488                  **kwds)    2489     2490 

/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in _plot(data, x, y, subplots, ax, kind, **kwds)    2292         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)    2293 
-> 2294     plot_obj.generate()    2295     plot_obj.draw()    2296     return plot_obj.result

/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in generate(self)
    922         self._make_legend()
    923         self._post_plot_logic()
--> 924         self._adorn_subplots()
    925 
    926     def _args_adjust(self):

/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in _adorn_subplots(self)    1052                     ax.set_xticklabels(xticklabels)    1053                 self._apply_axis_properties(ax.xaxis, rot=self.rot,
-> 1054                                            fontsize=self.fontsize)    1055             elif self.orientation == 'horizontal':    1056                 if self._need_to_set_index:

/home/vagrant/anaconda3/lib/python3.4/site-packages/pandas/tools/plotting.py in _apply_axis_properties(self, axis, rot, fontsize)    1061     1062  def _apply_axis_properties(self, axis, rot=None, fontsize=None):
-> 1063         labels = axis.get_majorticklabels() + axis.get_minorticklabels()    1064         for label in labels:    1065             if rot is not None:

/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/axis.py in get_majorticklabels(self)    1155     def get_majorticklabels(self):    1156         'Return a list of Text instances for the major ticklabels'
-> 1157         ticks = self.get_major_ticks()    1158         labels1 = [tick.label1 for tick in ticks if tick.label1On]    1159         labels2 = [tick.label2 for tick in ticks if tick.label2On]

/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/axis.py in get_major_ticks(self, numticks)    1284         'get the tick instances; grow as necessary'    1285         if numticks is None:
-> 1286             numticks = len(self.get_major_locator()())    1287         if len(self.majorTicks) < numticks:    1288             # update the new tick label properties from the old

/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in __call__(self)
    863     def __call__(self):
    864         'Return the locations of the ticks'
--> 865         self.refresh()
    866         return self._locator()
    867 

/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in refresh(self)
    880     def refresh(self):
    881         'Refresh internal information based on current limits.'
--> 882         dmin, dmax = self.viewlim_to_dt()
    883         self._locator = self.get_locator(dmin, dmax)
    884 

/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
    624     def viewlim_to_dt(self):
    625         vmin, vmax = self.axis.get_view_interval()
--> 626         return num2date(vmin, self.tz), num2date(vmax, self.tz)
    627 
    628     def _get_unit(self):

/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in num2date(x, tz)
    343         tz = _get_rc_timezone()
    344     if not cbook.iterable(x):
--> 345         return _from_ordinalf(x, tz)
    346     else:
    347         x = np.asarray(x)

/home/vagrant/anaconda3/lib/python3.4/site-packages/matplotlib/dates.py in _from_ordinalf(x, tz)
    223         tz = _get_rc_timezone()
    224     ix = int(x)
--> 225     dt = datetime.datetime.fromordinal(ix)
    226     remainder = float(x) - ix
    227     hour, remainder = divmod(24 * remainder, 1)

ValueError: ordinal must be >= 1

What does it mean?

这是什么意思?

How can i fix this?

我怎样才能解决这个问题?

回答by Mat

I was getting this error in ipython even with current pandas 0.20.3

即使使用当前的 Pandas 0.20.3,我也在 ipython 中收到此错误

Traced it down to having run a script beforehand which saved a figure with a different index, but hadn't done plt.show()as the figure had been saved and I didn't need to see it.

追溯到之前运行了一个脚本,该脚本保存了一个具有不同索引的图形,但没有这样做,plt.show()因为图形已被保存,我不需要看到它。

So as @naught101 hinted, forcing plt.close('all')before showing the next figure fixes the issue. Probably good practice at the end of scripts anyway.

正如@naught101 暗示的那样,plt.close('all')在显示下一个图之前强制解决了这个问题。无论如何,在脚本结束时可能是很好的做法。

回答by citynorman

This was a bug in pandas: 0.18.1 and fixed in 0.19.2, eg run conda upgrade pandas

这是 Pandas 中的一个错误:0.18.1 并在 0.19.2 中修复,例如运行 conda upgrade pandas