Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
import numpy as np
x = pd.Series([1., 2., 3.])
# AttributeError: 'Rolling' object has no attribute 'prod'
x.rolling(2).agg(np.prod)
Issue Description
Try to use np.prod
in rolling.agg
to calcualte rolling product, but get error message:
AttributeError Traceback (most recent call last)
Cell In[6], line 5
2 import numpy as np
4 x = pd.Series([1., 2., 3.])
----> 5 x.rolling(2).agg(np.prod)
File pandas/core/window/rolling.py:1849, in Rolling.aggregate(self, func, *args, **kwargs)
1811 @doc(
1812 _shared_docs["aggregate"],
1813 see_also=dedent(
(...)
1847 )
1848 def aggregate(self, func, *args, **kwargs):
-> 1849 return super().aggregate(func, *args, **kwargs)
File pandas/core/window/rolling.py:661, in BaseWindow.aggregate(self, func, *args, **kwargs)
660 def aggregate(self, func, *args, **kwargs):
--> 661 result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
662 if result is None:
663 return self.apply(func, raw=False, args=args, kwargs=kwargs)
File pandas/core/apply.py:171, in Apply.agg(self)
169 f = com.get_cython_func(arg)
170 if f and not args and not kwargs:
--> 171 return getattr(obj, f)()
173 # caller can react
174 return None
File pandas/core/window/rolling.py:318, in BaseWindow.__getattr__(self, attr)
315 if attr in self.obj:
316 return self[attr]
--> 318 raise AttributeError(
319 f"'{type(self).__name__}' object has no attribute '{attr}'"
320 )
AttributeError: 'Rolling' object has no attribute 'prod'
Expected Behavior
use a function to wrap np.prod
is fine.
import pandas as pd
import numpy as np
def prod(x):
return np.prod(x)
x = pd.Series([1., 2., 3.])
x.rolling(2).agg(prod)
"""
0 NaN
1 2.0
2 6.0
dtype: float64
"""
Installed Versions
INSTALLED VERSIONS
commit : 0f43794
python : 3.11.0.final.0
python-bits : 64
OS : Darwin
OS-release : 22.5.0
Version : Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : zh_CN.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.3
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.8.0
pip : 23.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : 1.0.3
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader: 0.10.0
bs4 : 4.12.2
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : 2023.1.0
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 12.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
snappy : None
sqlalchemy : 1.4.41
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None