Skip to content

Commit 414d8f5

Browse files
Backport PR pandas-dev#45642: REGR: Series.fillna(downcast=False) (pandas-dev#45666)
Co-authored-by: jbrockmendel <[email protected]>
1 parent a225499 commit 414d8f5

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

doc/source/whatsnew/v1.4.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Regression in :meth:`Series.mask` with ``inplace=True`` and ``PeriodDtype`` and an incompatible ``other`` coercing to a common dtype instead of raising (:issue:`45546`)
18+
- Regression in :meth:`Series.fillna` with ``downcast=False`` incorrectly downcasting ``object`` dtype (:issue:`45603`)
1819
- Regression in :meth:`DataFrame.loc.__setitem__` losing :class:`Index` name if :class:`DataFrame` was empty before (:issue:`45621`)
1920
-
2021

pandas/core/internals/blocks.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ def split_and_operate(self, func, *args, **kwargs) -> list[Block]:
528528

529529
@final
530530
def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]:
531+
if downcast is False:
532+
return blocks
531533

532534
if self.dtype == _dtype_obj:
533535
# GH#44241 We downcast regardless of the argument;
@@ -541,10 +543,6 @@ def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]:
541543

542544
if downcast is None:
543545
return blocks
544-
if downcast is False:
545-
# turn if off completely
546-
# TODO: not reached, deprecate in favor of downcast=None
547-
return blocks
548546

549547
return extend_blocks([b._downcast_2d(downcast) for b in blocks])
550548

pandas/tests/frame/methods/test_fillna.py

+6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ def test_fillna_downcast(self):
244244
expected = DataFrame({"a": [1, 0]})
245245
tm.assert_frame_equal(result, expected)
246246

247+
def test_fillna_downcast_false(self, frame_or_series):
248+
# GH#45603 preserve object dtype with downcast=False
249+
obj = frame_or_series([1, 2, 3], dtype="object")
250+
result = obj.fillna("", downcast=False)
251+
tm.assert_equal(result, obj)
252+
247253
@pytest.mark.parametrize("columns", [["A", "A", "B"], ["A", "A"]])
248254
def test_fillna_dictlike_value_duplicate_colnames(self, columns):
249255
# GH#43476

0 commit comments

Comments
 (0)