diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 208bbfa10b9b2..050647b9e0ee5 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -814,7 +814,7 @@ Numeric ^^^^^^^ - Bug in :meth:`DataFrame.add` cannot apply ufunc when inputs contain mixed DataFrame type and Series type (:issue:`39853`) - Bug in arithmetic operations on :class:`Series` not propagating mask when combining masked dtypes and numpy dtypes (:issue:`45810`, :issue:`42630`) -- 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`) - Bug in :meth:`DataFrame.sem` and :meth:`Series.sem` where an erroneous ``TypeError`` would always raise when using data backed by an :class:`ArrowDtype` (:issue:`49759`) Conversion diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4a28ed131a986..1740eaf4fd8bf 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -134,7 +134,6 @@ is_integer_dtype, is_iterator, is_list_like, - is_object_dtype, is_scalar, is_sequence, needs_i8_conversion, @@ -10353,10 +10352,6 @@ def _reduce( axis = self._get_axis_number(axis) assert axis in [0, 1] - def func(values: np.ndarray): - # We only use this in the case that operates on self.values - return op(values, axis=axis, skipna=skipna, **kwds) - def blk_func(values, axis: Axis = 1): if isinstance(values, ExtensionArray): if not is_1d_only_ea_dtype(values.dtype) and not isinstance( @@ -10376,51 +10371,40 @@ def _get_data() -> DataFrame: data = self._get_bool_data() return data - if numeric_only or axis == 0: - # For numeric_only non-None and axis non-None, we know - # which blocks to use and no try/except is needed. - # For numeric_only=None only the case with axis==0 and no object - # dtypes are unambiguous can be handled with BlockManager.reduce - # Case with EAs see GH#35881 - df = self - if numeric_only: - df = _get_data() - if axis == 1: - df = df.T - axis = 0 - - # After possibly _get_data and transposing, we are now in the - # simple case where we can use BlockManager.reduce - res = df._mgr.reduce(blk_func) - out = df._constructor(res).iloc[0] - if out_dtype is not None: - out = out.astype(out_dtype) - if axis == 0 and len(self) == 0 and name in ["sum", "prod"]: - # Even if we are object dtype, follow numpy and return - # float64, see test_apply_funcs_over_empty - out = out.astype(np.float64) - - return out - - assert not numeric_only and axis == 1 - - data = self - values = data.values - result = func(values) - - 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 + # Case with EAs see GH#35881 + df = self + if numeric_only: + df = _get_data() + if axis == 1: + if len(df.index) == 0: + # Taking a transpose would result in no columns, losing the dtype. + # In the empty case, reducing along axis 0 or 1 gives the same + # result dtype, so reduce with axis=0 and ignore values + result = df._reduce( + op, + name, + axis=0, + skipna=skipna, + numeric_only=False, + filter_type=filter_type, + **kwds, + ).iloc[:0] + result.index = df.index + return result + df = df.T + + # After possibly _get_data and transposing, we are now in the + # simple case where we can use BlockManager.reduce + res = df._mgr.reduce(blk_func) + out = df._constructor(res).iloc[0] + if out_dtype is not None: + out = out.astype(out_dtype) + if len(self) == 0 and name in ("mean", "std", "var", "median", "sum", "prod"): + # Even if we are object dtype, follow numpy and return + # float64, see test_apply_funcs_over_empty + out = out.astype(np.float64) - labels = self._get_agg_axis(axis) - result = self._constructor_sliced(result, index=labels) - return result + return out def _reduce_axis1(self, name: str, func, skipna: bool) -> Series: """ diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py index 382da6b8325a5..741d26bca03f1 100644 --- a/pandas/core/internals/array_manager.py +++ b/pandas/core/internals/array_manager.py @@ -981,14 +981,10 @@ def reduce(self: T, func: Callable) -> T: # TODO NaT doesn't preserve dtype, so we need to ensure to create # a timedelta result array if original was timedelta # what if datetime results in timedelta? (eg std) - if res is NaT and is_timedelta64_ns_dtype(arr.dtype): - result_arrays.append(np.array(["NaT"], dtype="timedelta64[ns]")) - else: - # error: Argument 1 to "append" of "list" has incompatible type - # "ExtensionArray"; expected "ndarray" - result_arrays.append( - sanitize_array([res], None) # type: ignore[arg-type] - ) + dtype = arr.dtype if res is NaT else None + result_arrays.append( + sanitize_array([res], None, dtype=dtype) # type: ignore[arg-type] + ) index = Index._simple_new(np.array([None], dtype=object)) # placeholder columns = self.items diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index b8e2b1fafe326..f638ab139c0c7 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -622,7 +622,7 @@ def nansum( 3.0 """ values, mask, dtype, dtype_max, _ = _get_values( - values, skipna, fill_value=0, mask=mask + values, skipna, fill_value=0.0, mask=mask ) dtype_sum = dtype_max if is_float_dtype(dtype): @@ -1389,7 +1389,7 @@ def nanprod( if skipna and mask is not None: values = values.copy() - values[mask] = 1 + values[mask] = 1.0 result = values.prod(axis) # error: Incompatible return value type (got "Union[ndarray, float]", expected # "float") @@ -1500,7 +1500,15 @@ def _maybe_null_out( result[null_mask] = None elif result is not NaT: if check_below_min_count(shape, mask, min_count): - result = np.nan + result_dtype = getattr(result, "dtype", None) + if is_float_dtype(result_dtype): + # Preserve dtype when possible + # mypy doesn't infer result_dtype is not None + result = getattr( + np, f"float{8 * result_dtype.itemsize}" # type: ignore[union-attr] + )("nan") + else: + result = np.nan return result diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 7aaad4d2ad081..a3099c86bed14 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1433,7 +1433,7 @@ def test_apply_datetime_tz_issue(): def test_mixed_column_raises(df, method): # GH 16832 if method == "sum": - msg = r'can only concatenate str \(not "int"\) to str' + msg = r'can only concatenate str \(not "float"\) to str' else: msg = "not supported between instances of 'str' and 'float'" with pytest.raises(TypeError, match=msg): diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index a3cd3e4afdda1..d56a01199c83d 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -318,11 +318,26 @@ def wrapper(x): DataFrame({0: [np.nan, 2], 1: [np.nan, 3], 2: [np.nan, 4]}, dtype=object), ], ) - def test_stat_operators_attempt_obj_array(self, method, df): + def test_stat_operators_attempt_obj_array( + self, method, df, using_array_manager, axis + ): # GH#676 assert df.values.dtype == np.object_ - result = getattr(df, method)(1) - expected = getattr(df.astype("f8"), method)(1) + result = getattr(df, method)(axis=axis) + expected = getattr(df.astype("f8"), method)(axis=axis) + # With values an np.array with dtype object: + # - When using blocks, `values.sum(axis=1, ...)` returns a np.array of dim 1 + # and this remains object dtype + # - When using arrays, `values.sum(axis=0, ...)` returns a Python float + if not using_array_manager and method in ("sum", "prod", "min", "max"): + expected = expected.astype(object) + elif ( + using_array_manager + and axis in (0, "index") + and method in ("min", "max") + and 0 in df.columns + ): + expected = expected.astype("int64") tm.assert_series_equal(result, expected) @pytest.mark.parametrize("op", ["mean", "std", "var", "skew", "kurt", "sem"]) @@ -695,6 +710,32 @@ def test_sum_corner(self): assert len(axis0) == 0 assert len(axis1) == 0 + @pytest.mark.parametrize( + "index", + [ + tm.makeRangeIndex(0), + tm.makeDateIndex(0), + tm.makeNumericIndex(0, dtype=int), + tm.makeNumericIndex(0, dtype=float), + tm.makeDateIndex(0, freq="M"), + tm.makePeriodIndex(0), + ], + ) + def test_axis_1_empty(self, all_reductions, index, using_array_manager): + df = DataFrame(columns=["a"], index=index) + result = getattr(df, all_reductions)(axis=1) + expected_dtype = { + "any": "bool", + "all": "bool", + "count": "int64", + "max": "object", + "min": "object", + }.get(all_reductions, "float") + if using_array_manager and all_reductions in ("max", "min"): + expected_dtype = "float" + expected = Series([], index=index, dtype=expected_dtype) + tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("method, unit", [("sum", 0), ("prod", 1)]) @pytest.mark.parametrize("numeric_only", [None, True, False]) def test_sum_prod_nanops(self, method, unit, numeric_only):