pct_change can't work well with groupby, when fill_method =None · Issue #30463 · pandas-dev/pandas (original) (raw)
Code Sample, a copy-pastable example if possible
import pandas as pd import numpy as np data = pd.DataFrame(np.random.random((10,2)), index=['a', 'b']*5) data.iloc[1:3,:] = np.nan
for pct_change function, when the fill_method = None, it works
data.pct_change(1, fill_method=None, limit=1)
0 1
a NaN NaN
b NaN NaN
a NaN NaN
b NaN NaN
a -0.498169 -0.568501
b -0.315982 1.340587
a 1.341901 -0.489576
b 0.088594 -0.691063
a -0.514451 0.054695
b 0.844514 -0.604511
but when use it with gourpby , it raise a error
data.reset_index().groupby('index')[0].pct_change(1, fill_method=None, limit=1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-60898304743e> in <module>
----> 1 data.reset_index().groupby('index')[0].pct_change(1, fill_method=None, limit=1)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/generic.py in pct_change(self, periods, fill_method, limit, freq)
1344 )
1345 )
-> 1346 filled = getattr(self, fill_method)(limit=limit)
1347 fill_grp = filled.groupby(self.grouper.labels)
1348 shifted = fill_grp.shift(periods=periods, freq=freq)
TypeError: getattr(): attribute name must be string
else, limit = 0
worked with gourpby, but can't run in pct_change
data.reset_index().groupby('index')[0].pct_change(1, fill_method='pad', limit=0)
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 -0.656739
6 0.601904
7 1.549380
8 -0.471434
9 -0.104398
Name: 0, dtype: float64
data[0].pct_change(1, fill_method='pad', limit=0)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/missing.py in pad_2d(values, limit, mask, dtype)
546
547 if np.all(values.shape):
--> 548 algos.pad_2d_inplace(values, mask, limit=limit)
549 else:
550 # for test coverage
pandas/_libs/algos.pyx in pandas._libs.algos.pad_2d_inplace()
ValueError: Limit must be greater than 0
pandas version is 0.25.1