From c575e8dd29b4fd56e5fe65fda601adae1ca940d9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 15 Oct 2019 17:01:24 -0700 Subject: [PATCH 1/3] REF: simplify DataFrame._reduce --- pandas/core/frame.py | 49 +++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 79e941f262931..94d0d0a42aad6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7799,17 +7799,6 @@ def _reduce( def f(x): return op(x, axis=axis, skipna=skipna, **kwds) - # exclude timedelta/datetime unless we are uniform types - if ( - axis == 1 - and self._is_datelike_mixed_type - and ( - not self._is_homogeneous_type - and not is_datetime64tz_dtype(self.dtypes[0]) - ) - ): - numeric_only = True - if numeric_only is None: try: values = self.values @@ -7824,27 +7813,23 @@ def f(x): # try by-column first if filter_type is None and axis == 0: - try: - - # this can end up with a non-reduction - # but not always. if the types are mixed - # with datelike then need to make sure a series - - # we only end up here if we have not specified - # numeric_only and yet we have tried a - # column-by-column reduction, where we have mixed type. - # So let's just do what we can - from pandas.core.apply import frame_apply - - opa = frame_apply( - self, func=f, result_type="expand", ignore_failures=True - ) - result = opa.get_result() - if result.ndim == self.ndim: - result = result.iloc[0] - return result - except Exception: - pass + # this can end up with a non-reduction + # but not always. if the types are mixed + # with datelike then need to make sure a series + + # we only end up here if we have not specified + # numeric_only and yet we have tried a + # column-by-column reduction, where we have mixed type. + # So let's just do what we can + from pandas.core.apply import frame_apply + + opa = frame_apply( + self, func=f, result_type="expand", ignore_failures=True + ) + result = opa.get_result() + if result.ndim == self.ndim: + result = result.iloc[0] + return result if filter_type is None or filter_type == "numeric": data = self._get_numeric_data() From 78e7334d7d880d4cdd30c284d1962b42e0e9c923 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 16 Oct 2019 15:29:50 -0700 Subject: [PATCH 2/3] move self.values outside of try --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 94d0d0a42aad6..01f7bd58dea6f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7800,8 +7800,8 @@ def f(x): return op(x, axis=axis, skipna=skipna, **kwds) if numeric_only is None: + values = self.values try: - values = self.values result = f(values) if filter_type == "bool" and is_object_dtype(values) and axis is None: From 2ba02ba8d2f3327e45f9c6646d78b45b829690c7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 18 Oct 2019 17:42:13 -0700 Subject: [PATCH 3/3] Fixup unused import --- pandas/core/frame.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ccb371878db9c..c90bf4ba7151f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -68,7 +68,6 @@ infer_dtype_from_object, is_bool_dtype, is_datetime64_any_dtype, - is_datetime64tz_dtype, is_dict_like, is_dtype_equal, is_extension_array_dtype,