Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: AttributeError when rolling.agg use np.prod to calculate rolling product #54203

Open
2 of 3 tasks
xythu opened this issue Jul 20, 2023 · 11 comments
Open
2 of 3 tasks
Labels
Bug Reduction Operations sum, mean, min, max, etc. Window rolling, ewma, expanding
Milestone

Comments

@xythu
Copy link

xythu commented Jul 20, 2023

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

@xythu xythu added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 20, 2023
@mroeschke mroeschke added Window rolling, ewma, expanding Reduction Operations sum, mean, min, max, etc. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 20, 2023
@mcgeestocks
Copy link
Contributor

Take

@rhshadrach
Copy link
Member

Just as in #53425, we should not be attempting to replace NumPy functions with pandas methods.

@mcgeestocks
Copy link
Contributor

Sorry for being slow here; back from Vacation

@rhshadrach
Copy link
Member

@mcgeestocks - no problem; are you still interested in working on this? I'd like to get this in for 2.2 as we'd like to enforce the deprecation in 3.0.

@mcgeestocks
Copy link
Contributor

I am but other commitments have had me tied up. What's your deadline? happy to drop so that it can get in for the timeline.

@rhshadrach
Copy link
Member

We're planning to release 2.2 in December. If this hasn't seen any action by November I plan to start working on it.

@mcgeestocks
Copy link
Contributor

Should have my life together by then! I'll un-assign if things change.

@rhshadrach
Copy link
Member

@mcgeestocks - With the 2.2 RC being cut in a few weeks, I'm going to take this up.

@rhshadrach
Copy link
Member

Actually, it appears there is nothing to do here. I get:

FutureWarning: The provided callable <function prod at 0x7fef909dc4c0> is currently using Rolling.prod. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string "prod" instead.
x.rolling(2).agg(np.prod)

While we could fix this for 2.2 directly since it raises today, it will automatically be fixed in 3.0.

@rhshadrach
Copy link
Member

I'm going to keep this issue open until we verify it's fixed in 3.0.

@rhshadrach rhshadrach added this to the 3.0 milestone Dec 2, 2023
@mcgeestocks
Copy link
Contributor

Sounds good apologies for sleeping on this @rhshadrach.

@mcgeestocks mcgeestocks removed their assignment Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reduction Operations sum, mean, min, max, etc. Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

4 participants