Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate percent change of a pandas dataframe across groups with a custom period?

I am trying to compute the percent change in a a column of a pandas dataframe. I can get it to work when I don’t specify periods, but I would like to specify the number of periods to consider. It has to work across groups is the problem, I can get it to work without groups, but for some reason it’s not working now?

_df_ty['velocity_7'] = _df_ty.groupby('store')['sales'].apply(pd.Series.pct_change(periods=7)).abs()

It yells at me with:

TypeError: pct_change() missing 1 required positional argument: 'self'

Which I don’t understand how that’s happening inside of apply? Google and the pandas docs aren’t helping here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Further, all the stack overflow answers I can find are just about calculating the percent change to begin with, I cannot find an example of one with grouping and a non-1 period in use.

>Solution :

Q.Which I don’t understand how that’s happening inside of apply?

Your calling a instance/bound method (pct_change) on a class Series without actually creating an instance first..hence the error. Here is the simple example to understand this..

class Foo:
    def greet(self, name):
        print('Hi', name)

Foo.greet(name='shubham') # Wrong: this will raise TypeError
Foo().greet(name='shubham') # Correct

Solution to your problem.

_df_ty['velocity_7'] = _df_ty.groupby('store')['sales']\
                             .apply(lambda s: s.pct_change(periods=7)).abs() # s is an instance of Series

Further you can simplify your solution since there is no need to use apply

_df_ty['velocity_7'] = _df_ty.groupby('store')['sales'].pct_change(periods=7).abs()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading