Skip to content
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

GroupBy nth ignores observed keyword for Categorical #26385

Closed
WillAyd opened this issue May 14, 2019 · 1 comment · Fixed by #26419
Closed

GroupBy nth ignores observed keyword for Categorical #26385

WillAyd opened this issue May 14, 2019 · 1 comment · Fixed by #26419
Labels
Categorical Categorical Data Type Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@WillAyd
Copy link
Member

WillAyd commented May 14, 2019

Was looking to consolidate nth, first and last in the GroupBy world but was hitting some issues with categorical tests and handling of missing data. Note that the following calls are not equivalent, though they should be:

>>> import numpy as np
>>> import pandas as pd

>>> s1 = pd.Categorical([np.nan, 'a', np.nan, 'a'], categories=['a', 'b', 'c'])
>>> s2 = pd.Series([1, 2, 3, 4])
>>> df = pd.DataFrame({'s1': s1, 's2': s2})
>>> grp = df.groupby('s1', observed=False)
>>> grp.first()
     s2
s1
a   2.0
b   NaN
c   NaN

>>> grp.nth(0)
    s2
s1
a    2
@WillAyd WillAyd added Categorical Categorical Data Type Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels May 14, 2019
@WillAyd WillAyd added this to the Contributions Welcome milestone May 14, 2019
@shantanu-gontia
Copy link
Contributor

I get the output -

>>> grp.nth(0)
     s2
s1    
a    2
c    1

@jreback jreback modified the milestones: Contributions Welcome, 0.25.1 Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type 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.

3 participants