BUG: groupby.pct_change() does not work properly in Pandas 0.23.0. Grouping is ignored. · Issue #21200 · pandas-dev/pandas (original) (raw)
Code Sample
import pandas as pd import numpy as np
df = pd.DataFrame(data=np.random.rand(8, 1), columns={'a'}) df['grp']=1 df.loc[::2, 'grp']=2 df['%_groupby']=df.groupby('grp')['a'].pct_change() df['%_shift']=df.groupby('grp')['a'].shift(0)/df.groupby('grp')['a'].shift(1)-1 print(df)
Problem description
When there are different groups in a dataframe, by using groupby
it is expected that the pct_change
function be applied on each group. However, combining groupby
with pct_change
does not produce the correct result.
Output:
a grp %_groupby %_shift
0 1.0 2 NaN NaN
1 1.1 1 0.100000 NaN
2 1.2 2 0.090909 0.200000
3 1.3 1 0.083333 0.181818
4 1.4 2 0.076923 0.166667
5 1.5 1 0.071429 0.153846
6 1.6 2 0.066667 0.142857
7 1.7 1 0.062500 0.133333
Expected Output
a grp %_groupby %_shift
0 1.0 2 NaN NaN
1 1.1 1 NaN NaN
2 1.2 2 0.200000 0.200000
3 1.3 1 0.181818 0.181818
4 1.4 2 0.166667 0.166667
5 1.5 1 0.153846 0.153846
6 1.6 2 0.142857 0.142857
7 1.7 1 0.133333 0.133333
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.23.0
pytest: 3.2.1
pip: 10.0.1
setuptools: 36.5.0.post20170921
Cython: 0.26.1
numpy: 1.14.3
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.2.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None