Skip to content

BUG: pandas 2.2.0 DataFrame.groupby regression when group labels of type pd.Timestamp #57192

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

Open
2 of 3 tasks
maread99 opened this issue Feb 1, 2024 · 3 comments
Open
2 of 3 tasks
Labels
Bug Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version Timezones Timezone data dtype
Milestone

Comments

@maread99
Copy link

maread99 commented Feb 1, 2024

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
assert pd.__version__ == "2.2.0"

index = pd.date_range("2024-02-01", "2024-02-03", freq="8h", tz="UTC")

# index
# DatetimeIndex(['2024-02-01 00:00:00+00:00', '2024-02-01 08:00:00+00:00',
#                '2024-02-01 16:00:00+00:00', '2024-02-02 00:00:00+00:00',
#                '2024-02-02 08:00:00+00:00', '2024-02-02 16:00:00+00:00',
#                '2024-02-03 00:00:00+00:00'],
#               dtype='datetime64[ns, UTC]', freq='8h')

df = pd.DataFrame([1] * len(index), index)

def f(ts: pd.Timestamp) -> pd.Timestamp:
    """Group by date, i.e. as timezone naive timestamp."""
    return ts.normalize().tz_convert(None)

grouped = df.groupby(by=f)
keys = list(grouped.groups.keys())
print(keys)

# [Timestamp('2024-02-01 00:00:00+0000', tz='UTC'),
#  Timestamp('2024-02-02 00:00:00+0000', tz='UTC'),
#  Timestamp('2024-02-03 00:00:00+0000', tz='UTC')]

Issue Description

Although the return from the 'by' function is timezone naive, the group labels are timezone aware (UTC).

(I've tried every combination of the as_index and group_keys options, always get the same group keys).

Expected Behavior

Would have expected behaviour as pandas 2.1.4 and prior, i.e,. group keys as returned from the 'by' function...

import pandas as pd
assert pd.__version__ == "2.1.4"

index = pd.date_range("2024-02-01", "2024-02-03", freq="8h", tz="UTC")
df = pd.DataFrame([1] * len(index), index)

def f(ts: pd.Timestamp) -> pd.Timestamp:
    return ts.normalize().tz_convert(None)

grouped = df.groupby(by=f)
keys = list(grouped.groups.keys())
print(keys)
[Timestamp('2024-02-01 00:00:00'),
 Timestamp('2024-02-02 00:00:00'),
 Timestamp('2024-02-03 00:00:00')]

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 141 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.4
dateutil : 2.8.2
setuptools : 58.1.0
pip : 23.3.2
Cython : None
pytest : 8.0.0
hypothesis : 6.97.3
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.4
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.14.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.9.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : 3.9.2
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

@maread99 maread99 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 1, 2024
@rhshadrach
Copy link
Member

Thanks for the report. Result of a git bisect:

commit 746e5eee860b6e143c33c9b985e095dac2e42677
Author: jbrockmendel
Date:   Mon Oct 16 11:50:28 2023 -0700

    ENH: EA._from_scalars (#53089)

cc @jbrockmendel

@rhshadrach rhshadrach added Groupby Regression Functionality that used to work in a prior pandas version Timezones Timezone data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 3, 2024
@rhshadrach rhshadrach added this to the 2.2.1 milestone Feb 3, 2024
@jbrockmendel
Copy link
Member

yah i think thats a problem in DTA._from_scalars. _from_scalars itself was a mistake xref #56430

@rhshadrach rhshadrach added Index Related to the Index class or subclasses and removed Groupby labels Feb 17, 2024
@rhshadrach
Copy link
Member

This can be reproduced just using index.map, so taking off the groupby label.

@lithomas1 lithomas1 modified the milestones: 2.2.1, 2.2.2 Feb 23, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.2, 2.2.3 Apr 10, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.3, 2.3 Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants