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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ Groupby/resample/rolling
- Bug in :meth:`core.groupby.DataFrameGroupBy.transform` when ``func='nunique'`` and columns are of type ``datetime64``, the result would also be of type ``datetime64`` instead of ``int64`` (:issue:`35109`)
- Bug in :meth:`DataFrame.groupby` raising an ``AttributeError`` when selecting a column and aggregating with ``as_index=False`` (:issue:`35246`).
- Bug in :meth:'DataFrameGroupBy.first' and :meth:'DataFrameGroupBy.last' that would raise an unnecessary ``ValueError`` when grouping on multiple ``Categoricals`` (:issue:`34951`)
- Bug when combining methods :meth:`DataFrame.groupby` with :meth:`DataFrame.resample` and :meth:`DataFrame.interpolate` raising an ``TypeError`` (:issue:`35325`)
Copy link
Member

@arw2019 arw2019 Jul 22, 2020

Choose a reason for hiding this comment

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

I might do

- Bug in `meth`:`Resampler.interpolate` when combining with :meth:`DataFrame.groupby`, :meth:`DataFrame.resample` was raising a ``TypeError`` (:issue:`35325`)

Copy link
Member Author

Choose a reason for hiding this comment

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

Release note might need to be moved to 1.2 (see #34730)

Thanks for the link. I was looking for the whatsnew 1.2 but have seen that the PR, which will add this, is still open.

Copy link
Member

Choose a reason for hiding this comment

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

has now been merged


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)