Skip to content

Commit 91e251c

Browse files
authored
CoW: Avoid warning in case of expansion (#56391)
1 parent 025ccb5 commit 91e251c

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

pandas/core/internals/managers.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -1325,16 +1325,16 @@ def column_setitem(
13251325
This is a method on the BlockManager level, to avoid creating an
13261326
intermediate Series at the DataFrame level (`s = df[loc]; s[idx] = value`)
13271327
"""
1328+
needs_to_warn = False
13281329
if warn_copy_on_write() and not self._has_no_reference(loc):
13291330
if not isinstance(
13301331
self.blocks[self.blknos[loc]].values,
13311332
(ArrowExtensionArray, ArrowStringArray),
13321333
):
1333-
warnings.warn(
1334-
COW_WARNING_GENERAL_MSG,
1335-
FutureWarning,
1336-
stacklevel=find_stack_level(),
1337-
)
1334+
# We might raise if we are in an expansion case, so defer
1335+
# warning till we actually updated
1336+
needs_to_warn = True
1337+
13381338
elif using_copy_on_write() and not self._has_no_reference(loc):
13391339
blkno = self.blknos[loc]
13401340
# Split blocks to only copy the column we want to modify
@@ -1358,6 +1358,13 @@ def column_setitem(
13581358
new_mgr = col_mgr.setitem((idx,), value)
13591359
self.iset(loc, new_mgr._block.values, inplace=True)
13601360

1361+
if needs_to_warn:
1362+
warnings.warn(
1363+
COW_WARNING_GENERAL_MSG,
1364+
FutureWarning,
1365+
stacklevel=find_stack_level(),
1366+
)
1367+
13611368
def insert(self, loc: int, item: Hashable, value: ArrayLike, refs=None) -> None:
13621369
"""
13631370
Insert item at selected position.

pandas/tests/copy_view/test_indexing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1145,13 +1145,12 @@ def test_set_value_copy_only_necessary_column(
11451145
view = df[:]
11461146

11471147
if val == "a" and indexer[0] != slice(None):
1148-
# TODO(CoW-warn) assert the FutureWarning for CoW is also raised
11491148
with tm.assert_produces_warning(
11501149
FutureWarning, match="Setting an item of incompatible dtype is deprecated"
11511150
):
11521151
indexer_func(df)[indexer] = val
11531152
else:
1154-
with tm.assert_cow_warning(warn_copy_on_write):
1153+
with tm.assert_cow_warning(warn_copy_on_write and val == 100):
11551154
indexer_func(df)[indexer] = val
11561155

11571156
if using_copy_on_write:

pandas/tests/frame/indexing/test_indexing.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1397,9 +1397,7 @@ def test_iloc_setitem_enlarge_no_warning(self, warn_copy_on_write):
13971397
df = DataFrame(columns=["a", "b"])
13981398
expected = df.copy()
13991399
view = df[:]
1400-
# TODO(CoW-warn) false positive: shouldn't warn in case of enlargement?
1401-
with tm.assert_produces_warning(FutureWarning if warn_copy_on_write else None):
1402-
df.iloc[:, 0] = np.array([1, 2], dtype=np.float64)
1400+
df.iloc[:, 0] = np.array([1, 2], dtype=np.float64)
14031401
tm.assert_frame_equal(view, expected)
14041402

14051403
def test_loc_internals_not_updated_correctly(self):

0 commit comments

Comments
 (0)