Skip to content

ENH: No numeric_only argument for pandas.core.groupby.GroupBy.rank() #44438

Open
@notBillJames

Description

@notBillJames

pandas.core.groupby.GroupBy.rank does not have a numeric_only argument like DataFrame.rank()

I have a DataFrame with several statistics from baseball teams across different years. I want to rank each team in each statistic, grouped by season. Every column is numeric, except for the teamID column which is an object type containing the names of each team as a string. My code looks something like this

stats = pd.read_csv('stats.csv')
ranked = stats.groupby('yearID').rank(method='average', ascending=False)
ranked['teamID'] = stats['teamID']

Since it is GroupBy.rank() I can't pass the numeric_only argument and that means I have to reassign ranked['teamID'] to the original column. I also cannot do

ranked = stats.groupby(['yearID', 'teamID']).rank(...)

because that would give everybody a rank of 1.

Is there a reason that numeric_only is included in DataFrame.rank() but not GroupBy.rank(). Could it be added to GroupBy.rank()?

Then I could code it like this, which would be easier.

```python
stats = pd.read_csv('stats.csv')
ranked = stats.groupby('yearID').rank(method='average', ascending=False, numeric_only=False)

I am just a hobbyist and so I don't know much about the implementation of these methods which means there may be something I am completely ignoring, or another more efficient way to do it. If so I would appreciate some enlightenment about what I am missing. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions