thoroughly-confused
thoroughly-confused

Reputation: 110

Pandas rolling_mean error with Python 2.7.3

I've been looking at Wes McKinney's 'Python for Data Analysis, and have run into an issue with pandas' moving windows functions described in the back-end of chapter 10.

The problem is that the 'rolling-mean' and other 'rolling_' functions error out with integer required TypeErrors. This only appears with Python 2.7.3 and pandas 0.12.0 - changing to a different version of Python appears to fix this. The error raised appears in the pandas library.

Has anyone seen this problem or can replicate it?

Does anyone know if there is a quick fix instead of upgrading my Python version?

Code and traceback as follows:

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

Upvotes: 2

Views: 1129

Answers (1)

adw
adw

Reputation: 431

I had the exact same problem. The error occurs because there are NaNs in the Series returned by rolling_mean.

A simple workaround is to dropna:

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

Upvotes: 3

Related Questions