Skip to content

REF: de-nest _get_cython_function #29609

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 6 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ def _cython_agg_blocks(

return new_items, new_blocks

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

Expand Down
56 changes: 21 additions & 35 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def apply(self, f, data: FrameOrSeries, axis: int = 0):
continue

# group might be modified
group_axes = _get_axes(group)
group_axes = group.axes
res = f(group)
if not _is_indexed_like(res, group_axes):
mutated = True
Expand Down Expand Up @@ -358,40 +358,33 @@ def _is_builtin_func(self, arg):
def _get_cython_function(self, kind: str, how: str, values, is_numeric: bool):

dtype_str = values.dtype.name
ftype = self._cython_functions[kind][how]

def get_func(fname):
# see if there is a fused-type version of function
# only valid for numeric
f = getattr(libgroupby, fname, None)
if f is not None and is_numeric:
return f

# otherwise find dtype-specific version, falling back to object
for dt in [dtype_str, "object"]:
f2 = getattr(
libgroupby,
"{fname}_{dtype_str}".format(fname=fname, dtype_str=dt),
None,
)
if f2 is not None:
return f2

if hasattr(f, "__signatures__"):
# inspect what fused types are implemented
if dtype_str == "object" and "object" not in f.__signatures__:
# return None so we get a NotImplementedError below
# instead of a TypeError at runtime
return None
# see if there is a fused-type version of function
# only valid for numeric
f = getattr(libgroupby, ftype, None)
if f is not None and is_numeric:
return f

ftype = self._cython_functions[kind][how]
# otherwise find dtype-specific version, falling back to object
for dt in [dtype_str, "object"]:
f2 = getattr(libgroupby, f"{ftype}_{dt}", None)
if f2 is not None:
return f2

if hasattr(f, "__signatures__"):
# inspect what fused types are implemented
if dtype_str == "object" and "object" not in f.__signatures__:
# disallow this function so we get a NotImplementedError below
# instead of a TypeError at runtime
f = None

func = get_func(ftype)
func = f

if func is None:
raise NotImplementedError(
"function is not implemented for this dtype: "
"[how->{how},dtype->{dtype_str}]".format(how=how, dtype_str=dtype_str)
f"function is not implemented for this dtype: "
f"[how->{how},dtype->{dtype_str}]"
)

return func
Expand Down Expand Up @@ -839,13 +832,6 @@ def agg_series(self, obj: Series, func):
return grouper.get_result()


def _get_axes(group):
if isinstance(group, Series):
return [group.index]
else:
return group.axes


def _is_indexed_like(obj, axes) -> bool:
if isinstance(obj, Series):
if len(axes) > 1:
Expand Down