Skip to content

Commit 5a9de8b

Browse files
phoflmroeschke
andauthored
REGR: Avoid unnecessary warning when setting empty dataframe (#48853)
* REGR: Avoid unnecessary warning when setting empty dataframe * Update doc/source/whatsnew/v1.5.1.rst Co-authored-by: Matthew Roeschke <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent b94cbb5 commit 5a9de8b

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Fixed regressions
7272
- Fixed Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`)
7373
- Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`)
7474
- Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`)
75+
- Fixed regression in :meth:`DataFrame.loc` raising ``FutureWarning`` when setting an empty :class:`DataFrame` (:issue:`48480`)
7576
- Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`)
7677
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)
7778
- Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None:
20062006

20072007
new_values = self.obj._get_column_array(loc)
20082008

2009-
if can_hold_element(orig_values, new_values):
2009+
if can_hold_element(orig_values, new_values) and not len(new_values) == 0:
20102010
# Don't issue the warning yet, as we can still trim a few cases where
20112011
# behavior will not change.
20122012

pandas/tests/frame/indexing/test_indexing.py

+9
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,15 @@ def test_loc_setitem_reordering_with_all_true_indexer(self, col):
14141414
df.loc[n * [True], ["x", "y"]] = df[["x", "y"]]
14151415
tm.assert_frame_equal(df, expected)
14161416

1417+
def test_loc_rhs_empty_warning(self):
1418+
# GH48480
1419+
df = DataFrame(columns=["a", "b"])
1420+
expected = df.copy()
1421+
rhs = DataFrame(columns=["a"])
1422+
with tm.assert_produces_warning(None):
1423+
df.loc[:, "a"] = rhs
1424+
tm.assert_frame_equal(df, expected)
1425+
14171426

14181427
class TestDataFrameIndexingUInt64:
14191428
def test_setitem(self, uint64_frame):

pandas/tests/indexing/test_loc.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,7 @@ def test_loc_setitem_consistency_empty(self):
600600
expected = DataFrame(columns=["x", "y"])
601601
expected["x"] = expected["x"].astype(np.int64)
602602
df = DataFrame(columns=["x", "y"])
603-
msg = "will attempt to set the values inplace instead"
604-
with tm.assert_produces_warning(FutureWarning, match=msg):
603+
with tm.assert_produces_warning(None):
605604
df.loc[:, "x"] = 1
606605
tm.assert_frame_equal(df, expected)
607606

0 commit comments

Comments
 (0)