Skip to content

Commit 60b6df6

Browse files
jbrockmendelMateusz Górski
authored and
Mateusz Górski
committed
REF: de-nest _get_cython_function (pandas-dev#29609)
1 parent 4181a8b commit 60b6df6

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1058,14 +1058,14 @@ def _cython_agg_blocks(
10581058

10591059
return new_items, new_blocks
10601060

1061-
def _aggregate_frame(self, func, *args, **kwargs):
1061+
def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
10621062
if self.grouper.nkeys != 1:
10631063
raise AssertionError("Number of keys must be 1")
10641064

10651065
axis = self.axis
10661066
obj = self._obj_with_exclusions
10671067

1068-
result = OrderedDict()
1068+
result = OrderedDict() # type: OrderedDict
10691069
if axis != obj._info_axis_number:
10701070
for name, data in self:
10711071
fres = func(data, *args, **kwargs)

pandas/core/groupby/ops.py

+21-35
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def apply(self, f, data: FrameOrSeries, axis: int = 0):
201201
continue
202202

203203
# group might be modified
204-
group_axes = _get_axes(group)
204+
group_axes = group.axes
205205
res = f(group)
206206
if not _is_indexed_like(res, group_axes):
207207
mutated = True
@@ -358,40 +358,33 @@ def _is_builtin_func(self, arg):
358358
def _get_cython_function(self, kind: str, how: str, values, is_numeric: bool):
359359

360360
dtype_str = values.dtype.name
361+
ftype = self._cython_functions[kind][how]
361362

362-
def get_func(fname):
363-
# see if there is a fused-type version of function
364-
# only valid for numeric
365-
f = getattr(libgroupby, fname, None)
366-
if f is not None and is_numeric:
367-
return f
368-
369-
# otherwise find dtype-specific version, falling back to object
370-
for dt in [dtype_str, "object"]:
371-
f2 = getattr(
372-
libgroupby,
373-
"{fname}_{dtype_str}".format(fname=fname, dtype_str=dt),
374-
None,
375-
)
376-
if f2 is not None:
377-
return f2
378-
379-
if hasattr(f, "__signatures__"):
380-
# inspect what fused types are implemented
381-
if dtype_str == "object" and "object" not in f.__signatures__:
382-
# return None so we get a NotImplementedError below
383-
# instead of a TypeError at runtime
384-
return None
363+
# see if there is a fused-type version of function
364+
# only valid for numeric
365+
f = getattr(libgroupby, ftype, None)
366+
if f is not None and is_numeric:
385367
return f
386368

387-
ftype = self._cython_functions[kind][how]
369+
# otherwise find dtype-specific version, falling back to object
370+
for dt in [dtype_str, "object"]:
371+
f2 = getattr(libgroupby, f"{ftype}_{dt}", None)
372+
if f2 is not None:
373+
return f2
374+
375+
if hasattr(f, "__signatures__"):
376+
# inspect what fused types are implemented
377+
if dtype_str == "object" and "object" not in f.__signatures__:
378+
# disallow this function so we get a NotImplementedError below
379+
# instead of a TypeError at runtime
380+
f = None
388381

389-
func = get_func(ftype)
382+
func = f
390383

391384
if func is None:
392385
raise NotImplementedError(
393-
"function is not implemented for this dtype: "
394-
"[how->{how},dtype->{dtype_str}]".format(how=how, dtype_str=dtype_str)
386+
f"function is not implemented for this dtype: "
387+
f"[how->{how},dtype->{dtype_str}]"
395388
)
396389

397390
return func
@@ -843,13 +836,6 @@ def agg_series(self, obj: Series, func):
843836
return grouper.get_result()
844837

845838

846-
def _get_axes(group):
847-
if isinstance(group, Series):
848-
return [group.index]
849-
else:
850-
return group.axes
851-
852-
853839
def _is_indexed_like(obj, axes) -> bool:
854840
if isinstance(obj, Series):
855841
if len(axes) > 1:

0 commit comments

Comments
 (0)