Skip to content

Commit f598670

Browse files
authored
BUG: groupby().any() returns true for groups with timedelta all NaT (#59782)
1 parent e78ebd3 commit f598670

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ Plotting
652652
Groupby/resample/rolling
653653
^^^^^^^^^^^^^^^^^^^^^^^^
654654
- Bug in :meth:`.DataFrameGroupBy.__len__` and :meth:`.SeriesGroupBy.__len__` would raise when the grouping contained NA values and ``dropna=False`` (:issue:`58644`)
655+
- Bug in :meth:`.DataFrameGroupBy.any` that returned True for groups where all Timedelta values are NaT. (:issue:`59712`)
655656
- Bug in :meth:`.DataFrameGroupBy.groups` and :meth:`.SeriesGroupby.groups` that would not respect groupby argument ``dropna`` (:issue:`55919`)
656657
- Bug in :meth:`.DataFrameGroupBy.median` where nat values gave an incorrect result. (:issue:`57926`)
657658
- Bug in :meth:`.DataFrameGroupBy.quantile` when ``interpolation="nearest"`` is inconsistent with :meth:`DataFrame.quantile` (:issue:`47942`)

pandas/core/groupby/ops.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ def _call_cython_op(
371371

372372
is_datetimelike = dtype.kind in "mM"
373373

374+
if self.how in ["any", "all"]:
375+
if mask is None:
376+
mask = isna(values)
377+
374378
if is_datetimelike:
375379
values = values.view("int64")
376380
is_numeric = True
@@ -380,12 +384,10 @@ def _call_cython_op(
380384
values = values.astype(np.float32)
381385

382386
if self.how in ["any", "all"]:
383-
if mask is None:
384-
mask = isna(values)
385387
if dtype == object:
386388
if kwargs["skipna"]:
387389
# GH#37501: don't raise on pd.NA when skipna=True
388-
if mask.any():
390+
if mask is not None and mask.any():
389391
# mask on original values computed separately
390392
values = values.copy()
391393
values[mask] = True

pandas/tests/groupby/test_grouping.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,3 +1180,15 @@ def test_grouping_by_key_is_in_axis():
11801180
result = gb.sum()
11811181
expected = DataFrame({"a": [1, 2], "b": [1, 2], "c": [7, 5]})
11821182
tm.assert_frame_equal(result, expected)
1183+
1184+
1185+
def test_groupby_any_with_timedelta():
1186+
# GH#59712
1187+
df = DataFrame({"value": [pd.Timedelta(1), pd.NaT]})
1188+
1189+
result = df.groupby(np.array([0, 1], dtype=np.int64))["value"].any()
1190+
1191+
expected = Series({0: True, 1: False}, name="value", dtype=bool)
1192+
expected.index = expected.index.astype(np.int64)
1193+
1194+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)