我一直在关注Wes McKinney的'Python for Data Analysis,并且遇到了第10章后端描述的大熊猫移动窗口函数的问题。
问题是'rolling-mean'和其他'rolling_'函数错误输出整数所需的TypeErrors。这只出现在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
答案 0 :(得分:3)
我遇到了完全相同的问题。发生此错误的原因是Series
返回的rolling_mean
中存在NaN。
一个简单的解决方法是dropna
:
>>> pd.rolling_mean(df, num).dropna().plot()