Skip to content

ENH: add indexing syntax to GroupBy.nth() #44589

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
johnzangwill opened this issue Nov 23, 2021 · 1 comment · Fixed by #44688
Closed

ENH: add indexing syntax to GroupBy.nth() #44589

johnzangwill opened this issue Nov 23, 2021 · 1 comment · Fixed by #44688
Assignees
Milestone

Comments

@johnzangwill
Copy link
Contributor

johnzangwill commented Nov 23, 2021

Proposed Enhancement

#42947 added slice function arguments to groupby.nth(), but not Python slice index expressions.
I would like to be able to use Python slice notation as an alternative to the current function call.
e.g.

  • groupby.nth[b:e:s] to be the same as groupby.nth(slice(b, e, s)).
  • groupby.nth[a, b, c, ...] to be the same as groupby.nth([a, b, c, ...]).

API breaking implications

None. This is just syntactic sugar using existing functionality, and the existing API will not be changed.

Current functionality

df = df = pd.DataFrame({'A': [1, 1, 2, 1, 2],  'B': [1, 2, 3, 4, 5]}, columns=['A', 'B'])
g = df.groupby("A")
g.nth(slice(None, -1))
 	 	B
 	A 	
 	1 	1
 	1 	2
 	2 	3

Current behavior

g.nth[:-1]
 	---------------------------------------------------------------------------
 	TypeError                                 Traceback (most recent call last)
 	~\AppData\Local\Temp/ipykernel_18684/1996983145.py in <module>
 	----> 1 g.nth[0:-1]

 	TypeError: 'method' object is not subscriptable

Desired behavior

g.nth[:-1]
 	 	B
 	A 	
 	1 	1
 	1 	2
 	2 	3
@johnzangwill johnzangwill added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 23, 2021
@johnzangwill
Copy link
Contributor Author

take

@jreback jreback added this to the 1.4 milestone Dec 3, 2021
@jreback jreback added Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants