Skip to content

BUG: groupby.sum with timedelta64 and NaT #44658

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

Merged
merged 5 commits into from
Nov 29, 2021
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Fixed regressions
- Fixed regression in creating a :class:`DataFrame` from a timezone-aware :class:`Timestamp` scalar near a Daylight Savings Time transition (:issue:`42505`)
- Fixed performance regression in :func:`read_csv` (:issue:`44106`)
- Fixed regression in :meth:`Series.duplicated` and :meth:`Series.drop_duplicates` when Series has :class:`Categorical` dtype with boolean categories (:issue:`44351`)
- Fixed regression in :meth:`.GroupBy.sum` with ``timedelta64[ns]`` dtype containing ``NaT`` failing to treat that value as NA (:issue:`42659`)
-

.. ---------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/groupby.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def group_add(
values: np.ndarray, # ndarray[complexfloating_t, ndim=2]
labels: np.ndarray, # const intp_t[:]
min_count: int = ...,
datetimelike: bool = ...,
) -> None: ...
def group_prod(
out: np.ndarray, # floating[:, ::1]
Expand Down
12 changes: 10 additions & 2 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ def group_add(add_t[:, ::1] out,
int64_t[::1] counts,
ndarray[add_t, ndim=2] values,
const intp_t[::1] labels,
Py_ssize_t min_count=0) -> None:
Py_ssize_t min_count=0,
bint datetimelike=False) -> None:
"""
Only aggregates on axis=0 using Kahan summation
"""
Expand Down Expand Up @@ -557,7 +558,14 @@ def group_add(add_t[:, ::1] out,
val = values[i, j]

# not nan
if val == val:
# With dt64/td64 values, values have been cast to float64
# instead if int64 for group_add, but the logic
# is otherwise the same as in _treat_as_na
if val == val and not (
add_t is float64_t
and datetimelike
and val == <float64_t>NPY_NAT
):
nobs[lab, j] += 1
y = val - compensation[lab, j]
t = sumx[lab, j] + y
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,16 @@ def _call_cython_op(
result_mask=result_mask,
is_datetimelike=is_datetimelike,
)
elif self.how in ["add"]:
# We support datetimelike
func(
result,
counts,
values,
comp_ids,
min_count,
datetimelike=is_datetimelike,
)
else:
func(result, counts, values, comp_ids, min_count)
else:
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,3 +1162,27 @@ def test_mean_on_timedelta():
pd.to_timedelta([4, 5]), name="time", index=Index(["A", "B"], name="cat")
)
tm.assert_series_equal(result, expected)


def test_groupby_sum_timedelta_with_nat():
# GH#42659
df = DataFrame(
{
"a": [1, 1, 2, 2],
"b": [pd.Timedelta("1d"), pd.Timedelta("2d"), pd.Timedelta("3d"), pd.NaT],
}
)
td3 = pd.Timedelta(days=3)

gb = df.groupby("a")

res = gb.sum()
expected = DataFrame({"b": [td3, td3]}, index=Index([1, 2], name="a"))
tm.assert_frame_equal(res, expected)

res = gb["b"].sum()
tm.assert_series_equal(res, expected["b"])

res = gb["b"].sum(min_count=2)
expected = Series([td3, pd.NaT], dtype="m8[ns]", name="b", index=expected.index)
tm.assert_series_equal(res, expected)