Skip to content

Commit 1c5cfec

Browse files
phoflKevin D Smith
authored and
Kevin D Smith
committed
BUG: Fix bug in quantile() for resample and groupby with Timedelta (pandas-dev#37145)
1 parent fa18cda commit 1c5cfec

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ Groupby/resample/rolling
516516
- Bug in :meth:`DataFrameGroupBy.rolling` returned wrong values with timeaware window containing ``NaN``. Raises ``ValueError`` because windows are not monotonic now (:issue:`34617`)
517517
- Bug in :meth:`Rolling.__iter__` where a ``ValueError`` was not raised when ``min_periods`` was larger than ``window`` (:issue:`37156`)
518518
- Using :meth:`Rolling.var()` instead of :meth:`Rolling.std()` avoids numerical issues for :meth:`Rolling.corr()` when :meth:`Rolling.var()` is still within floating point precision while :meth:`Rolling.std()` is not (:issue:`31286`)
519+
- Bug in :meth:`df.groupby(..).quantile() <pandas.core.groupby.DataFrameGroupBy.quantile>` and :meth:`df.resample(..).quantile() <pandas.core.resample.Resampler.quantile>` raised ``TypeError`` when values were of type ``Timedelta`` (:issue:`29485`)
519520
- Bug in :meth:`Rolling.median` and :meth:`Rolling.quantile` returned wrong values for :class:`BaseIndexer` subclasses with non-monotonic starting or ending points for windows (:issue:`37153`)
520521

521522
Reshaping

pandas/core/groupby/groupby.py

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class providing the base-class of operations.
6060
is_numeric_dtype,
6161
is_object_dtype,
6262
is_scalar,
63+
is_timedelta64_dtype,
6364
)
6465
from pandas.core.dtypes.missing import isna, notna
6566

@@ -2173,6 +2174,9 @@ def pre_processor(vals: np.ndarray) -> Tuple[np.ndarray, Optional[Type]]:
21732174
elif is_datetime64_dtype(vals.dtype):
21742175
inference = "datetime64[ns]"
21752176
vals = np.asarray(vals).astype(float)
2177+
elif is_timedelta64_dtype(vals.dtype):
2178+
inference = "timedelta64[ns]"
2179+
vals = np.asarray(vals).astype(float)
21762180

21772181
return vals, inference
21782182

pandas/tests/groupby/test_quantile.py

+18
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,21 @@ def test_groupby_quantile_skips_invalid_dtype(q):
236236
result = df.groupby("a").quantile(q)
237237
expected = df.groupby("a")[["b"]].quantile(q)
238238
tm.assert_frame_equal(result, expected)
239+
240+
241+
def test_groupby_timedelta_quantile():
242+
# GH: 29485
243+
df = DataFrame(
244+
{"value": pd.to_timedelta(np.arange(4), unit="s"), "group": [1, 1, 2, 2]}
245+
)
246+
result = df.groupby("group").quantile(0.99)
247+
expected = DataFrame(
248+
{
249+
"value": [
250+
pd.Timedelta("0 days 00:00:00.990000"),
251+
pd.Timedelta("0 days 00:00:02.990000"),
252+
]
253+
},
254+
index=pd.Index([1, 2], name="group"),
255+
)
256+
tm.assert_frame_equal(result, expected)

pandas/tests/resample/test_timedelta.py

+19
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,22 @@ def test_resample_with_timedelta_yields_no_empty_groups():
165165
index=pd.timedelta_range(start="1s", periods=13, freq="3s"),
166166
)
167167
tm.assert_frame_equal(result, expected)
168+
169+
170+
def test_resample_quantile_timedelta():
171+
# GH: 29485
172+
df = DataFrame(
173+
{"value": pd.to_timedelta(np.arange(4), unit="s")},
174+
index=pd.date_range("20200101", periods=4, tz="UTC"),
175+
)
176+
result = df.resample("2D").quantile(0.99)
177+
expected = DataFrame(
178+
{
179+
"value": [
180+
pd.Timedelta("0 days 00:00:00.990000"),
181+
pd.Timedelta("0 days 00:00:02.990000"),
182+
]
183+
},
184+
index=pd.date_range("20200101", periods=2, tz="UTC", freq="2D"),
185+
)
186+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)