Skip to content

BUG: groupby.resample(kind="period") raising AttributeError #54099

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 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)