2

我一直在看 Wes McKinney 的“用于数据分析的 Python”,并且遇到了第 10 章后端描述的 pandas 移动窗口函数的问题。

问题是“rolling-mean”和其他“rolling_”函数会因整数所需的类型错误而出错。这仅在 Python 2.7.3 和 pandas 0.12.0 中出现 - 更改为不同版本的 Python 似乎可以解决此问题。引发的错误出现在 pandas 库中。

有没有人看到这个问题或可以复制它?

有谁知道是否有快速修复而不是升级我的 Python 版本?

代码和回溯如下:

Python 2.7.3 (default, Jan  2 2013, 16:53:07) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import scipy as sp
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> from pandas import Series, DataFrame
>>> 
>>> close_px_all = pd.read_csv('ch09/stock_px.csv', parse_dates=True, index_col=0)
>>> close_px = close_px_all[['AAPL', 'MSFT', 'XOM']]
>>> close_px = close_px.resample('B', fill_method='ffill')
>>> 
>>> close_px['AAPL'].plot()
<matplotlib.axes.AxesSubplot object at 0x9ddd46c>
>>> pd.rolling_mean(close_px['AAPL'], 250).plot()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 1730, in plot_series
    plot_obj.generate()
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 856, in generate
    self._make_plot()
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 1240, in _make_plot
    self._make_ts_plot(data, **self.kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 1311, in _make_ts_plot
    _plot(data, 0, ax, label, self.style, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 1295, in _plot
    style=style, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/tseries/plotting.py", line 77, in tsplot
    lines = plotf(ax, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes.py", line 4137, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes.py", line 295, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes.py", line 214, in _xy_from_xy
    by = self.axes.yaxis.update_units(y)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axis.py", line 1336, in update_units
    converter = munits.registry.get_converter(data)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/units.py", line 137, in get_converter
    xravel = x.ravel()
  File "/usr/local/lib/python2.7/dist-packages/numpy/ma/core.py", line 4025, in ravel
    r._mask = ndarray.ravel(self._mask).reshape(r.shape)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/series.py", line 981, in reshape
    return ndarray.reshape(self, newshape, order)
TypeError: an integer is required
4

1 回答 1

3

我有同样的问题。Series发生错误是因为返回的中有 NaN rolling_mean

一个简单的解决方法是dropna

>>> pd.rolling_mean(df, num).dropna().plot()

于 2013-11-27T16:09:47.470 回答