Skip to content

Commit 0a108f0

Browse files
jbrockmendeljreback
authored andcommitted
CLN: move small bits outside of try/excepts (#28962)
1 parent d52850f commit 0a108f0

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

pandas/_libs/algos_take_helper.pxi.in

-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ cdef _take_2d(ndarray[take_t, ndim=2] values, object idx):
276276
Py_ssize_t i, j, N, K
277277
ndarray[Py_ssize_t, ndim=2, cast=True] indexer = idx
278278
ndarray[take_t, ndim=2] result
279-
object val
280279

281280
N, K = (<object>values).shape
282281

pandas/core/base.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def aggregate(self, func, *args, **kwargs):
267267

268268
agg = aggregate
269269

270-
def _try_aggregate_string_function(self, arg, *args, **kwargs):
270+
def _try_aggregate_string_function(self, arg: str, *args, **kwargs):
271271
"""
272272
if arg is a string, then try to operate on it:
273273
- try to find a function (or attribute) on ourselves
@@ -292,12 +292,10 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs):
292292

293293
f = getattr(np, arg, None)
294294
if f is not None:
295-
try:
295+
if hasattr(self, "__array__"):
296+
# in particular exclude Window
296297
return f(self, *args, **kwargs)
297298

298-
except (AttributeError, TypeError):
299-
pass
300-
301299
raise AttributeError(
302300
"'{arg}' is not a valid function for "
303301
"'{cls}' object".format(arg=arg, cls=type(self).__name__)

pandas/core/groupby/generic.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
952952
if alt is None:
953953
# we cannot perform the operation
954954
# in an alternate way, exclude the block
955+
assert how == "ohlc"
955956
deleted_items.append(locs)
956957
continue
957958

@@ -1025,17 +1026,20 @@ def _aggregate_frame(self, func, *args, **kwargs):
10251026
if axis != obj._info_axis_number:
10261027
try:
10271028
for name, data in self:
1028-
result[name] = self._try_cast(func(data, *args, **kwargs), data)
1029+
fres = func(data, *args, **kwargs)
1030+
result[name] = self._try_cast(fres, data)
10291031
except Exception:
10301032
return self._aggregate_item_by_item(func, *args, **kwargs)
10311033
else:
10321034
for name in self.indices:
1035+
data = self.get_group(name, obj=obj)
10331036
try:
1034-
data = self.get_group(name, obj=obj)
1035-
result[name] = self._try_cast(func(data, *args, **kwargs), data)
1037+
fres = func(data, *args, **kwargs)
10361038
except Exception:
10371039
wrapper = lambda x: func(x, *args, **kwargs)
10381040
result[name] = data.apply(wrapper, axis=axis)
1041+
else:
1042+
result[name] = self._try_cast(fres, data)
10391043

10401044
return self._wrap_frame_output(result, obj)
10411045

@@ -1410,9 +1414,10 @@ def _transform_item_by_item(self, obj, wrapper):
14101414
for i, col in enumerate(obj):
14111415
try:
14121416
output[col] = self[col].transform(wrapper)
1413-
inds.append(i)
14141417
except Exception:
14151418
pass
1419+
else:
1420+
inds.append(i)
14161421

14171422
if len(output) == 0:
14181423
raise TypeError("Transform function invalid for data types")

pandas/core/groupby/groupby.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -598,14 +598,7 @@ def pipe(self, func, *args, **kwargs):
598598
plot = property(GroupByPlot)
599599

600600
def _make_wrapper(self, name):
601-
if name not in self._apply_whitelist:
602-
is_callable = callable(getattr(self._selected_obj, name, None))
603-
kind = " callable " if is_callable else " "
604-
msg = (
605-
"Cannot access{0}attribute {1!r} of {2!r} objects, try "
606-
"using the 'apply' method".format(kind, name, type(self).__name__)
607-
)
608-
raise AttributeError(msg)
601+
assert name in self._apply_whitelist
609602

610603
self._set_group_selection()
611604

@@ -919,9 +912,10 @@ def _python_agg_general(self, func, *args, **kwargs):
919912
for name, obj in self._iterate_slices():
920913
try:
921914
result, counts = self.grouper.agg_series(obj, f)
922-
output[name] = self._try_cast(result, obj, numeric_only=True)
923915
except TypeError:
924916
continue
917+
else:
918+
output[name] = self._try_cast(result, obj, numeric_only=True)
925919

926920
if len(output) == 0:
927921
return self._python_apply_general(f)

0 commit comments

Comments
 (0)