Skip to content

Commit

Permalink
fixup after updata main and column_setitem + iloc inplace setitem cha…
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Jul 16, 2022
1 parent 40f2b24 commit 9339f15
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 43 deletions.
13 changes: 0 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3941,19 +3941,6 @@ def _set_value(
Sets whether or not index/col interpreted as indexers
"""
try:
if (
get_option("mode.copy_on_write")
and get_option("mode.data_manager") == "block"
):
if not takeable:
icol = self.columns.get_loc(col)
index = self.index.get_loc(index)
self._mgr.column_setitem(icol, index, value)
else:
self._mgr.column_setitem(col, index, value)
self._clear_item_cache()
return

if takeable:
icol = col
iindex = cast(int, index)
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/copy_view/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_subset_column_slice(using_copy_on_write, using_array_manager, dtype):
ids=["slice", "mask", "array"],
)
def test_subset_loc_rows_columns(
dtype, row_indexer, column_indexer, using_array_manager
dtype, row_indexer, column_indexer, using_array_manager, using_copy_on_write
):
# Case: taking a subset of the rows+columns of a DataFrame using .loc
# + afterwards modifying the subset
Expand All @@ -177,7 +177,7 @@ def test_subset_loc_rows_columns(
if (
isinstance(row_indexer, slice)
and isinstance(column_indexer, slice)
and (using_array_manager or dtype == "int64")
and (using_array_manager or (dtype == "int64" and not using_copy_on_write))
):
df_orig.iloc[1, 1] = 0
tm.assert_frame_equal(df, df_orig)
Expand All @@ -197,7 +197,7 @@ def test_subset_loc_rows_columns(
ids=["slice", "mask", "array"],
)
def test_subset_iloc_rows_columns(
dtype, row_indexer, column_indexer, using_array_manager
dtype, row_indexer, column_indexer, using_array_manager, using_copy_on_write
):
# Case: taking a subset of the rows+columns of a DataFrame using .iloc
# + afterwards modifying the subset
Expand All @@ -224,7 +224,7 @@ def test_subset_iloc_rows_columns(
if (
isinstance(row_indexer, slice)
and isinstance(column_indexer, slice)
and (using_array_manager or dtype == "int64")
and (using_array_manager or (dtype == "int64" and not using_copy_on_write))
):
df_orig.iloc[1, 1] = 0
tm.assert_frame_equal(df, df_orig)
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/copy_view/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def test_set_column_with_series(using_copy_on_write):
df["c"] = ser

if using_copy_on_write:
# with CoW we can delay the copy
assert np.shares_memory(df["c"].values, ser.values)
# TODO(CoW) with CoW we can delay the copy
# assert np.shares_memory(df["c"].values, ser.values)
assert not np.shares_memory(df["c"].values, ser.values)
else:
# the series data is copied
assert not np.shares_memory(df["c"].values, ser.values)
Expand Down Expand Up @@ -78,8 +79,9 @@ def test_set_columns_with_dataframe(using_copy_on_write):
df[["c", "d"]] = df2

if using_copy_on_write:
# with CoW we can delay the copy
assert np.shares_memory(df["c"].values, df2["c"].values)
# TODO(CoW) with CoW we can delay the copy
# assert np.shares_memory(df["c"].values, df2["c"].values)
assert not np.shares_memory(df["c"].values, df2["c"].values)
else:
# the data is copied
assert not np.shares_memory(df["c"].values, df2["c"].values)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ def test_setitem_string_column_numpy_dtype_raising(self):
expected = DataFrame([[1, 2, 5], [3, 4, 6]], columns=[0, 1, "0 - Name"])
tm.assert_frame_equal(df, expected)

def test_setitem_empty_df_duplicate_columns(self):
def test_setitem_empty_df_duplicate_columns(self, using_copy_on_write):
# GH#38521
df = DataFrame(columns=["a", "b", "b"], dtype="float64")
msg = "will attempt to set the values inplace instead"
Expand Down
9 changes: 0 additions & 9 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,12 @@ def test_iloc_setitem_fullcol_categorical(
df.iloc[0, 0] = "gamma"
assert cat[0] != "gamma"

# TODO with mixed dataframe ("split" path), we always overwrite the column
if using_copy_on_write and indexer == tm.loc:
# TODO(ArrayManager) with loc, slice(3) gets converted into slice(0, 3)
# which is then considered as "full" slice and does overwrite. For iloc
# this conversion is not done, and so it doesn't overwrite
overwrite = overwrite or (isinstance(key, slice) and key == slice(3))

frame = DataFrame({0: np.array([0, 1, 2], dtype=object), 1: range(3)})
df = frame.copy()
orig_vals = df.values
with tm.assert_produces_warning(FutureWarning, match=msg):
indexer(df)[key, 0] = cat
expected = DataFrame({0: cat, 1: range(3)})
if using_copy_on_write and not overwrite:
expected[0] = expected[0].astype(object)
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("box", [array, Series])
Expand Down
16 changes: 4 additions & 12 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,34 +716,26 @@ def test_loc_setitem_frame_with_reindex(self):
expected = DataFrame({"A": ser})
tm.assert_frame_equal(df, expected)

def test_loc_setitem_frame_with_reindex_mixed(self, using_copy_on_write):
def test_loc_setitem_frame_with_reindex_mixed(self):
# GH#40480
df = DataFrame(index=[3, 5, 4], columns=["A", "B"], dtype=float)
df["B"] = "string"
msg = "will attempt to set the values inplace instead"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.loc[[4, 3, 5], "A"] = np.array([1, 2, 3], dtype="int64")
ser = Series([2, 3, 1], index=[3, 5, 4], dtype=float)
if not using_copy_on_write:
# For default BlockManager case, this takes the "split" path,
# which still overwrites the column
ser = Series([2, 3, 1], index=[3, 5, 4], dtype="int64")
ser = Series([2, 3, 1], index=[3, 5, 4], dtype="int64")
expected = DataFrame({"A": ser})
expected["B"] = "string"
tm.assert_frame_equal(df, expected)

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

def test_loc_setitem_empty_frame(self):
Expand Down

0 comments on commit 9339f15

Please sign in to comment.