From 38cfaeae5eed2f5de47780932c8810f6f64beff2 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Fri, 30 Sep 2022 01:43:58 +0200 Subject: [PATCH] Backport PR #48866: REGR: replace replacing wrong values with inplace and datetime --- doc/source/whatsnew/v1.5.1.rst | 1 + pandas/core/internals/blocks.py | 2 ++ pandas/tests/frame/methods/test_fillna.py | 13 +++++++++++++ 3 files changed, 16 insertions(+) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 4e29ad4ed844a..6690f067956a0 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -77,6 +77,7 @@ Fixed regressions - Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`) - Fixed regression causing an ``AttributeError`` during warning emitted if the provided table name in :meth:`DataFrame.to_sql` and the table name actually used in the database do not match (:issue:`48733`) - Fixed regression in :func:`to_datetime` when ``arg`` was a date string with nanosecond and ``format`` contained ``%f`` would raise a ``ValueError`` (:issue:`48767`) +- Fixed regression in :meth:`DataFrame.fillna` replacing wrong values for ``datetime64[ns]`` dtype and ``inplace=True`` (:issue:`48863`) - Fixed :meth:`.DataFrameGroupBy.size` not returning a Series when ``axis=1`` (:issue:`48738`) - Fixed Regression in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`) - diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 010358d3a21ec..eab463a03d27f 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1502,6 +1502,8 @@ def putmask(self, mask, new) -> list[Block]: mask = extract_bool_array(mask) values = self.values + if values.ndim == 2: + values = values.T orig_new = new orig_mask = mask diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index 697b28a65ac2e..ccd564b46cffa 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -13,6 +13,7 @@ TimedeltaIndex, Timestamp, date_range, + to_datetime, ) import pandas._testing as tm from pandas.tests.frame.common import _check_mixed_float @@ -682,6 +683,18 @@ def test_fillna_with_columns_and_limit(self): tm.assert_frame_equal(result, expected) tm.assert_frame_equal(result2, expected2) + def test_fillna_datetime_inplace(self): + # GH#48863 + df = DataFrame( + { + "date1": to_datetime(["2018-05-30", None]), + "date2": to_datetime(["2018-09-30", None]), + } + ) + expected = df.copy() + df.fillna(np.nan, inplace=True) + tm.assert_frame_equal(df, expected) + def test_fillna_inplace_with_columns_limit_and_value(self): # GH40989 df = DataFrame(