Skip to content

Commit b4e9566

Browse files
authored
Fix fillna accepting downcast as dict (pandas-dev#40861)
1 parent a5b666c commit b4e9566

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ Missing
714714

715715
- Bug in :class:`Grouper` now correctly propagates ``dropna`` argument and :meth:`DataFrameGroupBy.transform` now correctly handles missing values for ``dropna=True`` (:issue:`35612`)
716716
- Bug in :func:`isna`, and :meth:`Series.isna`, :meth:`Index.isna`, :meth:`DataFrame.isna` (and the corresponding ``notna`` functions) not recognizing ``Decimal("NaN")`` objects (:issue:`39409`)
717-
-
717+
- Bug in :meth:`DataFrame.fillna` not accepting dictionary for ``downcast`` keyword (:issue:`40809`)
718718

719719
MultiIndex
720720
^^^^^^^^^^

pandas/core/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6446,11 +6446,13 @@ def fillna(
64466446
)
64476447

64486448
result = self if inplace else self.copy()
6449+
is_dict = isinstance(downcast, dict)
64496450
for k, v in value.items():
64506451
if k not in result:
64516452
continue
64526453
obj = result[k]
6453-
obj.fillna(v, limit=limit, inplace=True, downcast=downcast)
6454+
downcast_k = downcast if not is_dict else downcast.get(k)
6455+
obj.fillna(v, limit=limit, inplace=True, downcast=downcast_k)
64546456
return result if not inplace else None
64556457

64566458
elif not is_list_like(value):

pandas/tests/frame/methods/test_fillna.py

+7
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,13 @@ def test_fill_corner(self, float_frame, float_string_frame):
531531
# TODO(wesm): unused?
532532
result = empty_float.fillna(value=0) # noqa
533533

534+
def test_fillna_downcast_dict(self):
535+
# GH#40809
536+
df = DataFrame({"col1": [1, np.nan]})
537+
result = df.fillna({"col1": 2}, downcast={"col1": "int64"})
538+
expected = DataFrame({"col1": [1, 2]})
539+
tm.assert_frame_equal(result, expected)
540+
534541

535542
def test_fillna_nonconsolidated_frame():
536543
# https://github.com/pandas-dev/pandas/issues/36495

0 commit comments

Comments
 (0)