From ca5718b8ca81b018afd5e4933e30f3ed57242740 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 10 Nov 2019 17:33:33 -0800 Subject: [PATCH] REF: separate out _get_cython_func_and_vals --- pandas/core/groupby/ops.py | 49 +++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index e438db6c620ec..bbf717cb3d26b 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -393,6 +393,40 @@ def get_func(fname): return func + def _get_cython_func_and_vals( + self, kind: str, how: str, values: np.ndarray, is_numeric: bool + ): + """ + Find the appropriate cython function, casting if necessary. + + Parameters + ---------- + kind : sttr + how : srt + values : np.ndarray + is_numeric : bool + + Returns + ------- + func : callable + values : np.ndarray + """ + try: + func = self._get_cython_function(kind, how, values, is_numeric) + except NotImplementedError: + if is_numeric: + try: + values = ensure_float64(values) + except TypeError: + if lib.infer_dtype(values, skipna=False) == "complex": + values = values.astype(complex) + else: + raise + func = self._get_cython_function(kind, how, values, is_numeric) + else: + raise + return func, values + def _cython_operation( self, kind: str, values, how: str, axis: int, min_count: int = -1, **kwargs ): @@ -466,20 +500,7 @@ def _cython_operation( ) out_shape = (self.ngroups,) + values.shape[1:] - try: - func = self._get_cython_function(kind, how, values, is_numeric) - except NotImplementedError: - if is_numeric: - try: - values = ensure_float64(values) - except TypeError: - if lib.infer_dtype(values, skipna=False) == "complex": - values = values.astype(complex) - else: - raise - func = self._get_cython_function(kind, how, values, is_numeric) - else: - raise + func, values = self._get_cython_func_and_vals(kind, how, values, is_numeric) if how == "rank": out_dtype = "float"