Skip to content

Commit

Permalink
REF: separate out _get_cython_func_and_vals (pandas-dev#29537)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and Mateusz Górski committed Nov 18, 2019
1 parent ee0eaab commit fe74b41
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fe74b41

Please sign in to comment.