From 4709a830852e771f307467ec13a406e4c0c39e8c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 29 Sep 2022 13:26:26 +0200 Subject: [PATCH 1/2] REGR: Avoid unnecessary warning when setting empty dataframe --- doc/source/whatsnew/v1.5.1.rst | 1 + pandas/core/indexing.py | 2 +- pandas/tests/frame/indexing/test_indexing.py | 9 +++++++++ pandas/tests/indexing/test_loc.py | 3 +-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index ee66f2f649bc2..cb892e63b2c88 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -72,6 +72,7 @@ Fixed regressions - Fixed Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`) - Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`) - Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`) +- Fixed regression in :meth:`DataFrameloc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`) - Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`) - Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`) - Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 591ab9630ebb5..bc3708e4f84a2 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2006,7 +2006,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None: new_values = self.obj._get_column_array(loc) - if can_hold_element(orig_values, new_values): + if can_hold_element(orig_values, new_values) and not len(new_values) == 0: # Don't issue the warning yet, as we can still trim a few cases where # behavior will not change. diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 3062dff27d537..acd742c54b908 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1414,6 +1414,15 @@ def test_loc_setitem_reordering_with_all_true_indexer(self, col): df.loc[n * [True], ["x", "y"]] = df[["x", "y"]] tm.assert_frame_equal(df, expected) + def test_loc_rhs_empty_warning(self): + # GH48480 + df = DataFrame(columns=["a", "b"]) + expected = df.copy() + rhs = DataFrame(columns=["a"]) + with tm.assert_produces_warning(None): + df.loc[:, "a"] = rhs + tm.assert_frame_equal(df, expected) + class TestDataFrameIndexingUInt64: def test_setitem(self, uint64_frame): diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 4e5571c7087e7..e62fb98b0782d 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -600,8 +600,7 @@ def test_loc_setitem_consistency_empty(self): expected = DataFrame(columns=["x", "y"]) expected["x"] = expected["x"].astype(np.int64) df = DataFrame(columns=["x", "y"]) - msg = "will attempt to set the values inplace instead" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(None): df.loc[:, "x"] = 1 tm.assert_frame_equal(df, expected) From 3cecf4fb5f826601f8f6534f4327fb484106103d Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Thu, 29 Sep 2022 21:12:58 +0200 Subject: [PATCH 2/2] Update doc/source/whatsnew/v1.5.1.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v1.5.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index cb892e63b2c88..54febe3aecd71 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -72,7 +72,7 @@ Fixed regressions - Fixed Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`) - Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`) - Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`) -- Fixed regression in :meth:`DataFrameloc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`) +- Fixed regression in :meth:`DataFrame.loc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`) - Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`) - Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`) - Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)