From 98948fcf7fa11418b12a1e14de47593887a6d33a Mon Sep 17 00:00:00 2001 From: MeeseeksMachine <39504233+meeseeksmachine@users.noreply.github.com> Date: Fri, 30 Sep 2022 11:34:38 +0200 Subject: [PATCH] Backport PR #48853 on branch 1.5.x (REGR: Avoid unnecessary warning when setting empty dataframe) (#48873) Backport PR #48853: REGR: Avoid unnecessary warning when setting empty dataframe Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com> --- 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 6690f067956a0..e5a7f6529e383 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:`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`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 08461b47b4c80..464cb3f07df33 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2002,7 +2002,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer): 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)