Skip to content

Commit 4581f1b

Browse files
jbrockmendelJulianWgs
authored andcommitted
REF: groupby remove _selection_name (pandas-dev#41401)
1 parent 331cf5a commit 4581f1b

File tree

4 files changed

+21
-46
lines changed

4 files changed

+21
-46
lines changed

pandas/core/apply.py

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ def agg_list_like(self) -> FrameOrSeriesUnion:
322322
# i.e. obj is Series or DataFrame
323323
selected_obj = obj
324324
elif obj._selected_obj.ndim == 1:
325+
# For SeriesGroupBy this matches _obj_with_exclusions
325326
selected_obj = obj._selected_obj
326327
else:
327328
selected_obj = obj._obj_with_exclusions

pandas/core/base.py

-10
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,6 @@ class SelectionMixin(Generic[FrameOrSeries]):
180180
_internal_names = ["_cache", "__setstate__"]
181181
_internal_names_set = set(_internal_names)
182182

183-
@property
184-
def _selection_name(self):
185-
"""
186-
Return a name for myself;
187-
188-
This would ideally be called the 'name' property,
189-
but we cannot conflict with the Series.name property which can be set.
190-
"""
191-
return self._selection
192-
193183
@final
194184
@property
195185
def _selection_list(self):

pandas/core/groupby/generic.py

+17-34
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
abc,
1212
namedtuple,
1313
)
14-
import copy
1514
from functools import partial
1615
from textwrap import dedent
1716
from typing import (
@@ -169,18 +168,6 @@ class SeriesGroupBy(GroupBy[Series]):
169168
def _iterate_slices(self) -> Iterable[Series]:
170169
yield self._selected_obj
171170

172-
@property
173-
def _selection_name(self):
174-
"""
175-
since we are a series, we by definition only have
176-
a single name, but may be the result of a selection or
177-
the name of our object
178-
"""
179-
if self._selection is None:
180-
return self.obj.name
181-
else:
182-
return self._selection
183-
184171
_agg_examples_doc = dedent(
185172
"""
186173
Examples
@@ -314,15 +301,9 @@ def _aggregate_multiple_funcs(self, arg) -> DataFrame:
314301

315302
results: dict[base.OutputKey, FrameOrSeriesUnion] = {}
316303
for idx, (name, func) in enumerate(arg):
317-
obj = self
318304

319-
# reset the cache so that we
320-
# only include the named selection
321-
if name in self._selected_obj:
322-
obj = copy.copy(obj)
323-
obj._reset_cache()
324-
obj._selection = name
325-
results[base.OutputKey(label=name, position=idx)] = obj.aggregate(func)
305+
key = base.OutputKey(label=name, position=idx)
306+
results[key] = self.aggregate(func)
326307

327308
if any(isinstance(x, DataFrame) for x in results.values()):
328309
from pandas import concat
@@ -464,7 +445,7 @@ def _wrap_applied_output(
464445
# GH #6265
465446
return self.obj._constructor(
466447
[],
467-
name=self._selection_name,
448+
name=self.obj.name,
468449
index=self.grouper.result_index,
469450
dtype=data.dtype,
470451
)
@@ -485,14 +466,14 @@ def _get_index() -> Index:
485466
# if self.observed is False,
486467
# keep all-NaN rows created while re-indexing
487468
res_ser = res_df.stack(dropna=self.observed)
488-
res_ser.name = self._selection_name
469+
res_ser.name = self.obj.name
489470
return res_ser
490471
elif isinstance(values[0], (Series, DataFrame)):
491472
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
492473
else:
493474
# GH #6265 #24880
494475
result = self.obj._constructor(
495-
data=values, index=_get_index(), name=self._selection_name
476+
data=values, index=_get_index(), name=self.obj.name
496477
)
497478
return self._reindex_output(result)
498479

@@ -550,7 +531,7 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
550531
Transform with a callable func`.
551532
"""
552533
assert callable(func)
553-
klass = type(self._selected_obj)
534+
klass = type(self.obj)
554535

555536
results = []
556537
for name, group in self:
@@ -572,8 +553,10 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
572553
else:
573554
result = self.obj._constructor(dtype=np.float64)
574555

575-
result.name = self._selected_obj.name
576-
return result
556+
result.name = self.obj.name
557+
# error: Incompatible return value type (got "Union[DataFrame, Series]",
558+
# expected "Series")
559+
return result # type: ignore[return-value]
577560

578561
def _can_use_transform_fast(self, result) -> bool:
579562
return True
@@ -693,7 +676,7 @@ def nunique(self, dropna: bool = True) -> Series:
693676
res, out = np.zeros(len(ri), dtype=out.dtype), res
694677
res[ids[idx]] = out
695678

696-
result = self.obj._constructor(res, index=ri, name=self._selection_name)
679+
result = self.obj._constructor(res, index=ri, name=self.obj.name)
697680
return self._reindex_output(result, fill_value=0)
698681

699682
@doc(Series.describe)
@@ -799,7 +782,7 @@ def apply_series_value_counts():
799782
levels = [ping.group_index for ping in self.grouper.groupings] + [
800783
lev # type: ignore[list-item]
801784
]
802-
names = self.grouper.names + [self._selection_name]
785+
names = self.grouper.names + [self.obj.name]
803786

804787
if dropna:
805788
mask = codes[-1] != -1
@@ -855,7 +838,7 @@ def build_codes(lev_codes: np.ndarray) -> np.ndarray:
855838

856839
if is_integer_dtype(out.dtype):
857840
out = ensure_int64(out)
858-
return self.obj._constructor(out, index=mi, name=self._selection_name)
841+
return self.obj._constructor(out, index=mi, name=self.obj.name)
859842

860843
def count(self) -> Series:
861844
"""
@@ -876,7 +859,7 @@ def count(self) -> Series:
876859
result = self.obj._constructor(
877860
out,
878861
index=self.grouper.result_index,
879-
name=self._selection_name,
862+
name=self.obj.name,
880863
dtype="int64",
881864
)
882865
return self._reindex_output(result, fill_value=0)
@@ -1048,7 +1031,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
10481031

10491032
if isinstance(sobj, Series):
10501033
# GH#35246 test_groupby_as_index_select_column_sum_empty_df
1051-
result.columns = [self._selected_obj.name]
1034+
result.columns = [sobj.name]
10521035
else:
10531036
# select everything except for the last level, which is the one
10541037
# containing the name of the function(s), see GH#32040
@@ -1174,11 +1157,11 @@ def _wrap_applied_output(self, data, keys, values, not_indexed_same=False):
11741157
# TODO: sure this is right? we used to do this
11751158
# after raising AttributeError above
11761159
return self.obj._constructor_sliced(
1177-
values, index=key_index, name=self._selection_name
1160+
values, index=key_index, name=self._selection
11781161
)
11791162
elif not isinstance(first_not_none, Series):
11801163
# values are not series or array-like but scalars
1181-
# self._selection_name not passed through to Series as the
1164+
# self._selection not passed through to Series as the
11821165
# result should not take the name of original selection
11831166
# of columns
11841167
if self.as_index:

pandas/core/groupby/groupby.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1057,9 +1057,10 @@ def reset_identity(values):
10571057
values = reset_identity(values)
10581058
result = concat(values, axis=self.axis)
10591059

1060-
if isinstance(result, Series) and self._selection_name is not None:
1060+
name = self.obj.name if self.obj.ndim == 1 else self._selection
1061+
if isinstance(result, Series) and name is not None:
10611062

1062-
result.name = self._selection_name
1063+
result.name = name
10631064

10641065
return result
10651066

0 commit comments

Comments
 (0)