diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index 1bd8da2d2b03c..21aab5058d25b 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -15,6 +15,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Regression in :meth:`Series.mask` with ``inplace=True`` and ``PeriodDtype`` and an incompatible ``other`` coercing to a common dtype instead of raising (:issue:`45546`) +- Regression in :meth:`Series.fillna` with ``downcast=False`` incorrectly downcasting ``object`` dtype (:issue:`45603`) - Regression in :meth:`DataFrame.loc.__setitem__` losing :class:`Index` name if :class:`DataFrame` was empty before (:issue:`45621`) - diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 1bc27c80ef58e..5810f3515c4ae 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -528,6 +528,8 @@ def split_and_operate(self, func, *args, **kwargs) -> list[Block]: @final def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]: + if downcast is False: + return blocks if self.dtype == _dtype_obj: # GH#44241 We downcast regardless of the argument; @@ -541,10 +543,6 @@ def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]: if downcast is None: return blocks - if downcast is False: - # turn if off completely - # TODO: not reached, deprecate in favor of downcast=None - return blocks return extend_blocks([b._downcast_2d(downcast) for b in blocks]) diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index b5bdf6a70199c..21a45d9ee1f20 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -244,6 +244,12 @@ def test_fillna_downcast(self): expected = DataFrame({"a": [1, 0]}) tm.assert_frame_equal(result, expected) + def test_fillna_downcast_false(self, frame_or_series): + # GH#45603 preserve object dtype with downcast=False + obj = frame_or_series([1, 2, 3], dtype="object") + result = obj.fillna("", downcast=False) + tm.assert_equal(result, obj) + @pytest.mark.parametrize("columns", [["A", "A", "B"], ["A", "A"]]) def test_fillna_dictlike_value_duplicate_colnames(self, columns): # GH#43476