Skip to content

BUG: partial nan levels missing when grouping #10484

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
jreback opened this issue Jul 1, 2015 · 2 comments
Closed

BUG: partial nan levels missing when grouping #10484

jreback opened this issue Jul 1, 2015 · 2 comments
Labels
Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@jreback
Copy link
Contributor

jreback commented Jul 1, 2015

from #10468

In [40]: df = DataFrame({'A' : [1,2,3]},index=pd.MultiIndex.from_tuples([(1,np.nan),(1,1),(2,1)],names=['first','second']))

In [41]: df
Out[41]: 
              A
first second   
1     NaN     1
      1       2
2     1       3

In [42]: df.groupby(level='second').sum()
Out[42]: 
        A
second   
1       5

In [43]: df.groupby(level=['first','second']).sum()
Out[43]: 
              A
first second   
1     1       2
2     1       3

I suppose that [43] is missing the (1,nan) group here.

@fbnrst
Copy link
Contributor

fbnrst commented Jul 2, 2015

Maybe this example makes my point from #10468 a little clearer:

In [18]: df1 = pd.DataFrame({'A' : [1,2,3]},index=pd.MultiIndex.from_tuples([(1,np.nan),(1,1),(2,1)],names=['first','second']))

In [19]: df1
Out[19]: 
              A
first second   
1     NaN     1
      1       2
2     1       3

[3 rows x 1 columns]

In [20]: df1.groupby(level=['first','second']).groups
Out[20]: {(1, 1): [(1, nan), (1, 1.0)], (2, 1): [(2, 1.0)]}

In [21]: df2 = df1.reset_index()

In [22]: df2
Out[22]: 
   first  second  A
0      1     NaN  1
1      1       1  2
2      2       1  3

[3 rows x 3 columns]

In [23]: df2.groupby(['first','second']).groups
Out[23]: {(1, nan): [0], (1, 1.0): [1], (2, 1.0): [2]}

Out[20] skips (1, nan), but Out[23] doesn't. As the documentation says NaN in the grouping key will be skipped here, I would rather argue that Out[23] should skip (1, nan). Out[43] from jreback above would actually be correct.

@rhshadrach
Copy link
Member

The np.nan group is included in the result when you specify dropna=False in groupby. Closing.

@rhshadrach rhshadrach added the Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate label Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

5 participants