-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: groupby apply inconsistent output #34478
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
Comments
Not sure, if it is the exact same issue, but we see a similar inconsistency when doing conditional filtering. A minimal reproducible example: df = pd.DataFrame(
index=["a", "b", "c", "d"],
data=[(0, 1.), (0, 2.), (1, 3.), (1, 4.)],
columns=["group_col", "value"]
) Doing an apply with a value that actually triggers filtering ( df.groupby("group_col").apply(
lambda df: df[["value"]] if all(df["value"] < 3) else df.iloc[1:][["value"]]
)
>>> value
group_col
0 a 1.0
b 2.0
1 d 4.0 Doing an apply with a value that does not trigger filtering ( df.groupby("group_col").apply(
lambda df: df[["value"]] if all(df["value"] < 5) else df.iloc[1:][["value"]]
)
>>> value
a 1.0
b 2.0
c 3.0
d 4.0 Expected output: I would expect the latter to output: value
group_col
0 a 1.0
b 2.0
1 c 3.0
d 4.0 Output of pd.show_versions()INSTALLED VERSIONS ------------------ commit : None python : 3.8.2.final.0 python-bits : 64 OS : Darwin OS-release : 19.4.0 machine : x86_64 processor : i386 byteorder : little LC_ALL : en_US.UTF-8 LANG : en_US.UTF-8 LOCALE : en_US.UTF-8pandas : 1.0.3 |
@jendrikjoe Might be related. Not sure. Example: df2.groupby('group_col').value_col.apply(lambda x: x.value_counts().reindex(index=[1,2,3]))
0 NaN
1 NaN
2 3.0
3 NaN
Name: value_col, dtype: float64 df2.head(3).groupby('group_col').value_col.apply(lambda x: x.value_counts().reindex(index=[1,2,3]))
group_col
0.0 1 NaN
2 2.0
3 NaN
Name: value_col, dtype: float64 |
I played around with a little bit more with df1 and df2. If I changed the index list in the reindex argument, I get (the right multilevel answer): df2.groupby('group_col').value_col.apply(lambda x: x.value_counts().reindex(index=[1,2]))
group_col
0.0 1 NaN
2 3.0
Name: value_col, dtype: float64 As another example, I shortened the data and try with different index list. Here are the few outputs: df3 = pd.DataFrame({'group_col': [np.nan, 0.0,0.0], 'value_col': [2,2,2]})
df3.groupby('group_col').value_col.apply(lambda x: x.value_counts().reindex(index=[1,2]))
0 NaN
1 NaN
2 2.0
Name: value_col, dtype: float64 and df4 = pd.DataFrame({'group_col': [np.nan, 0.0], 'value_col': [2,2]})
df4.groupby('group_col').value_col.apply(lambda x: x.value_counts().reindex(index=[1]))
0 NaN
1 NaN
Name: value_col, dtype: float64 I suspect there is something about (re)-indexing and missing value. I would love to work on this, but I am new to this repo. If someone else could point me towards the right direction, I'd be happy to investigate further. Output of DetailsINSTALLED VERSIONScommit : None pandas : 1.0.4 |
I now get the expected output in the OP. |
take |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
groupby apply on
df1
gives multi-index outputgroupby apply on
df2
gives a single level index output:Problem description
groupby apply on
df1
anddf2
produce different outputs. They should produce the same output.Expected Output
The output of both the above mentioned examples should be this:
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.3
numpy : 1.18.3
pytz : 2019.3
dateutil : 2.8.1
pip : 20.1.1
setuptools : 46.1.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None
The text was updated successfully, but these errors were encountered: