Pandas无法绘制从Excel导入的时间序列

时间:2017-12-07 18:59:17

标签: excel pandas matplotlib

这是我从Data .xls导入的DataFrame

    0   1
664     2017-12-07 19:08:54     1.1377
665     2017-12-07 19:10:31     1.1374
666     2017-12-07 19:12:17     1.1377
667     2017-12-07 19:13:28     1.1377
668     2017-12-07 19:15:25     1.1379

我认为是关键词

0    datetime64[ns]
1           float64
dtype: object

但由于某些错误,它不会与ax = _df.plot()一起绘制。知道为什么吗? 这是我在熊猫的第一个小时,对不起,如果这是经典问题,但我只看到了ValueError: ordinal must be >= 1

的相关答案。

感谢您的投入

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-ca306a836171> in <module>()
----> 1 ax = _df.plot()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/plotting/_core.py in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   2671                           fontsize=fontsize, colormap=colormap, table=table,
   2672                           yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 2673                           sort_columns=sort_columns, **kwds)
   2674     __call__.__doc__ = plot_frame.__doc__
   2675 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/plotting/_core.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   1898                  yerr=yerr, xerr=xerr,
   1899                  secondary_y=secondary_y, sort_columns=sort_columns,
-> 1900                  **kwds)
   1901 
   1902 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/plotting/_core.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   1725         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   1726 
-> 1727     plot_obj.generate()
   1728     plot_obj.draw()
   1729     return plot_obj.result

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/plotting/_core.py in generate(self)
    258 
    259         for ax in self.axes:
--> 260             self._post_plot_logic_common(ax, self.data)
    261             self._post_plot_logic(ax, self.data)
    262 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/plotting/_core.py in _post_plot_logic_common(self, ax, data)
    393             self._apply_axis_properties(ax.xaxis, rot=self.rot,
    394                                         fontsize=self.fontsize)
--> 395             self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
    396 
    397             if hasattr(ax, 'right_ax'):

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/plotting/_core.py in _apply_axis_properties(self, axis, rot, fontsize)
    466 
    467     def _apply_axis_properties(self, axis, rot=None, fontsize=None):
--> 468         labels = axis.get_majorticklabels() + axis.get_minorticklabels()
    469         for label in labels:
    470             if rot is not None:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axis.py in get_majorticklabels(self)
   1189     def get_majorticklabels(self):
   1190         'Return a list of Text instances for the major ticklabels'
-> 1191         ticks = self.get_major_ticks()
   1192         labels1 = [tick.label1 for tick in ticks if tick.label1On]
   1193         labels2 = [tick.label2 for tick in ticks if tick.label2On]

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axis.py in get_major_ticks(self, numticks)
   1318         'get the tick instances; grow as necessary'
   1319         if numticks is None:
-> 1320             numticks = len(self.get_major_locator()())
   1321         if len(self.majorTicks) < numticks:
   1322             # update the new tick label properties from the old

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in __call__(self)
    984     def __call__(self):
    985         'Return the locations of the ticks'
--> 986         self.refresh()
    987         return self._locator()
    988 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in refresh(self)
   1004     def refresh(self):
   1005         'Refresh internal information based on current limits.'
-> 1006         dmin, dmax = self.viewlim_to_dt()
   1007         self._locator = self.get_locator(dmin, dmax)
   1008 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
    761             vmin, vmax = vmax, vmin
    762 
--> 763         return num2date(vmin, self.tz), num2date(vmax, self.tz)
    764 
    765     def _get_unit(self):

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in num2date(x, tz)
    399         tz = _get_rc_timezone()
    400     if not cbook.iterable(x):
--> 401         return _from_ordinalf(x, tz)
    402     else:
    403         x = np.asarray(x)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in _from_ordinalf(x, tz)
    252 
    253     ix = int(x)
--> 254     dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
    255 
    256     remainder = float(x) - ix

ValueError: ordinal must be >= 1

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    330                 pass
    331             else:
--> 332                 return printer(obj)
    333             # Finally look for special method names
    334             method = get_real_method(obj, self.print_method)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    235 
    236     if 'png' in formats:
--> 237         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    238     if 'retina' in formats or 'png2x' in formats:
    239         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    119 
    120     bytes_io = BytesIO()
--> 121     fig.canvas.print_figure(bytes_io, **kw)
    122     data = bytes_io.getvalue()
    123     if fmt == 'svg':

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2198                     orientation=orientation,
   2199                     dryrun=True,
-> 2200                     **kwargs)
   2201                 renderer = self.figure._cachedRenderer
   2202                 bbox_inches = self.figure.get_tightbbox(renderer)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
    543 
    544     def print_png(self, filename_or_obj, *args, **kwargs):
--> 545         FigureCanvasAgg.draw(self)
    546         renderer = self.get_renderer()
    547         original_dpi = renderer.dpi

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    462 
    463         try:
--> 464             self.figure.draw(self.renderer)
    465         finally:
    466             RendererAgg.lock.release()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/figure.py in draw(self, renderer)
   1142 
   1143             mimage._draw_list_compositing_images(
-> 1144                 renderer, self, dsu, self.suppressComposite)
   1145 
   1146             renderer.close_group('figure')

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
    137     if not_composite or not has_images:
    138         for zorder, a in dsu:
--> 139             a.draw(renderer)
    140     else:
    141         # Composite any adjacent images together

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
   2424             renderer.stop_rasterizing()
   2425 
-> 2426         mimage._draw_list_compositing_images(renderer, self, dsu)
   2427 
   2428         renderer.close_group('axes')

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
    137     if not_composite or not has_images:
    138         for zorder, a in dsu:
--> 139             a.draw(renderer)
    140     else:
    141         # Composite any adjacent images together

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axis.py in draw(self, renderer, *args, **kwargs)
   1134         renderer.open_group(__name__)
   1135 
-> 1136         ticks_to_draw = self._update_ticks(renderer)
   1137         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
   1138                                                                 renderer)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axis.py in _update_ticks(self, renderer)
    967 
    968         interval = self.get_view_interval()
--> 969         tick_tups = [t for t in self.iter_ticks()]
    970         if self._smart_bounds:
    971             # handle inverted limits

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axis.py in <listcomp>(.0)
    967 
    968         interval = self.get_view_interval()
--> 969         tick_tups = [t for t in self.iter_ticks()]
    970         if self._smart_bounds:
    971             # handle inverted limits

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axis.py in iter_ticks(self)
    910         Iterate through all of the major and minor ticks.
    911         """
--> 912         majorLocs = self.major.locator()
    913         majorTicks = self.get_major_ticks(len(majorLocs))
    914         self.major.formatter.set_locs(majorLocs)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in __call__(self)
    984     def __call__(self):
    985         'Return the locations of the ticks'
--> 986         self.refresh()
    987         return self._locator()
    988 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in refresh(self)
   1004     def refresh(self):
   1005         'Refresh internal information based on current limits.'
-> 1006         dmin, dmax = self.viewlim_to_dt()
   1007         self._locator = self.get_locator(dmin, dmax)
   1008 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
    761             vmin, vmax = vmax, vmin
    762 
--> 763         return num2date(vmin, self.tz), num2date(vmax, self.tz)
    764 
    765     def _get_unit(self):

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in num2date(x, tz)
    399         tz = _get_rc_timezone()
    400     if not cbook.iterable(x):
--> 401         return _from_ordinalf(x, tz)
    402     else:
    403         x = np.asarray(x)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in _from_ordinalf(x, tz)
    252 
    253     ix = int(x)
--> 254     dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
    255 
    256     remainder = float(x) - ix

ValueError: ordinal must be >= 1

<matplotlib.figure.Figure at 0x118f1e978>

1 个答案:

答案 0 :(得分:1)

您可能想要将第1列再次列为第0列,即数字与日期相对应?这将通过

完成
df.plot(x=0,y=1)

您的列未命名。所以你也可以命名它们并将索引重置为有用的东西(或者如下所示,不那么有用; - ))

df.columns = ["0", "1"]
df = df.set_index("0")
df.plot()

两者都应该给你一个如下的情节:

enter image description here