Skip to content

additional keys in groupby indices when NAs are present #9304

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
josepm opened this issue Jan 19, 2015 · 2 comments · Fixed by #38861
Closed

additional keys in groupby indices when NAs are present #9304

josepm opened this issue Jan 19, 2015 · 2 comments · Fixed by #38861
Assignees
Labels
Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@josepm
Copy link

josepm commented Jan 19, 2015

In [386]: h = pd.DataFrame({'a':[1,2,1,np.nan,1], 'b':[1,2,3,3,2], 'c':[2,3,1,4,2]})

In [387]: gh=h.groupby(['a', 'b'])

In [388]: gh.groups.keys()
Out[388]: [(1.0, 2), (nan, 3), (1.0, 3), (1.0, 1), (2.0, 2)]

In [389]: gh.indices.keys()
Out[389]: [(1.0, 2), (1.0, 3), (2.0, 3), (1.0, 1), (2.0, 2)]  # Incorrect

The tuple (2.0, 3) should not be here.
The problem goes away when there are no NAs

@ledmonster
Copy link

pandas.core.groupby:_get_indices_dict seems causing the problem.

In [61]: label_list = [ping.labels for ping in gh.grouper.groupings]
In [76]: keys = [_values_from_object(ping.group_index) for ping in gh.grouper.groupings]
In [77]: keys
Out[77]: [array([ 1.,  2.]), array([1, 2, 3])]
In [78]: label_list
Out[78]: [array([ 0,  1,  0, -1,  0]), array([0, 1, 2, 2, 1])]
In [79]: _get_indices_dict(label_list, keys)
Out[79]:
{(1.0, 2): array([4]),
 (1.0, 3): array([2]),
 (2.0, 3): array([3]),
 (1.0, 1): array([0]),
 (2.0, 2): array([1])}

@jreback
Copy link
Contributor

jreback commented Jan 25, 2015

groupbying with a nan group (or in a level) is not really supported. In general these cause weird types of issues. I'll mark it as a bug though.

@jreback jreback added Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Jan 25, 2015
@jreback jreback added this to the 0.17.0 milestone Jan 25, 2015
@rhshadrach rhshadrach self-assigned this Dec 31, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.3 Dec 31, 2020
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

Successfully merging a pull request may close this issue.

6 participants