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: pd.Series.groupby issues FutureWarning #60831

Open
2 of 3 tasks
TiborVoelcker opened this issue Feb 2, 2025 · 4 comments
Open
2 of 3 tasks

BUG: pd.Series.groupby issues FutureWarning #60831

TiborVoelcker opened this issue Feb 2, 2025 · 4 comments
Assignees
Labels
Bug Groupby Warnings Warnings that appear or should be added to pandas
Milestone

Comments

@TiborVoelcker
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# Some index that are not integers
index = pd.date_range(start='2000-01-01', periods=3, freq='YS')

# Integer as a name
data = pd.Series([1, 2, 3], index=index, name=2)

data.groupby(data) # 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]`

Issue Description

The warning comes from this line:

return gpr is obj[gpr.name]

Here, gpr.name is 2, which results in the warning. If the index consists of integers, this means obj[gpr.name] will actually return the line (which then fails the comparison).

I have not checked on the main branch, but the same line might be present here:

obj_gpr_column = obj[gpr.name]

Expected Behavior

Don't issue FutureWarning

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 186 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United Kingdom.1252

pandas : 2.2.3
numpy : 2.2.2
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 25.0
Cython : None
sphinx : None
IPython : 8.32.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.10.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2025.1
qtpy : None
pyqt5 : None

@TiborVoelcker TiborVoelcker added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 2, 2025
@rhshadrach
Copy link
Member

Thanks for the report. It appears to me we should only be doing this check when obj is a DataFrame. PRs to fix are welcome. I think this can go in 2.3.

@rhshadrach rhshadrach added Groupby Warnings Warnings that appear or should be added to pandas and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 4, 2025
@rhshadrach rhshadrach added this to the 2.3 milestone Feb 4, 2025
@sanggon6107
Copy link

take

@sanggon6107
Copy link

Hi @rhshadrach,
I've found that Series.groupby doesn't show the warning on the main branch(70edaa0) since Series.__getitem__ currently always treats the integer key as a label as discussed at #50617.
Do we still need to do the check only when obj is a DataFrame? I personally think it might make sense though since Series doesn't have columns.

@rhshadrach
Copy link
Member

I've found that Series.groupby doesn't show the warning on the main branch

This is because that deprecation has been enforced on the main branch for the 3.0 release. However we will be releasing 2.3 prior to this, and the warning still shows on the 2.3.x branch. This is where the fix should go.

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

No branches or pull requests

3 participants