首先,我使用 Python '2.7.3 | 64位, pandas 0.12.0, numpy 1.8.0。我正在关注Pandas时间序列的this教程,但是当我接触到这个时:
filled.ix['2011-11-03':'2011-11-04'].between_time('09:30', '16:00').plot()
plt.figsize(10,8)
plt.ylim(103.5, 104.5)
我收到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-151-78e7fd271d16> in <module>()
----> 1 filled.ix['2011-11-03':'2011-11-04'].between_time('09:30', '16:00').plot()
2 plt.figsize(10,8)
3 plt.ylim(103.5, 104.5)
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/tools/plotting.pyc in plot_frame(frame, x, y, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, style, title, xlim, ylim, logx, logy, xticks, yticks, kind, sort_columns, fontsize, secondary_y, **kwds)
1634 logy=logy, sort_columns=sort_columns,
1635 secondary_y=secondary_y, **kwds)
-> 1636 plot_obj.generate()
1637 plot_obj.draw()
1638 if subplots:
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/tools/plotting.pyc in generate(self)
854 self._compute_plot_data()
855 self._setup_subplots()
--> 856 self._make_plot()
857 self._post_plot_logic()
858 self._adorn_subplots()
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _make_plot(self)
1238 if not self.x_compat and self.use_index and self._use_dynamic_x():
1239 data = self._maybe_convert_index(self.data)
-> 1240 self._make_ts_plot(data, **self.kwds)
1241 else:
1242 lines = []
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _make_ts_plot(self, data, **kwargs)
1319 self._maybe_add_color(colors, kwds, style, i)
1320
-> 1321 _plot(data[col], i, ax, label, style, **kwds)
1322
1323 self._make_legend(lines, labels)
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _plot(data, col_num, ax, label, style, **kwds)
1293 def _plot(data, col_num, ax, label, style, **kwds):
1294 newlines = tsplot(data, plotf, ax=ax, label=label,
-> 1295 style=style, **kwds)
1296 ax.grid(self.grid)
1297 lines.append(newlines[0])
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/tseries/plotting.py in tsplot(series, plotf, **kwargs)
75 args.append(style)
76
---> 77 lines = plotf(ax, *args, **kwargs)
78 label = kwargs.get('label', None)
79
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axes.pyc in plot(self, *args, **kwargs)
4135 lines = []
4136
-> 4137 for line in self._get_lines(*args, **kwargs):
4138 self.add_line(line)
4139 lines.append(line)
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axes.pyc in _grab_next_args(self, *args, **kwargs)
315 return
316 if len(remaining) <= 3:
--> 317 for seg in self._plot_args(remaining, kwargs):
318 yield seg
319 return
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axes.pyc in _plot_args(self, tup, kwargs)
293 x = np.arange(y.shape[0], dtype=float)
294
--> 295 x, y = self._xy_from_xy(x, y)
296
297 if self.command == 'plot':
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axes.pyc in _xy_from_xy(self, x, y)
212 if self.axes.xaxis is not None and self.axes.yaxis is not None:
213 bx = self.axes.xaxis.update_units(x)
--> 214 by = self.axes.yaxis.update_units(y)
215
216 if self.command != 'plot':
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axis.pyc in update_units(self, data)
1334 """
1335
-> 1336 converter = munits.registry.get_converter(data)
1337 if converter is None:
1338 return False
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/units.pyc in get_converter(self, x)
135
136 if isinstance(x, np.ndarray) and x.size:
--> 137 xravel = x.ravel()
138 try:
139 # pass the first value of x that is not masked back to
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/ma/core.pyc in ravel(self)
4023 r._update_from(self)
4024 if self._mask is not nomask:
-> 4025 r._mask = ndarray.ravel(self._mask).reshape(r.shape)
4026 else:
4027 r._mask = nomask
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/series.pyc in reshape(self, newshape, order)
979 return self.values.reshape(newshape, order=order)
980 else:
--> 981 return ndarray.reshape(self, newshape, order)
982
983 def get(self, label, default=None):
TypeError: an integer is required
<matplotlib.figure.Figure at 0x5008050>
可能降级到较低的 matplotlib 或 numpy 版本是解决方案吗?我不知道如何解决这个问题。
答案 0 :(得分:0)
杰夫知道这个问题的根源,并在评论中说这是一个兼容性问题。直到Anaconda更新以解决问题之前我的解决方法是在我的绘图函数调用中使用dropna()
。我不知道它为什么会起作用但到目前为止它已经在所有情况下完成了!由于您可能不太关心NaN
,因此它的成本非常低,您可能不需要在问题解决时重写代码。例如
filled.ix['2011-11-03':'2011-11-04'].between_time('09:30', '16:00').dropna().plot()
plt.figsize(10,8)
plt.ylim(103.5, 104.5)