diff --git a/pandas/core/resample.py b/pandas/core/resample.py index cb129ec272ff3..9053bbdecf237 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -394,15 +394,18 @@ def _gotitem(self, key, ndim: int, subset=None): grouper = self.grouper if subset is None: subset = self.obj + if key is not None: + subset = subset[key] + else: + # reached via Apply.agg_dict_like with selection=None and ndim=1 + assert subset.ndim == 1 + if ndim == 1: + assert subset.ndim == 1 + grouped = get_groupby( subset, by=None, grouper=grouper, axis=self.axis, group_keys=self.group_keys ) - - # try the key selection - try: - return grouped[key] - except KeyError: - return grouped + return grouped def _groupby_and_aggregate(self, how, *args, **kwargs): """ @@ -1214,6 +1217,11 @@ def _gotitem(self, key, ndim, subset=None): # create a new object to prevent aliasing if subset is None: subset = self.obj + if key is not None: + subset = subset[key] + else: + # reached via Apply.agg_dict_like with selection=None, ndim=1 + assert subset.ndim == 1 # Try to select from a DataFrame, falling back to a Series try: @@ -1228,6 +1236,8 @@ def _gotitem(self, key, ndim, subset=None): (lib.is_scalar(key) and key in subset) or lib.is_list_like(key) ): selection = key + elif subset.ndim == 1 and lib.is_scalar(key) and key == subset.name: + selection = key new_rs = type(self)( groupby=groupby, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index ef0524e48f9e2..57ed10e798928 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -303,6 +303,8 @@ def _gotitem(self, key, ndim, subset=None): (is_scalar(key) and key in subset) or is_list_like(key) ): selection = key + elif subset.ndim == 1 and is_scalar(key) and key == subset.name: + selection = key new_win = type(self)(subset, selection=selection, **kwargs) return new_win