Skip to content

Commit ce32601

Browse files
authored
REF: Remove sanitize_column from _iset_item (#51702)
1 parent 02adb3d commit ce32601

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pandas/core/frame.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4111,9 +4111,11 @@ def _set_item_mgr(self, key, value: ArrayLike) -> None:
41114111
if len(self):
41124112
self._check_setitem_copy()
41134113

4114-
def _iset_item(self, loc: int, value) -> None:
4115-
arraylike = self._sanitize_column(value)
4116-
self._iset_item_mgr(loc, arraylike, inplace=True)
4114+
def _iset_item(self, loc: int, value: Series) -> None:
4115+
# We are only called from _replace_columnwise which guarantees that
4116+
# no reindex is necessary
4117+
# TODO(CoW): Optimize to avoid copy here, but have ton track refs
4118+
self._iset_item_mgr(loc, value._values.copy(), inplace=True)
41174119

41184120
# check if we are modifying a copy
41194121
# try to set first as we want an invalid

pandas/tests/copy_view/test_replace.py

+11
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,14 @@ def test_replace_list_none_inplace_refs(using_copy_on_write):
362362
tm.assert_frame_equal(df_orig, view)
363363
else:
364364
assert np.shares_memory(arr, get_array(df, "a"))
365+
366+
367+
def test_replace_columnwise_no_op(using_copy_on_write):
368+
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]})
369+
df_orig = df.copy()
370+
df2 = df.replace({"a": 10}, 100)
371+
if using_copy_on_write:
372+
# TODO(CoW): This should share memory
373+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
374+
df2.iloc[0, 0] = 100
375+
tm.assert_frame_equal(df, df_orig)

0 commit comments

Comments
 (0)