Skip to content

Commit 38546d0

Browse files
committed
fix issue
1 parent 956b800 commit 38546d0

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

pandas/core/resample.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import annotations
22

3+
from contextlib import nullcontext
34
import copy
45
from textwrap import dedent
56
from typing import (
67
TYPE_CHECKING,
78
Callable,
9+
ContextManager,
810
Hashable,
911
Literal,
1012
cast,
@@ -327,7 +329,13 @@ def pipe(
327329
axis="",
328330
)
329331
def aggregate(self, func=None, *args, **kwargs):
330-
result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
332+
context_manager: ContextManager
333+
if isinstance(self, DatetimeIndexResamplerGroupby):
334+
context_manager = com.temp_setattr(self._groupby, "as_index", True)
335+
else:
336+
context_manager = nullcontext()
337+
with context_manager:
338+
result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
331339
if result is None:
332340
how = func
333341
result = self._groupby_and_aggregate(how, *args, **kwargs)

pandas/core/window/rolling.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,8 @@ def _numba_apply(
659659
return self._resolve_output(out, obj)
660660

661661
def aggregate(self, func, *args, **kwargs):
662-
result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
662+
with com.temp_setattr(self._groupby, "as_index", True):
663+
result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
663664
if result is None:
664665
return self.apply(func, raw=False, args=args, kwargs=kwargs)
665666
return result

pandas/tests/resample/test_resampler_grouper.py

+26
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,29 @@ def test_groupby_resample_on_index_with_list_of_keys_missing_column():
659659
)
660660
with pytest.raises(KeyError, match="Columns not found"):
661661
df.groupby("group").resample("2D")[["val_not_in_dataframe"]].mean()
662+
663+
664+
def test_groupby_resample_agg_dict_works_for_as_index_false():
665+
# GH 52397
666+
expected = (
667+
DataFrame(
668+
{"a": np.repeat([0, 1, 2, 3, 4], 2), "b": range(50, 60)},
669+
index=date_range(start=Timestamp.now(), freq="1min", periods=10),
670+
)
671+
.groupby("a", as_index=True)
672+
.resample("2min")
673+
.min()
674+
)
675+
expected.drop(columns=["a"], inplace=True)
676+
677+
result = (
678+
DataFrame(
679+
{"a": np.repeat([0, 1, 2, 3, 4], 2), "b": range(50, 60)},
680+
index=date_range(start=Timestamp.now(), freq="1min", periods=10),
681+
)
682+
.groupby("a", as_index=False)
683+
.resample("2min")
684+
.agg({"b": "min"})
685+
)
686+
687+
tm.assert_frame_equal(expected, result)

0 commit comments

Comments
 (0)