Skip to content

REGR: Series.fillna(downcast=False) #45642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

Expand Down
6 changes: 2 additions & 4 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,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;
Expand All @@ -544,10 +546,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])

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down