Skip to content

Commit eec6080

Browse files
authored
fix bug when combining groupby with resample and interpolate with dat… (#35360)
1 parent 71dfed7 commit eec6080

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ Groupby/resample/rolling
249249

250250
- Bug in :meth:`DataFrameGroupBy.count` and :meth:`SeriesGroupBy.sum` returning ``NaN`` for missing categories when grouped on multiple ``Categoricals``. Now returning ``0`` (:issue:`35028`)
251251
- Bug in :meth:`DataFrameGroupBy.apply` that would some times throw an erroneous ``ValueError`` if the grouping axis had duplicate entries (:issue:`16646`)
252-
-
253-
-
252+
- Bug when combining methods :meth:`DataFrame.groupby` with :meth:`DataFrame.resample` and :meth:`DataFrame.interpolate` raising an ``TypeError`` (:issue:`35325`)
254253
- Bug in :meth:`DataFrameGroupBy.apply` where a non-nuisance grouping column would be dropped from the output columns if another groupby method was called before ``.apply()`` (:issue:`34656`)
255254

256255
Reshaping

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ def interpolate(
795795
"""
796796
Interpolate values according to different methods.
797797
"""
798-
result = self._upsample(None)
798+
result = self._upsample("asfreq")
799799
return result.interpolate(
800800
method=method,
801801
axis=axis,

pandas/tests/resample/test_time_grouper.py

+62
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,65 @@ def test_upsample_sum(method, method_args, expected_values):
287287
result = methodcaller(method, **method_args)(resampled)
288288
expected = pd.Series(expected_values, index=index)
289289
tm.assert_series_equal(result, expected)
290+
291+
292+
def test_groupby_resample_interpolate():
293+
# GH 35325
294+
d = {"price": [10, 11, 9], "volume": [50, 60, 50]}
295+
296+
df = pd.DataFrame(d)
297+
298+
df["week_starting"] = pd.date_range("01/01/2018", periods=3, freq="W")
299+
300+
result = (
301+
df.set_index("week_starting")
302+
.groupby("volume")
303+
.resample("1D")
304+
.interpolate(method="linear")
305+
)
306+
expected_ind = pd.MultiIndex.from_tuples(
307+
[
308+
(50, "2018-01-07"),
309+
(50, pd.Timestamp("2018-01-08")),
310+
(50, pd.Timestamp("2018-01-09")),
311+
(50, pd.Timestamp("2018-01-10")),
312+
(50, pd.Timestamp("2018-01-11")),
313+
(50, pd.Timestamp("2018-01-12")),
314+
(50, pd.Timestamp("2018-01-13")),
315+
(50, pd.Timestamp("2018-01-14")),
316+
(50, pd.Timestamp("2018-01-15")),
317+
(50, pd.Timestamp("2018-01-16")),
318+
(50, pd.Timestamp("2018-01-17")),
319+
(50, pd.Timestamp("2018-01-18")),
320+
(50, pd.Timestamp("2018-01-19")),
321+
(50, pd.Timestamp("2018-01-20")),
322+
(50, pd.Timestamp("2018-01-21")),
323+
(60, pd.Timestamp("2018-01-14")),
324+
],
325+
names=["volume", "week_starting"],
326+
)
327+
expected = pd.DataFrame(
328+
data={
329+
"price": [
330+
10.0,
331+
9.928571428571429,
332+
9.857142857142858,
333+
9.785714285714286,
334+
9.714285714285714,
335+
9.642857142857142,
336+
9.571428571428571,
337+
9.5,
338+
9.428571428571429,
339+
9.357142857142858,
340+
9.285714285714286,
341+
9.214285714285714,
342+
9.142857142857142,
343+
9.071428571428571,
344+
9.0,
345+
11.0,
346+
],
347+
"volume": [50.0] * 15 + [60],
348+
},
349+
index=expected_ind,
350+
)
351+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)