diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 21e6f0ea57451..8b0fec2f2c6f9 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -682,7 +682,7 @@ Missing - Bug in :class:`Grouper` now correctly propagates ``dropna`` argument and :meth:`DataFrameGroupBy.transform` now correctly handles missing values for ``dropna=True`` (:issue:`35612`) - 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`) -- +- Bug in :meth:`DataFrame.fillna` not accepting dictionary for ``downcast`` keyword (:issue:`40809`) MultiIndex ^^^^^^^^^^ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4ef5aa1109074..611da0c1ef284 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6540,11 +6540,13 @@ def fillna( ) result = self if inplace else self.copy() + is_dict = isinstance(downcast, dict) for k, v in value.items(): if k not in result: continue obj = result[k] - obj.fillna(v, limit=limit, inplace=True, downcast=downcast) + downcast_k = downcast if not is_dict else downcast.get(k) + obj.fillna(v, limit=limit, inplace=True, downcast=downcast_k) return result if not inplace else None elif not is_list_like(value): diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index 564481d01abc8..b827547b0753e 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -531,6 +531,13 @@ def test_fill_corner(self, float_frame, float_string_frame): # TODO(wesm): unused? result = empty_float.fillna(value=0) # noqa + def test_fillna_downcast_dict(self): + # GH#40809 + df = DataFrame({"col1": [1, np.nan]}) + result = df.fillna({"col1": 2}, downcast={"col1": "int64"}) + expected = DataFrame({"col1": [1, 2]}) + tm.assert_frame_equal(result, expected) + def test_fillna_nonconsolidated_frame(): # https://github.com/pandas-dev/pandas/issues/36495