Skip to content

WARN: Don't show FutureWarning when enlarging df with iloc #47621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2004,11 +2004,16 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
if (
isinstance(new_values, np.ndarray)
and isinstance(orig_values, np.ndarray)
and np.shares_memory(new_values, orig_values)
and (
np.shares_memory(new_values, orig_values)
or new_values.shape != orig_values.shape
)
):
# TODO: get something like tm.shares_memory working?
# The values were set inplace after all, no need to warn,
# e.g. test_rename_nocopy
# In case of enlarging we can not set inplace, so need to
# warn either
pass
else:
warnings.warn(
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,16 @@ def test_loc_setitem_rhs_frame(self, idxr, val):
expected = DataFrame({"a": [np.nan, val]})
tm.assert_frame_equal(df, expected)

@td.skip_array_manager_invalid_test
def test_iloc_setitem_enlarge_no_warning(self):
# GH#47381
df = DataFrame(columns=["a", "b"])
expected = df.copy()
view = df[:]
with tm.assert_produces_warning(None):
df.iloc[:, 0] = np.array([1, 2], dtype=np.float64)
tm.assert_frame_equal(view, expected)


class TestDataFrameIndexingUInt64:
def test_setitem(self, uint64_frame):
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,9 +806,7 @@ def test_setitem_string_column_numpy_dtype_raising(self):
def test_setitem_empty_df_duplicate_columns(self):
# GH#38521
df = DataFrame(columns=["a", "b", "b"], dtype="float64")
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.loc[:, "a"] = list(range(2))
df.loc[:, "a"] = list(range(2))
expected = DataFrame(
[[0, np.nan, np.nan], [1, np.nan, np.nan]], columns=["a", "b", "b"]
)
Expand Down