Skip to content

Commit 9339f15

Browse files
fixup after updata main and column_setitem + iloc inplace setitem changes (pandas-devgh-45333)
1 parent 40f2b24 commit 9339f15

File tree

6 files changed

+15
-43
lines changed

6 files changed

+15
-43
lines changed

pandas/core/frame.py

-13
Original file line numberDiff line numberDiff line change
@@ -3941,19 +3941,6 @@ def _set_value(
39413941
Sets whether or not index/col interpreted as indexers
39423942
"""
39433943
try:
3944-
if (
3945-
get_option("mode.copy_on_write")
3946-
and get_option("mode.data_manager") == "block"
3947-
):
3948-
if not takeable:
3949-
icol = self.columns.get_loc(col)
3950-
index = self.index.get_loc(index)
3951-
self._mgr.column_setitem(icol, index, value)
3952-
else:
3953-
self._mgr.column_setitem(col, index, value)
3954-
self._clear_item_cache()
3955-
return
3956-
39573944
if takeable:
39583945
icol = col
39593946
iindex = cast(int, index)

pandas/tests/copy_view/test_indexing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def test_subset_column_slice(using_copy_on_write, using_array_manager, dtype):
150150
ids=["slice", "mask", "array"],
151151
)
152152
def test_subset_loc_rows_columns(
153-
dtype, row_indexer, column_indexer, using_array_manager
153+
dtype, row_indexer, column_indexer, using_array_manager, using_copy_on_write
154154
):
155155
# Case: taking a subset of the rows+columns of a DataFrame using .loc
156156
# + afterwards modifying the subset
@@ -177,7 +177,7 @@ def test_subset_loc_rows_columns(
177177
if (
178178
isinstance(row_indexer, slice)
179179
and isinstance(column_indexer, slice)
180-
and (using_array_manager or dtype == "int64")
180+
and (using_array_manager or (dtype == "int64" and not using_copy_on_write))
181181
):
182182
df_orig.iloc[1, 1] = 0
183183
tm.assert_frame_equal(df, df_orig)
@@ -197,7 +197,7 @@ def test_subset_loc_rows_columns(
197197
ids=["slice", "mask", "array"],
198198
)
199199
def test_subset_iloc_rows_columns(
200-
dtype, row_indexer, column_indexer, using_array_manager
200+
dtype, row_indexer, column_indexer, using_array_manager, using_copy_on_write
201201
):
202202
# Case: taking a subset of the rows+columns of a DataFrame using .iloc
203203
# + afterwards modifying the subset
@@ -224,7 +224,7 @@ def test_subset_iloc_rows_columns(
224224
if (
225225
isinstance(row_indexer, slice)
226226
and isinstance(column_indexer, slice)
227-
and (using_array_manager or dtype == "int64")
227+
and (using_array_manager or (dtype == "int64" and not using_copy_on_write))
228228
):
229229
df_orig.iloc[1, 1] = 0
230230
tm.assert_frame_equal(df, df_orig)

pandas/tests/copy_view/test_setitem.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ def test_set_column_with_series(using_copy_on_write):
3434
df["c"] = ser
3535

3636
if using_copy_on_write:
37-
# with CoW we can delay the copy
38-
assert np.shares_memory(df["c"].values, ser.values)
37+
# TODO(CoW) with CoW we can delay the copy
38+
# assert np.shares_memory(df["c"].values, ser.values)
39+
assert not np.shares_memory(df["c"].values, ser.values)
3940
else:
4041
# the series data is copied
4142
assert not np.shares_memory(df["c"].values, ser.values)
@@ -78,8 +79,9 @@ def test_set_columns_with_dataframe(using_copy_on_write):
7879
df[["c", "d"]] = df2
7980

8081
if using_copy_on_write:
81-
# with CoW we can delay the copy
82-
assert np.shares_memory(df["c"].values, df2["c"].values)
82+
# TODO(CoW) with CoW we can delay the copy
83+
# assert np.shares_memory(df["c"].values, df2["c"].values)
84+
assert not np.shares_memory(df["c"].values, df2["c"].values)
8385
else:
8486
# the data is copied
8587
assert not np.shares_memory(df["c"].values, df2["c"].values)

pandas/tests/frame/indexing/test_setitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ def test_setitem_string_column_numpy_dtype_raising(self):
776776
expected = DataFrame([[1, 2, 5], [3, 4, 6]], columns=[0, 1, "0 - Name"])
777777
tm.assert_frame_equal(df, expected)
778778

779-
def test_setitem_empty_df_duplicate_columns(self):
779+
def test_setitem_empty_df_duplicate_columns(self, using_copy_on_write):
780780
# GH#38521
781781
df = DataFrame(columns=["a", "b", "b"], dtype="float64")
782782
msg = "will attempt to set the values inplace instead"

pandas/tests/indexing/test_iloc.py

-9
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,12 @@ def test_iloc_setitem_fullcol_categorical(
107107
df.iloc[0, 0] = "gamma"
108108
assert cat[0] != "gamma"
109109

110-
# TODO with mixed dataframe ("split" path), we always overwrite the column
111-
if using_copy_on_write and indexer == tm.loc:
112-
# TODO(ArrayManager) with loc, slice(3) gets converted into slice(0, 3)
113-
# which is then considered as "full" slice and does overwrite. For iloc
114-
# this conversion is not done, and so it doesn't overwrite
115-
overwrite = overwrite or (isinstance(key, slice) and key == slice(3))
116-
117110
frame = DataFrame({0: np.array([0, 1, 2], dtype=object), 1: range(3)})
118111
df = frame.copy()
119112
orig_vals = df.values
120113
with tm.assert_produces_warning(FutureWarning, match=msg):
121114
indexer(df)[key, 0] = cat
122115
expected = DataFrame({0: cat, 1: range(3)})
123-
if using_copy_on_write and not overwrite:
124-
expected[0] = expected[0].astype(object)
125116
tm.assert_frame_equal(df, expected)
126117

127118
@pytest.mark.parametrize("box", [array, Series])

pandas/tests/indexing/test_loc.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -716,34 +716,26 @@ def test_loc_setitem_frame_with_reindex(self):
716716
expected = DataFrame({"A": ser})
717717
tm.assert_frame_equal(df, expected)
718718

719-
def test_loc_setitem_frame_with_reindex_mixed(self, using_copy_on_write):
719+
def test_loc_setitem_frame_with_reindex_mixed(self):
720720
# GH#40480
721721
df = DataFrame(index=[3, 5, 4], columns=["A", "B"], dtype=float)
722722
df["B"] = "string"
723723
msg = "will attempt to set the values inplace instead"
724724
with tm.assert_produces_warning(FutureWarning, match=msg):
725725
df.loc[[4, 3, 5], "A"] = np.array([1, 2, 3], dtype="int64")
726-
ser = Series([2, 3, 1], index=[3, 5, 4], dtype=float)
727-
if not using_copy_on_write:
728-
# For default BlockManager case, this takes the "split" path,
729-
# which still overwrites the column
730-
ser = Series([2, 3, 1], index=[3, 5, 4], dtype="int64")
726+
ser = Series([2, 3, 1], index=[3, 5, 4], dtype="int64")
731727
expected = DataFrame({"A": ser})
732728
expected["B"] = "string"
733729
tm.assert_frame_equal(df, expected)
734730

735-
def test_loc_setitem_frame_with_inverted_slice(self, using_copy_on_write):
731+
def test_loc_setitem_frame_with_inverted_slice(self):
736732
# GH#40480
737733
df = DataFrame(index=[1, 2, 3], columns=["A", "B"], dtype=float)
738734
df["B"] = "string"
739735
msg = "will attempt to set the values inplace instead"
740736
with tm.assert_produces_warning(FutureWarning, match=msg):
741737
df.loc[slice(3, 0, -1), "A"] = np.array([1, 2, 3], dtype="int64")
742-
expected = DataFrame({"A": [3.0, 2.0, 1.0], "B": "string"}, index=[1, 2, 3])
743-
if not using_copy_on_write:
744-
# For default BlockManager case, this takes the "split" path,
745-
# which still overwrites the column
746-
expected["A"] = expected["A"].astype("int64")
738+
expected = DataFrame({"A": [3, 2, 1], "B": "string"}, index=[1, 2, 3])
747739
tm.assert_frame_equal(df, expected)
748740

749741
def test_loc_setitem_empty_frame(self):

0 commit comments

Comments
 (0)