Skip to content

REF: Remove sanitize_column from _iset_item #51702

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
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4105,9 +4105,11 @@ def _set_item_mgr(self, key, value: ArrayLike) -> None:
if len(self):
self._check_setitem_copy()

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

# check if we are modifying a copy
# try to set first as we want an invalid
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/copy_view/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,14 @@ def test_replace_list_none_inplace_refs(using_copy_on_write):
tm.assert_frame_equal(df_orig, view)
else:
assert np.shares_memory(arr, get_array(df, "a"))


def test_replace_columnwise_no_op(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]})
df_orig = df.copy()
df2 = df.replace({"a": 10}, 100)
df2.iloc[0, 0] = 100
if using_copy_on_write:
# TODO(CoW): This should share memory
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)