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

BUG: Prevent pd.Series.groupby from showing FutureWarning #60894

Merged
merged 2 commits into from
Feb 12, 2025

Conversation

sanggon6107
Copy link

Linked Issue

  • Future warning observed when series.groupby is called where its name is an integer. The warning seems to stem from is_in_obj() defined in get_grouper() ; Below indexing results in the warning since gpr.name is integer type. For example :
>>>import pandas as pd
>>>arr = [['a', 'a', 'b', 'b'], ['aa', 'bb', 'aa', 'bb']]
>>>idx = pd.MultiIndex.from_arrays(arr, names=['index1', 'index2'])
>>>ser = pd.Series([10, 20, 30, 40], index=idx, name=2)
>>>ser

index1  index2
a       aa        10
        bb        20
b       aa        30
        bb        40
Name: 2, dtype: int64

>>>ser.groupby(ser)
FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`

Proposed Code Change

  • Since pd.Series doesn't have columns, added type check so that is_in_obj() is called only when obj is a DataFrame.
    for gpr, level in zip(keys, levels):
        if isinstance(obj, DataFrame) and is_in_obj(gpr):  # df.groupby(df['name'])
            in_axis = True
            exclusions.add(gpr.name)

Please let me know if there are any changes needed. Thanks!

@rhshadrach rhshadrach added Bug Groupby Warnings Warnings that appear or should be added to pandas labels Feb 10, 2025
@rhshadrach rhshadrach added this to the 2.3 milestone Feb 10, 2025
@rhshadrach rhshadrach changed the title [2.3.x] BUG: Prevent pd.Series.groupby from showing FutureWarning BUG: Prevent pd.Series.groupby from showing FutureWarning Feb 10, 2025
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Since the behavior isn't really changing here (other than to avoid a warning in 2.3.x that no longer exists on main), I think we're okay without a test.

@rhshadrach
Copy link
Member

The failures here don't look related, but I'm not seeing them fail in other PRs. @mroeschke - have you seen test_apply_raw_float_frame_lambda fail in other PRs recently?

@mroeschke mroeschke merged commit 79e203e into pandas-dev:2.3.x Feb 12, 2025
43 of 45 checks passed
@mroeschke
Copy link
Member

Thanks @sanggon6107

Hmm interesting, no I haven't seen that test fail recently. I wonder if it's only happening on the 2.3.x branch for some reason

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants