Skip to content

Commit

Permalink
exclude "expanding empty df" case
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Nov 28, 2023
1 parent 27bf409 commit b1a4777
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
5 changes: 1 addition & 4 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand All @@ -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. "
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b1a4777

Please sign in to comment.