Skip to content

Combination of groupby with dropna set to True and apply function not returning expected output #43205

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

Closed
deponovo opened this issue Aug 25, 2021 · 1 comment · Fixed by #43236
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@deponovo
Copy link
Contributor

deponovo commented Aug 25, 2021

(Using pandas 1.3.1 and numpy 1.20.3)

This is a repetition of the SO question found here. (I did not have this issue previously when working with version 1.1.3).

In the following snippet:

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "a": [1, 2, 3, 4, 5, 6, 7, 8, 9], 
        "b": [1, np.nan, 1, np.nan, 2, 1, 2, np.nan, 1]
    }
)
df_again = df.groupby("b", dropna=False).apply(lambda x: x)

I was expecting df and df_again to be identical. But they are not:

df
   a    b
0  1  1.0
1  2  NaN
2  3  1.0
3  4  NaN
4  5  2.0
5  6  1.0
6  7  2.0
7  8  NaN
8  9  1.0

df_again
   a    b
0  1  1.0
2  3  1.0
4  5  2.0
5  6  1.0
6  7  2.0
8  9  1.0

Now, if I tweak slightly the lambda expression slightly to "see" what is going on by df.groupby("b", dropna=False).apply(lambda x: print(x)) I can actually visualize that also the portion of the df where b was NaN was processed.

@deponovo deponovo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 25, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Aug 25, 2021
@simonjayhawkins
Copy link
Member

Thanks @deponovo for the report

first bad commit: [03e5858] BUG: groupby.apply on the NaN group drops values with original axes return (#38257) cc @GYHHAHA

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 25, 2021
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Aug 25, 2021
@simonjayhawkins simonjayhawkins added Apply Apply, Aggregate, Transform, Map Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Aug 25, 2021
@jreback jreback modified the milestones: Contributions Welcome, 1.3.3 Sep 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants