Skip to content

Commit 97c4ef6

Browse files
authored
CLN: Cleanup after CoW setitem PRs (#53142)
1 parent 1a254df commit 97c4ef6

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

doc/source/whatsnew/v2.1.0.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ including other versions of pandas.
1414
Enhancements
1515
~~~~~~~~~~~~
1616

17-
.. _whatsnew_210.enhancements.enhancement1:
17+
.. _whatsnew_210.enhancements.cow:
1818

19-
enhancement1
20-
^^^^^^^^^^^^
19+
Copy-on-Write improvements
20+
^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
22+
- Setting a :class:`Series` into a :class:`DataFrame` now creates a lazy instead of a deep copy (:issue:`53142`)
2123

2224
.. _whatsnew_210.enhancements.enhancement2:
2325

pandas/core/frame.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -3915,7 +3915,6 @@ def isetitem(self, loc, value) -> None:
39153915
In cases where ``frame.columns`` is unique, this is equivalent to
39163916
``frame[frame.columns[i]] = value``.
39173917
"""
3918-
using_cow = using_copy_on_write()
39193918
if isinstance(value, DataFrame):
39203919
if is_integer(loc):
39213920
loc = [loc]
@@ -3927,13 +3926,11 @@ def isetitem(self, loc, value) -> None:
39273926
)
39283927

39293928
for i, idx in enumerate(loc):
3930-
arraylike, refs = self._sanitize_column(
3931-
value.iloc[:, i], using_cow=using_cow
3932-
)
3929+
arraylike, refs = self._sanitize_column(value.iloc[:, i])
39333930
self._iset_item_mgr(idx, arraylike, inplace=False, refs=refs)
39343931
return
39353932

3936-
arraylike, refs = self._sanitize_column(value, using_cow=using_cow)
3933+
arraylike, refs = self._sanitize_column(value)
39373934
self._iset_item_mgr(loc, arraylike, inplace=False, refs=refs)
39383935

39393936
def __setitem__(self, key, value):
@@ -4170,7 +4167,7 @@ def _set_item(self, key, value) -> None:
41704167
Series/TimeSeries will be conformed to the DataFrames index to
41714168
ensure homogeneity.
41724169
"""
4173-
value, refs = self._sanitize_column(value, using_cow=using_copy_on_write())
4170+
value, refs = self._sanitize_column(value)
41744171

41754172
if (
41764173
key in self.columns
@@ -4813,7 +4810,7 @@ def insert(
48134810
elif isinstance(value, DataFrame):
48144811
value = value.iloc[:, 0]
48154812

4816-
value, refs = self._sanitize_column(value, using_cow=using_copy_on_write())
4813+
value, refs = self._sanitize_column(value)
48174814
self._mgr.insert(loc, column, value, refs=refs)
48184815

48194816
def assign(self, **kwargs) -> DataFrame:
@@ -4884,9 +4881,7 @@ def assign(self, **kwargs) -> DataFrame:
48844881
data[k] = com.apply_if_callable(v, data)
48854882
return data
48864883

4887-
def _sanitize_column(
4888-
self, value, using_cow: bool = False
4889-
) -> tuple[ArrayLike, BlockValuesRefs | None]:
4884+
def _sanitize_column(self, value) -> tuple[ArrayLike, BlockValuesRefs | None]:
48904885
"""
48914886
Ensures new columns (which go into the BlockManager as new blocks) are
48924887
always copied (or a reference is being tracked to them under CoW)
@@ -4907,7 +4902,7 @@ def _sanitize_column(
49074902
if is_dict_like(value):
49084903
if not isinstance(value, Series):
49094904
value = Series(value)
4910-
return _reindex_for_setitem(value, self.index, using_cow=using_cow)
4905+
return _reindex_for_setitem(value, self.index)
49114906

49124907
if is_list_like(value):
49134908
com.require_length_match(value, self.index)
@@ -11923,12 +11918,12 @@ def _from_nested_dict(data) -> collections.defaultdict:
1192311918

1192411919

1192511920
def _reindex_for_setitem(
11926-
value: DataFrame | Series, index: Index, using_cow: bool = False
11921+
value: DataFrame | Series, index: Index
1192711922
) -> tuple[ArrayLike, BlockValuesRefs | None]:
1192811923
# reindex if necessary
1192911924

1193011925
if value.index.equals(index) or not len(index):
11931-
if using_cow and isinstance(value, Series):
11926+
if using_copy_on_write() and isinstance(value, Series):
1193211927
return value._values, value._references
1193311928
return value._values.copy(), None
1193411929

0 commit comments

Comments
 (0)