Skip to content

Commit 0511c49

Browse files
authored
BUG: groupby.resample(kind="period") raising AttributeError (#54099)
1 parent 4d38764 commit 0511c49

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,11 @@ Groupby/resample/rolling
511511
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)
512512
- Bug in :meth:`GroupBy.quantile` may implicitly sort the result index with ``sort=False`` (:issue:`53009`)
513513
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`)
514+
- Bug in :meth:`DataFrameGroupby.resample` with ``kind="period"`` raising ``AttributeError`` (:issue:`24103`)
514515
- Bug in :meth:`Resampler.ohlc` with empty object returning a :class:`Series` instead of empty :class:`DataFrame` (:issue:`42902`)
515516
- 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`)
516517
- 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`)
517518
- Bug in :meth:`SeriesGroupBy.sum` and :meth:`DataFrameGroupby.sum` summing ``np.inf + np.inf`` and ``(-np.inf) + (-np.inf)`` to ``np.nan`` (:issue:`53606`)
518-
-
519519

520520
Reshaping
521521
^^^^^^^^^

pandas/core/resample.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,13 @@ def _wrap_result(self, result):
17971797
# we may have a different kind that we were asked originally
17981798
# convert if needed
17991799
if self.kind == "period" and not isinstance(result.index, PeriodIndex):
1800-
result.index = result.index.to_period(self.freq)
1800+
if isinstance(result.index, MultiIndex):
1801+
# GH 24103 - e.g. groupby resample
1802+
if not isinstance(result.index.levels[-1], PeriodIndex):
1803+
new_level = result.index.levels[-1].to_period(self.freq)
1804+
result.index = result.index.set_levels(new_level, level=-1)
1805+
else:
1806+
result.index = result.index.to_period(self.freq)
18011807
return result
18021808

18031809

pandas/tests/resample/test_resampler_grouper.py

+26
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,29 @@ def test_groupby_resample_on_index_with_list_of_keys_missing_column():
662662
)
663663
with pytest.raises(KeyError, match="Columns not found"):
664664
df.groupby("group").resample("2D")[["val_not_in_dataframe"]].mean()
665+
666+
667+
@pytest.mark.parametrize("kind", ["datetime", "period"])
668+
def test_groupby_resample_kind(kind):
669+
# GH 24103
670+
df = DataFrame(
671+
{
672+
"datetime": pd.to_datetime(
673+
["20181101 1100", "20181101 1200", "20181102 1300", "20181102 1400"]
674+
),
675+
"group": ["A", "B", "A", "B"],
676+
"value": [1, 2, 3, 4],
677+
}
678+
)
679+
df = df.set_index("datetime")
680+
result = df.groupby("group")["value"].resample("D", kind=kind).last()
681+
682+
dt_level = pd.DatetimeIndex(["2018-11-01", "2018-11-02"])
683+
if kind == "period":
684+
dt_level = dt_level.to_period(freq="D")
685+
expected_index = pd.MultiIndex.from_product(
686+
[["A", "B"], dt_level],
687+
names=["group", "datetime"],
688+
)
689+
expected = Series([1, 3, 2, 4], index=expected_index, name="value")
690+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)