Skip to content

Commit

Permalink
Backport PR #48853 on branch 1.5.x (REGR: Avoid unnecessary warning w…
Browse files Browse the repository at this point in the history
…hen 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>
  • Loading branch information
meeseeksmachine and phofl committed Sep 30, 2022
1 parent 96be757 commit 98948fc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 98948fc

Please sign in to comment.