Python, Pandas ; ValueError('window must be an integer',)
This is an error from Pandas. You are passing a string to df.rolling
, but it expects only integer values. You probably want to pass int(new)
instead.
Edit: as noted below, evidently the Pandas documentation is incomplete, and the real ultimate problem in this case is probably the lack of a time index, since creating a naive Dataframe and passing values like "10d"
definitely raises the indicated error:
In [2]: df = pd.DataFrame({'B': [0, 1, 2, 10, 4]})
In [3]: df.rolling('10d')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-2a9875316cd7> in <module>
----> 1 df.rolling('10d')
~/anaconda/lib/python3.7/site-packages/pandas/core/generic.py in rolling(self, window, min_periods, center, win_type, on, axis, closed)
8906 min_periods=min_periods,
8907 center=center, win_type=win_type,
-> 8908 on=on, axis=axis, closed=closed)
8909
8910 cls.rolling = rolling
~/anaconda/lib/python3.7/site-packages/pandas/core/window.py in rolling(obj, win_type, **kwds)
2467 return Window(obj, win_type=win_type, **kwds)
2468
-> 2469 return Rolling(obj, **kwds)
2470
2471
~/anaconda/lib/python3.7/site-packages/pandas/core/window.py in __init__(self, obj, window, min_periods, center, win_type, axis, on, closed, **kwargs)
78 self.win_freq = None
79 self.axis = obj._get_axis_number(axis) if axis is not None else None
---> 80 self.validate()
81
82 @property
~/anaconda/lib/python3.7/site-packages/pandas/core/window.py in validate(self)
1476
1477 elif not is_integer(self.window):
-> 1478 raise ValueError("window must be an integer")
1479 elif self.window < 0:
1480 raise ValueError("window must be non-negative")
ValueError: window must be an integer
As of today, the documentation states as follows:
window : int, or offset
Size of the moving window. This is the number of observations used for calculating the statistic. Each window will be a fixed size.
If its an offset then this will be the time period of each window. Each window will be a variable sized based on the observations included in the time-period. This is only valid for datetimelike indexes. This is new in 0.19.0
It is not clear from me whether the time information is a column in your dataframe or part of a MultiIndex. For the first case, you can use .set_index('time')
.
For MultiIndex, currently, you cannot use offsets. See the related issue. If that works, you can use .reset_index()
to transform it into a single index dataframe (see here).
Update: you can also pass datetime columns for offset-based rolling metrics with the on
parameter (and, therefore, you do not have to have an index).