Skip to content

Commit b2dc8bf

Browse files
jbrockmendeljreback
authored andcommitted
REF: separate out _get_cython_func_and_vals (#29537)
1 parent 29818dd commit b2dc8bf

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

pandas/core/groupby/ops.py

+35-14
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,40 @@ def get_func(fname):
393393

394394
return func
395395

396+
def _get_cython_func_and_vals(
397+
self, kind: str, how: str, values: np.ndarray, is_numeric: bool
398+
):
399+
"""
400+
Find the appropriate cython function, casting if necessary.
401+
402+
Parameters
403+
----------
404+
kind : sttr
405+
how : srt
406+
values : np.ndarray
407+
is_numeric : bool
408+
409+
Returns
410+
-------
411+
func : callable
412+
values : np.ndarray
413+
"""
414+
try:
415+
func = self._get_cython_function(kind, how, values, is_numeric)
416+
except NotImplementedError:
417+
if is_numeric:
418+
try:
419+
values = ensure_float64(values)
420+
except TypeError:
421+
if lib.infer_dtype(values, skipna=False) == "complex":
422+
values = values.astype(complex)
423+
else:
424+
raise
425+
func = self._get_cython_function(kind, how, values, is_numeric)
426+
else:
427+
raise
428+
return func, values
429+
396430
def _cython_operation(
397431
self, kind: str, values, how: str, axis: int, min_count: int = -1, **kwargs
398432
):
@@ -466,20 +500,7 @@ def _cython_operation(
466500
)
467501
out_shape = (self.ngroups,) + values.shape[1:]
468502

469-
try:
470-
func = self._get_cython_function(kind, how, values, is_numeric)
471-
except NotImplementedError:
472-
if is_numeric:
473-
try:
474-
values = ensure_float64(values)
475-
except TypeError:
476-
if lib.infer_dtype(values, skipna=False) == "complex":
477-
values = values.astype(complex)
478-
else:
479-
raise
480-
func = self._get_cython_function(kind, how, values, is_numeric)
481-
else:
482-
raise
503+
func, values = self._get_cython_func_and_vals(kind, how, values, is_numeric)
483504

484505
if how == "rank":
485506
out_dtype = "float"

0 commit comments

Comments
 (0)