Skip to content

BUG: Fix groupby().any() behavior for timedelta columns with all null valuesfix issue #59712 #59750

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ def _call_cython_op(
if values.dtype == "float16":
values = values.astype(np.float32)

values = values.T
if mask is not None:
mask = mask.T
if result_mask is not None:
result_mask = result_mask.T

if self.how in ["any", "all"]:
if mask is None:
mask = isna(values)
Expand All @@ -392,12 +398,6 @@ def _call_cython_op(
values = values.astype(bool, copy=False).view(np.int8)
is_numeric = True

values = values.T
if mask is not None:
mask = mask.T
if result_mask is not None:
result_mask = result_mask.T

out_shape = self._get_output_shape(ngroups, values)
func = self._get_cython_function(self.kind, self.how, values.dtype, is_numeric)
values = self._get_cython_vals(values)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,3 +1180,15 @@ def test_grouping_by_key_is_in_axis():
result = gb.sum()
expected = DataFrame({"a": [1, 2], "b": [1, 2], "c": [7, 5]})
tm.assert_frame_equal(result, expected)

def test_groupby_any_timedelta_with_all_nulls():
# Create a DataFrame with timedelta values, including NaT (null timedelta)
df = pd.DataFrame({
'timedelta': [pd.Timedelta(days=1), pd.NaT],
'group': [0, 1]
})
result = df.groupby('group')['timedelta'].any()

# Expected behavior: group 1 should return False because it has all null values
expected = pd.Series([True, False], index=[0, 1])
pd.testing.assert_series_equal(result, expected)
Loading