Skip to content

Commit 9a25c3d

Browse files
phoflim-vinicius
authored and
im-vinicius
committed
ERR: Raise TypeError for groupby.var with timeldeta (pandas-dev#53045)
1 parent 5e59b3c commit 9a25c3d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ Groupby/resample/rolling
401401
the function operated on the whole index rather than each element of the index. (:issue:`51979`)
402402
- Bug in :meth:`DataFrameGroupBy.apply` causing an error to be raised when the input :class:`DataFrame` was subset as a :class:`DataFrame` after groupby (``[['a']]`` and not ``['a']``) and the given callable returned :class:`Series` that were not all indexed the same. (:issue:`52444`)
403403
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)
404-
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64 or :class:`PeriodDtype` values (:issue:`52128`)
404+
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`)
405405
-
406406

407407
Reshaping

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ def _groupby_op(
15831583
)
15841584
else:
15851585
# timedeltas we can add but not multiply
1586-
if how in ["prod", "cumprod", "skew"]:
1586+
if how in ["prod", "cumprod", "skew", "var"]:
15871587
raise TypeError(f"timedelta64 type does not support {how} operations")
15881588

15891589
# All of the functions implemented here are ordinal, so we can

pandas/tests/groupby/test_raises.py

+28
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ def df_with_datetime_col():
6666
return df
6767

6868

69+
@pytest.fixture
70+
def df_with_timedelta_col():
71+
df = DataFrame(
72+
{
73+
"a": [1, 1, 1, 1, 1, 2, 2, 2, 2],
74+
"b": [3, 3, 4, 4, 4, 4, 4, 3, 3],
75+
"c": range(9),
76+
"d": datetime.timedelta(days=1),
77+
}
78+
)
79+
return df
80+
81+
6982
@pytest.fixture
7083
def df_with_cat_col():
7184
df = DataFrame(
@@ -323,6 +336,21 @@ def test_groupby_raises_datetime_np(
323336
_call_and_check(klass, msg, how, gb, groupby_func_np, tuple())
324337

325338

339+
@pytest.mark.parametrize("func", ["prod", "cumprod", "skew", "var"])
340+
def test_groupby_raises_timedelta(func, df_with_timedelta_col):
341+
df = df_with_timedelta_col
342+
gb = df.groupby(by="a")
343+
344+
_call_and_check(
345+
TypeError,
346+
"timedelta64 type does not support .* operations",
347+
"method",
348+
gb,
349+
func,
350+
[],
351+
)
352+
353+
326354
@pytest.mark.parametrize("how", ["method", "agg", "transform"])
327355
def test_groupby_raises_category(
328356
how, by, groupby_series, groupby_func, using_copy_on_write, df_with_cat_col

0 commit comments

Comments
 (0)