Skip to content

Commit a627c70

Browse files
Backport PR #38094: REGR: fix regression in groupby aggregation with out-of-bounds datetimes (#38123)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent c53df5f commit a627c70

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/source/whatsnew/v1.1.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in inplace operations on :class:`Series` with ``ExtensionDtype`` with NumPy dtyped operand (:issue:`37910`)
2121
- Fixed regression in metadata propagation for ``groupby`` iterator (:issue:`37343`)
2222
- Fixed regression in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`)
23+
- Fixed regression in :meth:`DataFrame.groupby` aggregation with out-of-bounds datetime objects in an object-dtype column (:issue:`36003`)
2324
- Fixed regression in ``df.groupby(..).rolling(..)`` with the resulting :class:`MultiIndex` when grouping by a label that is in the index (:issue:`37641`)
2425
- Fixed regression in :meth:`DataFrame.fillna` not filling ``NaN`` after other operations such as :meth:`DataFrame.pivot` (:issue:`36495`).
2526

pandas/_libs/reduction.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ cdef class _BaseGrouper:
4646
Slider islider, Slider vslider):
4747
if cached_typ is None:
4848
cached_ityp = self.ityp(islider.buf)
49-
cached_typ = self.typ(vslider.buf, index=cached_ityp, name=self.name)
49+
cached_typ = self.typ(
50+
vslider.buf, dtype=vslider.buf.dtype, index=cached_ityp, name=self.name
51+
)
5052
else:
5153
# See the comment in indexes/base.py about _index_data.
5254
# We need this for EA-backed indexes that have a reference

pandas/tests/groupby/aggregate/test_aggregate.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
test .agg behavior / note that .apply is tested generally in test_groupby.py
33
"""
4+
import datetime
45
import functools
56
from functools import partial
67

@@ -1089,3 +1090,21 @@ def test_agg_no_suffix_index():
10891090
result = df["A"].agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
10901091
expected = pd.Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"], name="A")
10911092
tm.assert_series_equal(result, expected)
1093+
1094+
1095+
def test_aggregate_datetime_objects():
1096+
# https://github.com/pandas-dev/pandas/issues/36003
1097+
# ensure we don't raise an error but keep object dtype for out-of-bounds
1098+
# datetimes
1099+
df = DataFrame(
1100+
{
1101+
"A": ["X", "Y"],
1102+
"B": [
1103+
datetime.datetime(2005, 1, 1, 10, 30, 23, 540000),
1104+
datetime.datetime(3005, 1, 1, 10, 30, 23, 540000),
1105+
],
1106+
}
1107+
)
1108+
result = df.groupby("A").B.max()
1109+
expected = df.set_index("A")["B"]
1110+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)