Skip to content

Commit e0cc505

Browse files
authored
REF: remove no-op casting (#41317)
1 parent c89b19f commit e0cc505

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

pandas/core/groupby/groupby.py

+1-17
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class providing the base-class of operations.
6060
doc,
6161
)
6262

63-
from pandas.core.dtypes.cast import maybe_downcast_numeric
6463
from pandas.core.dtypes.common import (
65-
ensure_float,
6664
is_bool_dtype,
6765
is_datetime64_dtype,
6866
is_integer_dtype,
@@ -1275,19 +1273,7 @@ def _python_agg_general(self, func, *args, **kwargs):
12751273
except TypeError:
12761274
continue
12771275

1278-
assert result is not None
12791276
key = base.OutputKey(label=name, position=idx)
1280-
1281-
if self.grouper._filter_empty_groups:
1282-
mask = counts.ravel() > 0
1283-
1284-
# since we are masking, make sure that we have a float object
1285-
values = result
1286-
if is_numeric_dtype(values.dtype):
1287-
values = ensure_float(values)
1288-
1289-
result = maybe_downcast_numeric(values[mask], result.dtype)
1290-
12911277
output[key] = result
12921278

12931279
if not output:
@@ -3086,9 +3072,7 @@ def _reindex_output(
30863072
Object (potentially) re-indexed to include all possible groups.
30873073
"""
30883074
groupings = self.grouper.groupings
3089-
if groupings is None:
3090-
return output
3091-
elif len(groupings) == 1:
3075+
if len(groupings) == 1:
30923076
return output
30933077

30943078
# if we only care about the observed values

pandas/core/groupby/ops.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
needs_i8_conversion,
5757
)
5858
from pandas.core.dtypes.dtypes import ExtensionDtype
59-
from pandas.core.dtypes.generic import ABCCategoricalIndex
6059
from pandas.core.dtypes.missing import (
6160
isna,
6261
maybe_fill,
@@ -89,6 +88,7 @@
8988
grouper,
9089
)
9190
from pandas.core.indexes.api import (
91+
CategoricalIndex,
9292
Index,
9393
MultiIndex,
9494
ensure_index,
@@ -676,7 +676,6 @@ def __init__(
676676
):
677677
assert isinstance(axis, Index), axis
678678

679-
self._filter_empty_groups = self.compressed = len(groupings) != 1
680679
self.axis = axis
681680
self._groupings: list[grouper.Grouping] = list(groupings)
682681
self.sort = sort
@@ -822,9 +821,7 @@ def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
822821
@cache_readonly
823822
def indices(self):
824823
""" dict {group name -> group indices} """
825-
if len(self.groupings) == 1 and isinstance(
826-
self.result_index, ABCCategoricalIndex
827-
):
824+
if len(self.groupings) == 1 and isinstance(self.result_index, CategoricalIndex):
828825
# This shows unused categories in indices GH#38642
829826
return self.groupings[0].indices
830827
codes_list = [ping.codes for ping in self.groupings]
@@ -913,7 +910,7 @@ def reconstructed_codes(self) -> list[np.ndarray]:
913910

914911
@cache_readonly
915912
def result_index(self) -> Index:
916-
if not self.compressed and len(self.groupings) == 1:
913+
if len(self.groupings) == 1:
917914
return self.groupings[0].result_index.rename(self.names[0])
918915

919916
codes = self.reconstructed_codes
@@ -924,7 +921,9 @@ def result_index(self) -> Index:
924921

925922
@final
926923
def get_group_levels(self) -> list[Index]:
927-
if not self.compressed and len(self.groupings) == 1:
924+
# Note: only called from _insert_inaxis_grouper_inplace, which
925+
# is only called for BaseGrouper, never for BinGrouper
926+
if len(self.groupings) == 1:
928927
return [self.groupings[0].result_index]
929928

930929
name_list = []
@@ -1091,7 +1090,6 @@ def __init__(
10911090
):
10921091
self.bins = ensure_int64(bins)
10931092
self.binlabels = ensure_index(binlabels)
1094-
self._filter_empty_groups = False
10951093
self.mutated = mutated
10961094
self.indexer = indexer
10971095

@@ -1201,10 +1199,9 @@ def names(self) -> list[Hashable]:
12011199

12021200
@property
12031201
def groupings(self) -> list[grouper.Grouping]:
1204-
return [
1205-
grouper.Grouping(lvl, lvl, in_axis=False, level=None, name=name)
1206-
for lvl, name in zip(self.levels, self.names)
1207-
]
1202+
lev = self.binlabels
1203+
ping = grouper.Grouping(lev, lev, in_axis=False, level=None, name=lev.name)
1204+
return [ping]
12081205

12091206
def _aggregate_series_fast(
12101207
self, obj: Series, func: F

0 commit comments

Comments
 (0)