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

- Bug in :meth:`DataFrameGroupBy.count` and :meth:`SeriesGroupBy.sum` returning ``NaN`` for missing categories when grouped on multiple ``Categoricals``. Now returning ``0`` (:issue:`35028`)
- Bug in :meth:`DataFrameGroupBy.apply` that would some times throw an erroneous ``ValueError`` if the grouping axis had duplicate entries (:issue:`16646`)
-
-
- Bug when combining methods :meth:`DataFrame.groupby` with :meth:`DataFrame.resample` and :meth:`DataFrame.interpolate` raising an ``TypeError`` (:issue:`35325`)
- 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`)

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_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,65 @@ def test_upsample_sum(method, method_args, expected_values):
result = methodcaller(method, **method_args)(resampled)
expected = pd.Series(expected_values, index=index)
tm.assert_series_equal(result, expected)


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)