Skip to content

Commit a7c8f77

Browse files
committed
REF: groupby internal names (pandas-dev#55551)
1 parent 7786be4 commit a7c8f77

File tree

8 files changed

+53
-53
lines changed

8 files changed

+53
-53
lines changed

pandas/api/typing/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from pandas.core.window import (
2020
Expanding,
21-
ExpandingGroupby,
21+
ExpandingGroupBy,
2222
ExponentialMovingWindow,
2323
ExponentialMovingWindowGroupby,
2424
Rolling,
@@ -35,7 +35,7 @@
3535
"DataFrameGroupBy",
3636
"DatetimeIndexResamplerGroupby",
3737
"Expanding",
38-
"ExpandingGroupby",
38+
"ExpandingGroupBy",
3939
"ExponentialMovingWindow",
4040
"ExponentialMovingWindowGroupby",
4141
"JsonReader",

pandas/core/groupby/generic.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
281281
return self.obj._constructor(
282282
[],
283283
name=self.obj.name,
284-
index=self.grouper.result_index,
284+
index=self.grouper.agg_index,
285285
dtype=obj.dtype,
286286
)
287287

@@ -306,8 +306,8 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
306306
stacklevel=find_stack_level(),
307307
)
308308

309-
# result is a dict whose keys are the elements of result_index
310-
result = Series(result, index=self.grouper.result_index)
309+
# result is a dict whose keys are the elements of agg_index
310+
result = Series(result, index=self.grouper.agg_index)
311311
result = self._wrap_aggregated_output(result)
312312
return result
313313

@@ -402,7 +402,7 @@ def _wrap_applied_output(
402402
# GH#47787 see test_group_on_empty_multiindex
403403
res_index = data.index
404404
else:
405-
res_index = self.grouper.result_index
405+
res_index = self.grouper.agg_index
406406

407407
return self.obj._constructor(
408408
[],
@@ -414,7 +414,7 @@ def _wrap_applied_output(
414414

415415
if isinstance(values[0], dict):
416416
# GH #823 #24880
417-
index = self.grouper.result_index
417+
index = self.grouper.agg_index
418418
res_df = self.obj._constructor_expanddim(values, index=index)
419419
res_df = self._reindex_output(res_df)
420420
# if self.observed is False,
@@ -437,7 +437,7 @@ def _wrap_applied_output(
437437
else:
438438
# GH #6265 #24880
439439
result = self.obj._constructor(
440-
data=values, index=self.grouper.result_index, name=self.obj.name
440+
data=values, index=self.grouper.agg_index, name=self.obj.name
441441
)
442442
if not self.as_index:
443443
result = self._insert_inaxis_grouper(result)
@@ -562,7 +562,7 @@ def _transform_general(
562562
from pandas.core.reshape.concat import concat
563563

564564
concatenated = concat(results)
565-
result = self._set_result_index_ordered(concatenated)
565+
result = self._set_agg_index_ordered(concatenated)
566566
else:
567567
result = self.obj._constructor(dtype=np.float64)
568568

@@ -706,7 +706,7 @@ def nunique(self, dropna: bool = True) -> Series | DataFrame:
706706
res = out
707707
else:
708708
res = out[1:]
709-
ri = self.grouper.result_index
709+
ri = self.grouper.agg_index
710710

711711
# we might have duplications among the bins
712712
if len(res) != len(ri):
@@ -1561,9 +1561,9 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
15611561
fres = func(grp_df, *args, **kwargs)
15621562
result[name] = fres
15631563

1564-
result_index = self.grouper.result_index
1564+
agg_index = self.grouper.agg_index
15651565
other_ax = obj.axes[1 - self.axis]
1566-
out = self.obj._constructor(result, index=other_ax, columns=result_index)
1566+
out = self.obj._constructor(result, index=other_ax, columns=agg_index)
15671567
if self.axis == 0:
15681568
out = out.T
15691569

@@ -1581,7 +1581,7 @@ def _wrap_applied_output(
15811581
# GH#47787 see test_group_on_empty_multiindex
15821582
res_index = data.index
15831583
else:
1584-
res_index = self.grouper.result_index
1584+
res_index = self.grouper.agg_index
15851585

15861586
result = self.obj._constructor(index=res_index, columns=data.columns)
15871587
result = result.astype(data.dtypes, copy=False)
@@ -1601,7 +1601,7 @@ def _wrap_applied_output(
16011601
is_transform=is_transform,
16021602
)
16031603

1604-
key_index = self.grouper.result_index if self.as_index else None
1604+
key_index = self.grouper.agg_index if self.as_index else None
16051605

16061606
if isinstance(first_not_none, (np.ndarray, Index)):
16071607
# GH#1738: values is list of arrays of unequal lengths
@@ -1767,7 +1767,7 @@ def _transform_general(self, func, engine, engine_kwargs, *args, **kwargs):
17671767
other_axis = 1 if self.axis == 0 else 0 # switches between 0 & 1
17681768
concatenated = concat(applied, axis=self.axis, verify_integrity=False)
17691769
concatenated = concatenated.reindex(concat_index, axis=other_axis, copy=False)
1770-
return self._set_result_index_ordered(concatenated)
1770+
return self._set_agg_index_ordered(concatenated)
17711771

17721772
__examples_dataframe_doc = dedent(
17731773
"""
@@ -2048,7 +2048,7 @@ def _apply_to_column_groupbys(self, func) -> DataFrame:
20482048

20492049
if not len(results):
20502050
# concat would raise
2051-
res_df = DataFrame([], columns=columns, index=self.grouper.result_index)
2051+
res_df = DataFrame([], columns=columns, index=self.grouper.agg_index)
20522052
else:
20532053
res_df = concat(results, keys=columns, axis=1)
20542054

pandas/core/groupby/groupby.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class providing the base-class of operations.
149149

150150
from pandas.core.resample import Resampler
151151
from pandas.core.window import (
152-
ExpandingGroupby,
152+
ExpandingGroupBy,
153153
ExponentialMovingWindowGroupby,
154154
RollingGroupby,
155155
)
@@ -1415,7 +1415,7 @@ def curried(x):
14151415
if self.grouper.has_dropped_na and is_transform:
14161416
# result will have dropped rows due to nans, fill with null
14171417
# and ensure index is ordered same as the input
1418-
result = self._set_result_index_ordered(result)
1418+
result = self._set_agg_index_ordered(result)
14191419
return result
14201420

14211421
# -----------------------------------------------------------------
@@ -1433,7 +1433,7 @@ def _concat_objects(
14331433
if self.group_keys and not is_transform:
14341434
if self.as_index:
14351435
# possible MI return case
1436-
group_keys = self.grouper.result_index
1436+
group_keys = self.grouper.agg_index
14371437
group_levels = self.grouper.levels
14381438
group_names = self.grouper.names
14391439

@@ -1490,10 +1490,10 @@ def _concat_objects(
14901490
return result
14911491

14921492
@final
1493-
def _set_result_index_ordered(
1493+
def _set_agg_index_ordered(
14941494
self, result: OutputFrameOrSeries
14951495
) -> OutputFrameOrSeries:
1496-
# set the result index on the passed values object and
1496+
# set the agg index on the passed values object and
14971497
# return the new object, xref 8046
14981498

14991499
obj_axis = self.obj._get_axis(self.axis)
@@ -1586,7 +1586,7 @@ def _wrap_aggregated_output(
15861586
index = Index(range(self.grouper.ngroups))
15871587

15881588
else:
1589-
index = self.grouper.result_index
1589+
index = self.grouper.agg_index
15901590

15911591
if qs is not None:
15921592
# We get here with len(qs) != 1 and not self.as_index
@@ -1674,7 +1674,7 @@ def _numba_agg_general(
16741674
res_mgr = df._mgr.apply(
16751675
aggregator, labels=ids, ngroups=ngroups, **aggregator_kwargs
16761676
)
1677-
res_mgr.axes[1] = self.grouper.result_index
1677+
res_mgr.axes[1] = self.grouper.agg_index
16781678
result = df._constructor_from_mgr(res_mgr, axes=res_mgr.axes)
16791679

16801680
if data.ndim == 1:
@@ -1745,7 +1745,7 @@ def _aggregate_with_numba(self, func, *args, engine_kwargs=None, **kwargs):
17451745
len(df.columns),
17461746
*args,
17471747
)
1748-
index = self.grouper.result_index
1748+
index = self.grouper.agg_index
17491749
if data.ndim == 1:
17501750
result_kwargs = {"name": data.name}
17511751
result = result.ravel()
@@ -2038,7 +2038,7 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:
20382038

20392039
# for each col, reshape to size of original frame by take operation
20402040
ids, _, _ = self.grouper.group_info
2041-
result = result.reindex(self.grouper.result_index, axis=self.axis, copy=False)
2041+
result = result.reindex(self.grouper.agg_index, axis=self.axis, copy=False)
20422042

20432043
if self.obj.ndim == 1:
20442044
# i.e. SeriesGroupBy
@@ -2814,7 +2814,7 @@ def _value_counts(
28142814
and not grouping._observed
28152815
for grouping in groupings
28162816
):
2817-
levels_list = [ping.result_index for ping in groupings]
2817+
levels_list = [ping.agg_index for ping in groupings]
28182818
multi_index, _ = MultiIndex.from_product(
28192819
levels_list, names=[ping.name for ping in groupings]
28202820
).sortlevel()
@@ -3514,7 +3514,7 @@ def ohlc(self) -> DataFrame:
35143514

35153515
agg_names = ["open", "high", "low", "close"]
35163516
result = self.obj._constructor_expanddim(
3517-
res_values, index=self.grouper.result_index, columns=agg_names
3517+
res_values, index=self.grouper.agg_index, columns=agg_names
35183518
)
35193519
return self._reindex_output(result)
35203520

@@ -3835,18 +3835,18 @@ def rolling(self, *args, **kwargs) -> RollingGroupby:
38353835
@final
38363836
@Substitution(name="groupby")
38373837
@Appender(_common_see_also)
3838-
def expanding(self, *args, **kwargs) -> ExpandingGroupby:
3838+
def expanding(self, *args, **kwargs) -> ExpandingGroupBy:
38393839
"""
38403840
Return an expanding grouper, providing expanding
38413841
functionality per group.
38423842
38433843
Returns
38443844
-------
3845-
pandas.api.typing.ExpandingGroupby
3845+
pandas.api.typing.ExpandingGroupBy
38463846
"""
3847-
from pandas.core.window import ExpandingGroupby
3847+
from pandas.core.window import ExpandingGroupBy
38483848

3849-
return ExpandingGroupby(
3849+
return ExpandingGroupBy(
38503850
self._selected_obj,
38513851
*args,
38523852
_grouper=self.grouper,
@@ -5594,7 +5594,7 @@ def _reindex_output(
55945594
output = output.drop(labels=list(g_names), axis=1)
55955595

55965596
# Set a temp index and reindex (possibly expanding)
5597-
output = output.set_index(self.grouper.result_index).reindex(
5597+
output = output.set_index(self.grouper.agg_index).reindex(
55985598
index, copy=False, fill_value=fill_value
55995599
)
56005600

@@ -5782,8 +5782,8 @@ def _idxmax_idxmin(
57825782
if len(self.grouper.groupings) == 1:
57835783
result_len = len(self.grouper.groupings[0].grouping_vector.unique())
57845784
else:
5785-
# result_index only contains observed groups in this case
5786-
result_len = len(self.grouper.result_index)
5785+
# agg_index only contains observed groups in this case
5786+
result_len = len(self.grouper.agg_index)
57875787
assert result_len <= expected_len
57885788
has_unobserved = result_len < expected_len
57895789

pandas/core/groupby/grouper.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def __init__(
591591
# error: Cannot determine type of "grouping_vector" [has-type]
592592
ng = newgrouper.groupings[0].grouping_vector # type: ignore[has-type]
593593
# use Index instead of ndarray so we can recover the name
594-
grouping_vector = Index(ng, name=newgrouper.result_index.name)
594+
grouping_vector = Index(ng, name=newgrouper.agg_index.name)
595595

596596
elif not isinstance(
597597
grouping_vector, (Series, Index, ExtensionArray, np.ndarray)
@@ -651,7 +651,7 @@ def name(self) -> Hashable:
651651
return self._orig_grouper.name
652652

653653
elif isinstance(self.grouping_vector, ops.BaseGrouper):
654-
return self.grouping_vector.result_index.name
654+
return self.grouping_vector.agg_index.name
655655

656656
elif isinstance(self.grouping_vector, Index):
657657
return self.grouping_vector.name
@@ -694,21 +694,21 @@ def codes(self) -> npt.NDArray[np.signedinteger]:
694694
@cache_readonly
695695
def group_arraylike(self) -> ArrayLike:
696696
"""
697-
Analogous to result_index, but holding an ArrayLike to ensure
697+
Analogous to agg_index, but holding an ArrayLike to ensure
698698
we can retain ExtensionDtypes.
699699
"""
700700
if self._all_grouper is not None:
701701
# retain dtype for categories, including unobserved ones
702-
return self.result_index._values
702+
return self.agg_index._values
703703

704704
elif self._passed_categorical:
705705
return self.group_index._values
706706

707707
return self._codes_and_uniques[1]
708708

709709
@cache_readonly
710-
def result_index(self) -> Index:
711-
# result_index retains dtype for categories, including unobserved ones,
710+
def agg_index(self) -> Index:
711+
# agg_index retains dtype for categories, including unobserved ones,
712712
# which group_index does not
713713
if self._all_grouper is not None:
714714
group_idx = self.group_index
@@ -788,7 +788,7 @@ def _codes_and_uniques(self) -> tuple[npt.NDArray[np.signedinteger], ArrayLike]:
788788
elif isinstance(self.grouping_vector, ops.BaseGrouper):
789789
# we have a list of groupers
790790
codes = self.grouping_vector.codes_info
791-
uniques = self.grouping_vector.result_index._values
791+
uniques = self.grouping_vecto.agg_index._values
792792
elif self._uniques is not None:
793793
# GH#50486 Code grouping_vector using _uniques; allows
794794
# including uniques that are not present in grouping_vector.

pandas/core/groupby/ops.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def group_keys_seq(self):
634634
@cache_readonly
635635
def indices(self) -> dict[Hashable, npt.NDArray[np.intp]]:
636636
"""dict {group name -> group indices}"""
637-
if len(self.groupings) == 1 and isinstance(self.result_index, CategoricalIndex):
637+
if len(self.groupings) == 1 and isinstance(self.agg_index, CategoricalIndex):
638638
# This shows unused categories in indices GH#38642
639639
return self.groupings[0].indices
640640
codes_list = [ping.codes for ping in self.groupings]
@@ -644,7 +644,7 @@ def indices(self) -> dict[Hashable, npt.NDArray[np.intp]]:
644644
@final
645645
def result_ilocs(self) -> npt.NDArray[np.intp]:
646646
"""
647-
Get the original integer locations of result_index in the input.
647+
Get the original integer locations of agg_index in the input.
648648
"""
649649
# Original indices are where group_index would go via sorting.
650650
# But when dropna is true, we need to remove null values while accounting for
@@ -692,7 +692,7 @@ def size(self) -> Series:
692692
out = np.bincount(ids[ids != -1], minlength=ngroups)
693693
else:
694694
out = []
695-
return Series(out, index=self.result_index, dtype="int64")
695+
return Series(out, index=self.agg_index, dtype="int64")
696696

697697
@cache_readonly
698698
def groups(self) -> dict[Hashable, np.ndarray]:
@@ -755,7 +755,7 @@ def _get_compressed_codes(
755755
@final
756756
@cache_readonly
757757
def ngroups(self) -> int:
758-
return len(self.result_index)
758+
return len(self.agg_index)
759759

760760
@property
761761
def reconstructed_codes(self) -> list[npt.NDArray[np.intp]]:
@@ -764,12 +764,12 @@ def reconstructed_codes(self) -> list[npt.NDArray[np.intp]]:
764764
return decons_obs_group_ids(ids, obs_ids, self.shape, codes, xnull=True)
765765

766766
@cache_readonly
767-
def result_index(self) -> Index:
767+
def agg_index(self) -> Index:
768768
if len(self.groupings) == 1:
769-
return self.groupings[0].result_index.rename(self.names[0])
769+
return self.groupings[0].agg_index.rename(self.names[0])
770770

771771
codes = self.reconstructed_codes
772-
levels = [ping.result_index for ping in self.groupings]
772+
levels = [ping.agg_index for ping in self.groupings]
773773
return MultiIndex(
774774
levels=levels, codes=codes, verify_integrity=False, names=self.names
775775
)
@@ -1071,7 +1071,7 @@ def reconstructed_codes(self) -> list[np.ndarray]:
10711071
return [np.r_[0, np.flatnonzero(self.bins[1:] != self.bins[:-1]) + 1]]
10721072

10731073
@cache_readonly
1074-
def result_index(self) -> Index:
1074+
def agg_index(self) -> Index:
10751075
if len(self.binlabels) != 0 and isna(self.binlabels[0]):
10761076
return self.binlabels[1:]
10771077

pandas/core/window/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
)
55
from pandas.core.window.expanding import (
66
Expanding,
7-
ExpandingGroupby,
7+
ExpandingGroupBy,
88
)
99
from pandas.core.window.rolling import (
1010
Rolling,
@@ -14,7 +14,7 @@
1414

1515
__all__ = [
1616
"Expanding",
17-
"ExpandingGroupby",
17+
"ExpandingGroupBy",
1818
"ExponentialMovingWindow",
1919
"ExponentialMovingWindowGroupby",
2020
"Rolling",

0 commit comments

Comments
 (0)