Skip to content

Inconsistent results for groupby on multiple columns with NaN value #17445

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
linar-jether opened this issue Sep 6, 2017 · 2 comments
Closed
Labels
Docs Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@linar-jether
Copy link

When grouping a DataFrame on multiple columns, and one of the columns contains a NaN value, the row will be excluded from the result, this is already documented here: #3729 and has an open issue.

But the .groups attribute includes these NaN rows, which is inconsistent with the groupby results.

See this simple example:

import pandas as pd
import numpy as np
df = pd.DataFrame({'a': ['1', '2', '3'], 'b': ['4', np.NaN, '6'], 'c': [7, 8, 9]})

df.groupby(['a', 'b']).groups
Out[75]: 
{('1', '4'): Int64Index([0], dtype='int64'),
 ('2', nan): Int64Index([1], dtype='int64'), ##### Is excluded when iterating the results
 ('3', '6'): Int64Index([2], dtype='int64')}


{k: g.index for k, g in df.groupby(['a', 'b'])}
Out[76]: 
{('1', '4'): Int64Index([0], dtype='int64'),
 ('3', '6'): Int64Index([2], dtype='int64')}
@jreback
Copy link
Contributor

jreback commented Sep 6, 2017

I suppose this could be better documented in the doc-string of .groups. This is generally not a useful user exposed item. What are you trying to do?

@mroeschke mroeschke added the Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate label Jun 12, 2021
@rhshadrach
Copy link
Member

Closing as a duplicate of #55919

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

4 participants