Skip to content

fix bug when combining groupby with resample and interpolate with dat… #35360

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
4 changes: 1 addition & 3 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ Plotting
Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^

-
-

- Bug when combining methods :meth:`DataFrame.groupby` with :meth:`DataFrame.resample` and :meth:`DataFrame.interpolate` raising an ``TypeError`` (:issue:`35325`)

Reshaping
^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ def interpolate(
"""
Interpolate values according to different methods.
"""
result = self._upsample(None)
result = self._upsample("asfreq")
return result.interpolate(
method=method,
axis=axis,
Expand Down
62 changes: 62 additions & 0 deletions pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,65 @@ def test_median_duplicate_columns():
result = df.resample("5s").median()
expected.columns = result.columns
tm.assert_frame_equal(result, expected)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this to pandas/tests/resample/test_time_grouper.py


def test_groupby_resample_interpolate():
# GH 35325
d = {"price": [10, 11, 9], "volume": [50, 60, 50]}

df = pd.DataFrame(d)

df["week_starting"] = pd.date_range("01/01/2018", periods=3, freq="W")

result = (
df.set_index("week_starting")
.groupby("volume")
.resample("1D")
.interpolate(method="linear")
)
expected_ind = pd.MultiIndex.from_tuples(
[
(50, "2018-01-07"),
(50, pd.Timestamp("2018-01-08")),
(50, pd.Timestamp("2018-01-09")),
(50, pd.Timestamp("2018-01-10")),
(50, pd.Timestamp("2018-01-11")),
(50, pd.Timestamp("2018-01-12")),
(50, pd.Timestamp("2018-01-13")),
(50, pd.Timestamp("2018-01-14")),
(50, pd.Timestamp("2018-01-15")),
(50, pd.Timestamp("2018-01-16")),
(50, pd.Timestamp("2018-01-17")),
(50, pd.Timestamp("2018-01-18")),
(50, pd.Timestamp("2018-01-19")),
(50, pd.Timestamp("2018-01-20")),
(50, pd.Timestamp("2018-01-21")),
(60, pd.Timestamp("2018-01-14")),
],
names=["volume", "week_starting"],
)
expected = pd.DataFrame(
data={
"price": [
10.0,
9.928571428571429,
9.857142857142858,
9.785714285714286,
9.714285714285714,
9.642857142857142,
9.571428571428571,
9.5,
9.428571428571429,
9.357142857142858,
9.285714285714286,
9.214285714285714,
9.142857142857142,
9.071428571428571,
9.0,
11.0,
],
"volume": [50.0] * 15 + [60],
},
index=expected_ind,
)
tm.assert_frame_equal(result, expected)