Skip to content

REF: remove no-op casting #41317

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 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 1 addition & 17 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class providing the base-class of operations.
doc,
)

from pandas.core.dtypes.cast import maybe_downcast_numeric
from pandas.core.dtypes.common import (
ensure_float,
is_bool_dtype,
is_datetime64_dtype,
is_integer_dtype,
Expand Down Expand Up @@ -1271,19 +1269,7 @@ def _python_agg_general(self, func, *args, **kwargs):
except TypeError:
continue

assert result is not None
key = base.OutputKey(label=name, position=idx)

if self.grouper._filter_empty_groups:
mask = counts.ravel() > 0

# since we are masking, make sure that we have a float object
values = result
if is_numeric_dtype(values.dtype):
values = ensure_float(values)

result = maybe_downcast_numeric(values[mask], result.dtype)

output[key] = result

if not output:
Expand Down Expand Up @@ -3035,9 +3021,7 @@ def _reindex_output(
Object (potentially) re-indexed to include all possible groups.
"""
groupings = self.grouper.groupings
if groupings is None:
return output
elif len(groupings) == 1:
if len(groupings) == 1:
return output

# if we only care about the observed values
Expand Down
21 changes: 9 additions & 12 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
needs_i8_conversion,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import ABCCategoricalIndex
from pandas.core.dtypes.missing import (
isna,
maybe_fill,
Expand Down Expand Up @@ -89,6 +88,7 @@
grouper,
)
from pandas.core.indexes.api import (
CategoricalIndex,
Index,
MultiIndex,
ensure_index,
Expand Down Expand Up @@ -676,7 +676,6 @@ def __init__(
):
assert isinstance(axis, Index), axis

self._filter_empty_groups = self.compressed = len(groupings) != 1
self.axis = axis
self._groupings: list[grouper.Grouping] = list(groupings)
self.sort = sort
Expand Down Expand Up @@ -822,9 +821,7 @@ def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
@cache_readonly
def indices(self):
""" dict {group name -> group indices} """
if len(self.groupings) == 1 and isinstance(
self.result_index, ABCCategoricalIndex
):
if len(self.groupings) == 1 and isinstance(self.result_index, CategoricalIndex):
# This shows unused categories in indices GH#38642
return self.groupings[0].indices
codes_list = [ping.codes for ping in self.groupings]
Expand Down Expand Up @@ -913,7 +910,7 @@ def reconstructed_codes(self) -> list[np.ndarray]:

@cache_readonly
def result_index(self) -> Index:
if not self.compressed and len(self.groupings) == 1:
if len(self.groupings) == 1:
return self.groupings[0].result_index.rename(self.names[0])

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

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

name_list = []
Expand Down Expand Up @@ -1091,7 +1090,6 @@ def __init__(
):
self.bins = ensure_int64(bins)
self.binlabels = ensure_index(binlabels)
self._filter_empty_groups = False
self.mutated = mutated
self.indexer = indexer

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

@property
def groupings(self) -> list[grouper.Grouping]:
return [
grouper.Grouping(lvl, lvl, in_axis=False, level=None, name=name)
for lvl, name in zip(self.levels, self.names)
]
lev = self.binlabels
ping = grouper.Grouping(lev, lev, in_axis=False, level=None, name=lev.name)
return [ping]

def _aggregate_series_fast(
self, obj: Series, func: F
Expand Down