diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index c8e811ce82b1f..ce57dc2e74e11 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -173,7 +173,7 @@ Groupby/resample/rolling Reshaping ^^^^^^^^^ -- +- Bug effecting all numeric and boolean reduction methods not returning subclassed data type. (:issue:`25596`) - Bug in :meth:`DataFrame.pivot_table` when only MultiIndexed columns is set (:issue:`17038`) - Bug in :meth:`DataFrame.unstack` and :meth:`Series.unstack` can take tuple names in MultiIndexed data (:issue:`19966`) - Bug in :meth:`DataFrame.pivot_table` when ``margin`` is ``True`` and only ``column`` is defined (:issue:`31016`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 40a5ce25f4422..7cc97fd104b2f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8005,7 +8005,7 @@ def _get_data(axis_matters): result = coerce_to_dtypes(result, self.dtypes) if constructor is not None: - result = Series(result, index=labels) + result = self._constructor_sliced(result, index=labels) return result def nunique(self, axis=0, dropna=True) -> Series: diff --git a/pandas/tests/frame/test_subclass.py b/pandas/tests/frame/test_subclass.py index 4a436d70dc48f..a2e7dc527c4b8 100644 --- a/pandas/tests/frame/test_subclass.py +++ b/pandas/tests/frame/test_subclass.py @@ -557,3 +557,17 @@ def strech(row): result = df.apply(lambda x: [1, 2, 3], axis=1) assert not isinstance(result, tm.SubclassedDataFrame) tm.assert_series_equal(result, expected) + + def test_subclassed_numeric_reductions(self, all_numeric_reductions): + # GH 25596 + + df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) + result = getattr(df, all_numeric_reductions)() + assert isinstance(result, tm.SubclassedSeries) + + def test_subclassed_boolean_reductions(self, all_boolean_reductions): + # GH 25596 + + df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) + result = getattr(df, all_boolean_reductions)() + assert isinstance(result, tm.SubclassedSeries)