11
11
abc ,
12
12
namedtuple ,
13
13
)
14
- import copy
15
14
from functools import partial
16
15
from textwrap import dedent
17
16
from typing import (
@@ -169,18 +168,6 @@ class SeriesGroupBy(GroupBy[Series]):
169
168
def _iterate_slices (self ) -> Iterable [Series ]:
170
169
yield self ._selected_obj
171
170
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
-
184
171
_agg_examples_doc = dedent (
185
172
"""
186
173
Examples
@@ -314,15 +301,9 @@ def _aggregate_multiple_funcs(self, arg) -> DataFrame:
314
301
315
302
results : dict [base .OutputKey , FrameOrSeriesUnion ] = {}
316
303
for idx , (name , func ) in enumerate (arg ):
317
- obj = self
318
304
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 )
326
307
327
308
if any (isinstance (x , DataFrame ) for x in results .values ()):
328
309
from pandas import concat
@@ -464,7 +445,7 @@ def _wrap_applied_output(
464
445
# GH #6265
465
446
return self .obj ._constructor (
466
447
[],
467
- name = self ._selection_name ,
448
+ name = self .obj . name ,
468
449
index = self .grouper .result_index ,
469
450
dtype = data .dtype ,
470
451
)
@@ -485,14 +466,14 @@ def _get_index() -> Index:
485
466
# if self.observed is False,
486
467
# keep all-NaN rows created while re-indexing
487
468
res_ser = res_df .stack (dropna = self .observed )
488
- res_ser .name = self ._selection_name
469
+ res_ser .name = self .obj . name
489
470
return res_ser
490
471
elif isinstance (values [0 ], (Series , DataFrame )):
491
472
return self ._concat_objects (keys , values , not_indexed_same = not_indexed_same )
492
473
else :
493
474
# GH #6265 #24880
494
475
result = self .obj ._constructor (
495
- data = values , index = _get_index (), name = self ._selection_name
476
+ data = values , index = _get_index (), name = self .obj . name
496
477
)
497
478
return self ._reindex_output (result )
498
479
@@ -550,7 +531,7 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
550
531
Transform with a callable func`.
551
532
"""
552
533
assert callable (func )
553
- klass = type (self ._selected_obj )
534
+ klass = type (self .obj )
554
535
555
536
results = []
556
537
for name , group in self :
@@ -572,8 +553,10 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
572
553
else :
573
554
result = self .obj ._constructor (dtype = np .float64 )
574
555
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]
577
560
578
561
def _can_use_transform_fast (self , result ) -> bool :
579
562
return True
@@ -693,7 +676,7 @@ def nunique(self, dropna: bool = True) -> Series:
693
676
res , out = np .zeros (len (ri ), dtype = out .dtype ), res
694
677
res [ids [idx ]] = out
695
678
696
- result = self .obj ._constructor (res , index = ri , name = self ._selection_name )
679
+ result = self .obj ._constructor (res , index = ri , name = self .obj . name )
697
680
return self ._reindex_output (result , fill_value = 0 )
698
681
699
682
@doc (Series .describe )
@@ -799,7 +782,7 @@ def apply_series_value_counts():
799
782
levels = [ping .group_index for ping in self .grouper .groupings ] + [
800
783
lev # type: ignore[list-item]
801
784
]
802
- names = self .grouper .names + [self ._selection_name ]
785
+ names = self .grouper .names + [self .obj . name ]
803
786
804
787
if dropna :
805
788
mask = codes [- 1 ] != - 1
@@ -855,7 +838,7 @@ def build_codes(lev_codes: np.ndarray) -> np.ndarray:
855
838
856
839
if is_integer_dtype (out .dtype ):
857
840
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 )
859
842
860
843
def count (self ) -> Series :
861
844
"""
@@ -876,7 +859,7 @@ def count(self) -> Series:
876
859
result = self .obj ._constructor (
877
860
out ,
878
861
index = self .grouper .result_index ,
879
- name = self ._selection_name ,
862
+ name = self .obj . name ,
880
863
dtype = "int64" ,
881
864
)
882
865
return self ._reindex_output (result , fill_value = 0 )
@@ -1048,7 +1031,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
1048
1031
1049
1032
if isinstance (sobj , Series ):
1050
1033
# GH#35246 test_groupby_as_index_select_column_sum_empty_df
1051
- result .columns = [self . _selected_obj .name ]
1034
+ result .columns = [sobj .name ]
1052
1035
else :
1053
1036
# select everything except for the last level, which is the one
1054
1037
# 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):
1174
1157
# TODO: sure this is right? we used to do this
1175
1158
# after raising AttributeError above
1176
1159
return self .obj ._constructor_sliced (
1177
- values , index = key_index , name = self ._selection_name
1160
+ values , index = key_index , name = self ._selection
1178
1161
)
1179
1162
elif not isinstance (first_not_none , Series ):
1180
1163
# 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
1182
1165
# result should not take the name of original selection
1183
1166
# of columns
1184
1167
if self .as_index :
0 commit comments