diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 7822f94b6abab..abc8efa8fd83b 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2112,9 +2112,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None: # then we can use value's dtype (or inferred dtype) # instead of object dtype = self.obj.dtypes.iloc[loc] - if dtype not in (np.void, object) and not isinstance( - value, ABCDataFrame - ): + if dtype != np.void and not (self.obj.empty): # Exclude np.void, as that is a special case for expansion. # We want to warn for # df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) @@ -2123,7 +2121,6 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None: # df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) # df.loc[:, 'b'] = .3 # Exclude `object`, as then no upcasting happens. - # Exclude ABCDataFrame due to https://github.com/pandas-dev/pandas/issues/56223 warnings.warn( f"Setting an item of incompatible dtype is deprecated " "and will raise in a future error of pandas. " diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 61220aaa94f92..d9e193dc4a08d 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1207,8 +1207,7 @@ def test_loc_setitem_empty_append_expands_rows_mixed_dtype(self): df = DataFrame(columns=["x", "y"]) df["x"] = df["x"].astype(np.int64) - with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"): - df.loc[:, "x"] = data + df.loc[:, "x"] = data tm.assert_frame_equal(df, expected) def test_loc_setitem_empty_append_single_value(self): @@ -1488,7 +1487,11 @@ def test_loc_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture): # if result started off with object dtype, then the .loc.__setitem__ # below would retain object dtype result = DataFrame(index=idx, columns=["var"], dtype=np.float64) - result.loc[:, idxer] = expected + with tm.assert_produces_warning( + FutureWarning if idxer == "var" else None, match="incompatible dtype" + ): + # See https://github.com/pandas-dev/pandas/issues/56223 + result.loc[:, idxer] = expected tm.assert_frame_equal(result, expected) def test_loc_setitem_time_key(self, using_array_manager):