From d4c31fe9f53450015c21faf42b65ca2e348248eb Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 29 Sep 2022 18:01:15 +0200 Subject: [PATCH 1/3] REGR: replace replacing wrong values with inplace and datetime --- doc/source/whatsnew/v1.5.1.rst | 1 + pandas/core/internals/blocks.py | 5 ++++- pandas/tests/frame/methods/test_fillna.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index a8ec836ba93df..d6d7ed9ea4ad6 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 62d8d7e504b4a..b45a6d818d4aa 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1515,7 +1515,10 @@ def putmask(self, mask, new) -> list[Block]: """ mask = extract_bool_array(mask) - values = self.values + if self.ndim == 2: + values = self.values.T + else: + values = self.values 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( From f9f3a36ae2c8457f0a70b0492fc520e02996cd77 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 29 Sep 2022 19:28:04 +0200 Subject: [PATCH 2/3] Fix arrow --- pandas/core/internals/blocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index b45a6d818d4aa..3690ab07ee8c1 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1515,7 +1515,7 @@ def putmask(self, mask, new) -> list[Block]: """ mask = extract_bool_array(mask) - if self.ndim == 2: + if self.values.ndim == 2: values = self.values.T else: values = self.values From 586cbf9edcad76c2ec6abc974688335093b82090 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 29 Sep 2022 19:38:28 +0200 Subject: [PATCH 3/3] Refactor --- pandas/core/internals/blocks.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 3690ab07ee8c1..afe18493ec276 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1515,10 +1515,9 @@ def putmask(self, mask, new) -> list[Block]: """ mask = extract_bool_array(mask) - if self.values.ndim == 2: - values = self.values.T - else: - values = self.values + values = self.values + if values.ndim == 2: + values = values.T orig_new = new orig_mask = mask