Skip to content

Commit 91abd0a

Browse files
REGR: fix regression in groupby aggregation with out-of-bounds datetimes (#38094)
1 parent 003c4a7 commit 91abd0a

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
@@ -44,7 +44,9 @@ cdef class _BaseGrouper:
4444
Slider islider, Slider vslider):
4545
if cached_typ is None:
4646
cached_ityp = self.ityp(islider.buf)
47-
cached_typ = self.typ(vslider.buf, index=cached_ityp, name=self.name)
47+
cached_typ = self.typ(
48+
vslider.buf, dtype=vslider.buf.dtype, index=cached_ityp, name=self.name
49+
)
4850
else:
4951
# See the comment in indexes/base.py about _index_data.
5052
# 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

@@ -1156,3 +1157,21 @@ def test_agg_no_suffix_index():
11561157
result = df["A"].agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
11571158
expected = Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"], name="A")
11581159
tm.assert_series_equal(result, expected)
1160+
1161+
1162+
def test_aggregate_datetime_objects():
1163+
# https://github.com/pandas-dev/pandas/issues/36003
1164+
# ensure we don't raise an error but keep object dtype for out-of-bounds
1165+
# datetimes
1166+
df = DataFrame(
1167+
{
1168+
"A": ["X", "Y"],
1169+
"B": [
1170+
datetime.datetime(2005, 1, 1, 10, 30, 23, 540000),
1171+
datetime.datetime(3005, 1, 1, 10, 30, 23, 540000),
1172+
],
1173+
}
1174+
)
1175+
result = df.groupby("A").B.max()
1176+
expected = df.set_index("A")["B"]
1177+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)