Skip to content

Commit 3ff1b5f

Browse files
authored
REF: remove unnecesary try/except (#35839)
1 parent e2a622c commit 3ff1b5f

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

pandas/core/groupby/generic.py

+29-32
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import numpy as np
3131

3232
from pandas._libs import lib
33-
from pandas._typing import FrameOrSeries, FrameOrSeriesUnion
33+
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion
3434
from pandas.util._decorators import Appender, Substitution, doc
3535

3636
from pandas.core.dtypes.cast import (
@@ -59,6 +59,7 @@
5959
validate_func_kwargs,
6060
)
6161
import pandas.core.algorithms as algorithms
62+
from pandas.core.arrays import ExtensionArray
6263
from pandas.core.base import DataError, SpecificationError
6364
import pandas.core.common as com
6465
from pandas.core.construction import create_series_with_explicit_dtype
@@ -1033,32 +1034,31 @@ def _cython_agg_blocks(
10331034

10341035
no_result = object()
10351036

1036-
def cast_result_block(result, block: "Block", how: str) -> "Block":
1037-
# see if we can cast the block to the desired dtype
1037+
def cast_agg_result(result, values: ArrayLike, how: str) -> ArrayLike:
1038+
# see if we can cast the values to the desired dtype
10381039
# this may not be the original dtype
10391040
assert not isinstance(result, DataFrame)
10401041
assert result is not no_result
10411042

1042-
dtype = maybe_cast_result_dtype(block.dtype, how)
1043+
dtype = maybe_cast_result_dtype(values.dtype, how)
10431044
result = maybe_downcast_numeric(result, dtype)
10441045

1045-
if block.is_extension and isinstance(result, np.ndarray):
1046-
# e.g. block.values was an IntegerArray
1047-
# (1, N) case can occur if block.values was Categorical
1046+
if isinstance(values, ExtensionArray) and isinstance(result, np.ndarray):
1047+
# e.g. values was an IntegerArray
1048+
# (1, N) case can occur if values was Categorical
10481049
# and result is ndarray[object]
10491050
# TODO(EA2D): special casing not needed with 2D EAs
10501051
assert result.ndim == 1 or result.shape[0] == 1
10511052
try:
10521053
# Cast back if feasible
1053-
result = type(block.values)._from_sequence(
1054-
result.ravel(), dtype=block.values.dtype
1054+
result = type(values)._from_sequence(
1055+
result.ravel(), dtype=values.dtype
10551056
)
10561057
except (ValueError, TypeError):
10571058
# reshape to be valid for non-Extension Block
10581059
result = result.reshape(1, -1)
10591060

1060-
agg_block: "Block" = block.make_block(result)
1061-
return agg_block
1061+
return result
10621062

10631063
def blk_func(block: "Block") -> List["Block"]:
10641064
new_blocks: List["Block"] = []
@@ -1092,28 +1092,25 @@ def blk_func(block: "Block") -> List["Block"]:
10921092
# Categoricals. This will done by later self._reindex_output()
10931093
# Doing it here creates an error. See GH#34951
10941094
sgb = get_groupby(obj, self.grouper, observed=True)
1095-
try:
1096-
result = sgb.aggregate(lambda x: alt(x, axis=self.axis))
1097-
except TypeError:
1098-
# we may have an exception in trying to aggregate
1099-
# continue and exclude the block
1100-
raise
1101-
else:
1102-
assert isinstance(result, (Series, DataFrame)) # for mypy
1103-
# In the case of object dtype block, it may have been split
1104-
# in the operation. We un-split here.
1105-
result = result._consolidate()
1106-
assert isinstance(result, (Series, DataFrame)) # for mypy
1107-
assert len(result._mgr.blocks) == 1
1108-
1109-
# unwrap DataFrame to get array
1110-
result = result._mgr.blocks[0].values
1111-
if isinstance(result, np.ndarray) and result.ndim == 1:
1112-
result = result.reshape(1, -1)
1113-
agg_block = cast_result_block(result, block, how)
1114-
new_blocks = [agg_block]
1095+
result = sgb.aggregate(lambda x: alt(x, axis=self.axis))
1096+
1097+
assert isinstance(result, (Series, DataFrame)) # for mypy
1098+
# In the case of object dtype block, it may have been split
1099+
# in the operation. We un-split here.
1100+
result = result._consolidate()
1101+
assert isinstance(result, (Series, DataFrame)) # for mypy
1102+
assert len(result._mgr.blocks) == 1
1103+
1104+
# unwrap DataFrame to get array
1105+
result = result._mgr.blocks[0].values
1106+
if isinstance(result, np.ndarray) and result.ndim == 1:
1107+
result = result.reshape(1, -1)
1108+
res_values = cast_agg_result(result, block.values, how)
1109+
agg_block = block.make_block(res_values)
1110+
new_blocks = [agg_block]
11151111
else:
1116-
agg_block = cast_result_block(result, block, how)
1112+
res_values = cast_agg_result(result, block.values, how)
1113+
agg_block = block.make_block(res_values)
11171114
new_blocks = [agg_block]
11181115
return new_blocks
11191116

0 commit comments

Comments
 (0)