From 5fa0847bd4fc7e7cb18e331f3d8c9181431799e7 Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Wed, 12 Jul 2023 17:40:07 -0400 Subject: [PATCH] BUG: groupby.resample(kind="period") raising AttributeError --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/resample.py | 8 +++++- .../tests/resample/test_resampler_grouper.py | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7450fc6fdc1da..dd99d5031e724 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -511,11 +511,11 @@ Groupby/resample/rolling - Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`) - Bug in :meth:`GroupBy.quantile` may implicitly sort the result index with ``sort=False`` (:issue:`53009`) - Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`) +- Bug in :meth:`DataFrameGroupby.resample` with ``kind="period"`` raising ``AttributeError`` (:issue:`24103`) - Bug in :meth:`Resampler.ohlc` with empty object returning a :class:`Series` instead of empty :class:`DataFrame` (:issue:`42902`) - Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` after performing column selection when using ``dropna="any"`` or ``dropna="all"`` would not subset columns (:issue:`53518`) - Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` raised after performing column selection when using ``dropna="any"`` or ``dropna="all"`` resulted in rows being dropped (:issue:`53518`) - Bug in :meth:`SeriesGroupBy.sum` and :meth:`DataFrameGroupby.sum` summing ``np.inf + np.inf`` and ``(-np.inf) + (-np.inf)`` to ``np.nan`` (:issue:`53606`) -- Reshaping ^^^^^^^^^ diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 6a4e33229690b..e4cebc01cccdd 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1797,7 +1797,13 @@ def _wrap_result(self, result): # we may have a different kind that we were asked originally # convert if needed if self.kind == "period" and not isinstance(result.index, PeriodIndex): - result.index = result.index.to_period(self.freq) + if isinstance(result.index, MultiIndex): + # GH 24103 - e.g. groupby resample + if not isinstance(result.index.levels[-1], PeriodIndex): + new_level = result.index.levels[-1].to_period(self.freq) + result.index = result.index.set_levels(new_level, level=-1) + else: + result.index = result.index.to_period(self.freq) return result diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index df14a5bc374c6..23b4f4bcf01d1 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -662,3 +662,29 @@ def test_groupby_resample_on_index_with_list_of_keys_missing_column(): ) with pytest.raises(KeyError, match="Columns not found"): df.groupby("group").resample("2D")[["val_not_in_dataframe"]].mean() + + +@pytest.mark.parametrize("kind", ["datetime", "period"]) +def test_groupby_resample_kind(kind): + # GH 24103 + df = DataFrame( + { + "datetime": pd.to_datetime( + ["20181101 1100", "20181101 1200", "20181102 1300", "20181102 1400"] + ), + "group": ["A", "B", "A", "B"], + "value": [1, 2, 3, 4], + } + ) + df = df.set_index("datetime") + result = df.groupby("group")["value"].resample("D", kind=kind).last() + + dt_level = pd.DatetimeIndex(["2018-11-01", "2018-11-02"]) + if kind == "period": + dt_level = dt_level.to_period(freq="D") + expected_index = pd.MultiIndex.from_product( + [["A", "B"], dt_level], + names=["group", "datetime"], + ) + expected = Series([1, 3, 2, 4], index=expected_index, name="value") + tm.assert_series_equal(result, expected)