From 4f903f6a43ef77b7c5c659b729b607bbec06f982 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Thu, 10 Nov 2022 08:53:11 -0500 Subject: [PATCH] BUG: DataFrame reductions with object dtype and axis=1 --- doc/source/whatsnew/v2.0.0.rst | 2 +- pandas/core/frame.py | 7 ------- pandas/tests/frame/test_reductions.py | 2 ++ 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 73a75667b46da..ca1d708786f6f 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -571,7 +571,7 @@ Timezones Numeric ^^^^^^^ - Bug in :meth:`DataFrame.add` cannot apply ufunc when inputs contain mixed DataFrame type and Series type (:issue:`39853`) -- Bug in DataFrame reduction methods (e.g. :meth:`DataFrame.sum`) with object dtype, ``axis=1`` and ``numeric_only=False`` would not be coerced to float (:issue:`49551`) +- Bug in DataFrame reduction methods (e.g. :meth:`DataFrame.sum`) with object dtype, ``axis=1`` and ``numeric_only=False`` would have results unnecessarily coerced to float; coercion still occurs for reductions that necessarily result in floats (``mean``, ``var``, ``std``, ``skew``) (:issue:`49603`) - Conversion diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1627a7add25ed..eef198059ca2e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -134,7 +134,6 @@ is_iterator, is_list_like, is_numeric_dtype, - is_object_dtype, is_scalar, is_sequence, needs_i8_conversion, @@ -10563,12 +10562,6 @@ def _get_data() -> DataFrame: if hasattr(result, "dtype"): if filter_type == "bool" and notna(result).all(): result = result.astype(np.bool_) - elif filter_type is None and is_object_dtype(result.dtype): - try: - result = result.astype(np.float64) - except (ValueError, TypeError): - # try to coerce to the original dtypes item by item if we can - pass labels = self._get_agg_axis(axis) result = self._constructor_sliced(result, index=labels) diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 0e5c6057b9a61..bfd9cd03df8f6 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -331,6 +331,8 @@ def test_stat_operators_attempt_obj_array(self, method, df): assert df.values.dtype == np.object_ result = getattr(df, method)(1) expected = getattr(df.astype("f8"), method)(1) + if method in ("sum", "prod", "min", "max"): + expected = expected.astype(object) tm.assert_series_equal(result, expected) @pytest.mark.parametrize("op", ["mean", "std", "var", "skew", "kurt", "sem"])