Skip to content

Commit 96cf851

Browse files
authored
REGR: Series.fillna(downcast=False) (#45642)
1 parent 20d0964 commit 96cf851

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
@@ -531,6 +531,8 @@ def split_and_operate(self, func, *args, **kwargs) -> list[Block]:
531531

532532
@final
533533
def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]:
534+
if downcast is False:
535+
return blocks
534536

535537
if self.dtype == _dtype_obj:
536538
# GH#44241 We downcast regardless of the argument;
@@ -544,10 +546,6 @@ def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]:
544546

545547
if downcast is None:
546548
return blocks
547-
if downcast is False:
548-
# turn if off completely
549-
# TODO: not reached, deprecate in favor of downcast=None
550-
return blocks
551549

552550
return extend_blocks([b._downcast_2d(downcast) for b in blocks])
553551

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)