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: 'ExponentialMovingWindow' object has no attribute 'apply' when using 'agg' #41700

Open
gioxc88 opened this issue May 28, 2021 · 5 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Window rolling, ewma, expanding

Comments

@gioxc88
Copy link

gioxc88 commented May 28, 2021

The following code fails:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.normal(size=(100, 10)))
df.ewm(alpha=0.1).agg(lambda x: x)

Below the traceback

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-69-c018ca7d52b8> in <module>
----> 1 df.ewm(alpha=0.1).agg(lambda x: x)

c:\dev\projects\weedget\venv\lib\site-packages\pandas\core\window\ewm.py in aggregate(self, func, *args, **kwargs)
    316     )
    317     def aggregate(self, func, *args, **kwargs):
--> 318         return super().aggregate(func, *args, **kwargs)
    319 
    320     agg = aggregate

c:\dev\projects\weedget\venv\lib\site-packages\pandas\core\window\rolling.py in aggregate(self, func, *args, **kwargs)
    472         result, how = aggregate(self, func, *args, **kwargs)
    473         if result is None:
--> 474             return self.apply(func, raw=False, args=args, kwargs=kwargs)
    475         return result
    476 

c:\dev\projects\weedget\venv\lib\site-packages\pandas\core\window\rolling.py in __getattr__(self, attr)
    206             return self[attr]
    207 
--> 208         raise AttributeError(
    209             f"'{type(self).__name__}' object has no attribute '{attr}'"
    210         )

AttributeError: 'ExponentialMovingWindow' object has no attribute 'apply'
@gioxc88 gioxc88 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 28, 2021
@mroeschke
Copy link
Member

Thanks for the report. I think conceptually it's not well defined what a custom exponentially weighted udf would return. In the meanwhile, I think it would be better if ExponentialMovingWindow.apply would raise a NotImplementedError

@mroeschke mroeschke added Enhancement Error Reporting Incorrect or improved errors from pandas Window rolling, ewma, expanding and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 28, 2021
@gioxc88
Copy link
Author

gioxc88 commented May 28, 2021

Thanks for the report. I think conceptually it's not well defined what a custom exponentially weighted udf would return. In the meanwhile, I think it would be better if ExponentialMovingWindow.apply would raise a NotImplementedError

Thanks for looking into it.
I don't want to oversimplify but in my opinion is relatively straightforward.
If I use .apply(fn) on ewm I would expect fn to receive the weighted series according to the exponential weights.

@hudson-ai
Copy link

@gioxc88 what is a weighted series though? It seems that fn would have to take both the series and the weights as arguments. Think about how you would have to implement the standard deviation, for example. You cannot simply multiply the values times the weights.

@gioxc88
Copy link
Author

gioxc88 commented Jun 17, 2021

@gioxc88 what is a weighted series though? It seems that fn would have to take both the series and the weights as arguments. Think about how you would have to implement the standard deviation, for example. You cannot simply multiply the values times the weights.

@hudson-ai thanks for the answer. You are indeed correct. The function passed to apply should receive both the series and the weights.

@KIC
Copy link

KIC commented Dec 1, 2022

@gioxc88 what is a weighted series though? It seems that fn would have to take both the series and the weights as arguments. Think about how you would have to implement the standard deviation, for example. You cannot simply multiply the values times the weights.

that's exactly what I would expect. alternatively the current row along with the last result and the alpha factor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

4 participants