Skip to content

REF: groupby remove _selection_name #41401

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 3 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def agg_list_like(self) -> FrameOrSeriesUnion:
# i.e. obj is Series or DataFrame
selected_obj = obj
elif obj._selected_obj.ndim == 1:
# For SeriesGroupBy this matches _obj_with_exclusions
selected_obj = obj._selected_obj
else:
selected_obj = obj._obj_with_exclusions
Expand Down
10 changes: 0 additions & 10 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,6 @@ class SelectionMixin:
_internal_names = ["_cache", "__setstate__"]
_internal_names_set = set(_internal_names)

@property
def _selection_name(self):
"""
Return a name for myself;

This would ideally be called the 'name' property,
but we cannot conflict with the Series.name property which can be set.
"""
return self._selection

@final
@property
def _selection_list(self):
Expand Down
61 changes: 29 additions & 32 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
abc,
namedtuple,
)
import copy
from functools import partial
from textwrap import dedent
from typing import (
Expand Down Expand Up @@ -172,16 +171,8 @@ def _iterate_slices(self) -> Iterable[Series]:
yield self._selected_obj

@property
def _selection_name(self):
"""
since we are a series, we by definition only have
a single name, but may be the result of a selection or
the name of our object
"""
if self._selection is None:
return self.obj.name
else:
return self._selection
def _selection_name(self) -> Hashable:
return self.obj.name

_agg_examples_doc = dedent(
"""
Expand Down Expand Up @@ -316,15 +307,9 @@ def _aggregate_multiple_funcs(self, arg) -> DataFrame:

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

# reset the cache so that we
# only include the named selection
if name in self._selected_obj:
obj = copy.copy(obj)
obj._reset_cache()
obj._selection = name
results[base.OutputKey(label=name, position=idx)] = obj.aggregate(func)
key = base.OutputKey(label=name, position=idx)
results[key] = self.aggregate(func)

if any(isinstance(x, DataFrame) for x in results.values()):
from pandas import concat
Expand Down Expand Up @@ -466,7 +451,7 @@ def _wrap_applied_output(
# GH #6265
return self.obj._constructor(
[],
name=self._selection_name,
name=self.obj.name,
index=self.grouper.result_index,
dtype=data.dtype,
)
Expand All @@ -488,14 +473,14 @@ def _get_index() -> Index:
# if self.observed is False,
# keep all-NaN rows created while re-indexing
result = result.stack(dropna=self.observed)
result.name = self._selection_name
result.name = self.obj.name
return result
elif isinstance(values[0], (Series, DataFrame)):
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
else:
# GH #6265 #24880
result = self.obj._constructor(
data=values, index=_get_index(), name=self._selection_name
data=values, index=_get_index(), name=self.obj.name
)
return self._reindex_output(result)

Expand Down Expand Up @@ -533,7 +518,7 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
Transform with a callable func`.
"""
assert callable(func)
klass = type(self._selected_obj)
klass = type(self.obj)

results = []
for name, group in self:
Expand All @@ -555,8 +540,10 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
else:
result = self.obj._constructor(dtype=np.float64)

result.name = self._selected_obj.name
return result
result.name = self.obj.name
# error: Incompatible return value type (got "Union[DataFrame, Series]",
# expected "Series")
return result # type: ignore[return-value]

def _can_use_transform_fast(self, result) -> bool:
return True
Expand Down Expand Up @@ -676,7 +663,7 @@ def nunique(self, dropna: bool = True) -> Series:
res, out = np.zeros(len(ri), dtype=out.dtype), res
res[ids[idx]] = out

result = self.obj._constructor(res, index=ri, name=self._selection_name)
result = self.obj._constructor(res, index=ri, name=self.obj.name)
return self._reindex_output(result, fill_value=0)

@doc(Series.describe)
Expand Down Expand Up @@ -782,7 +769,7 @@ def apply_series_value_counts():
levels = [ping.group_index for ping in self.grouper.groupings] + [
lev # type: ignore[list-item]
]
names = self.grouper.names + [self._selection_name]
names = self.grouper.names + [self.obj.name]

if dropna:
mask = codes[-1] != -1
Expand Down Expand Up @@ -838,7 +825,7 @@ def build_codes(lev_codes: np.ndarray) -> np.ndarray:

if is_integer_dtype(out.dtype):
out = ensure_int64(out)
return self.obj._constructor(out, index=mi, name=self._selection_name)
return self.obj._constructor(out, index=mi, name=self.obj.name)

def count(self) -> Series:
"""
Expand All @@ -859,7 +846,7 @@ def count(self) -> Series:
result = self.obj._constructor(
out,
index=self.grouper.result_index,
name=self._selection_name,
name=self.obj.name,
dtype="int64",
)
return self._reindex_output(result, fill_value=0)
Expand Down Expand Up @@ -1026,7 +1013,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)

if isinstance(sobj, Series):
# GH#35246 test_groupby_as_index_select_column_sum_empty_df
result.columns = [self._selected_obj.name]
result.columns = [sobj.name]
else:
# select everything except for the last level, which is the one
# containing the name of the function(s), see GH#32040
Expand All @@ -1042,6 +1029,16 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)

agg = aggregate

@property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm can you kill this last one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated + green

def _selection_name(self):
"""
Return a name for myself;

This would ideally be called the 'name' property,
but we cannot conflict with the Series.name property which can be set.
"""
return self._selection

def _iterate_slices(self) -> Iterable[Series]:
obj = self._selected_obj
if self.axis == 1:
Expand Down Expand Up @@ -1152,11 +1149,11 @@ def _wrap_applied_output(self, data, keys, values, not_indexed_same=False):
# TODO: sure this is right? we used to do this
# after raising AttributeError above
return self.obj._constructor_sliced(
values, index=key_index, name=self._selection_name
values, index=key_index, name=self._selection
)
elif not isinstance(first_not_none, Series):
# values are not series or array-like but scalars
# self._selection_name not passed through to Series as the
# self._selection not passed through to Series as the
# result should not take the name of original selection
# of columns
if self.as_index:
Expand Down