Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,3 +1797,11 @@ def using_array_manager():
Fixture to check if the array manager is being used.
"""
return pd.options.mode.data_manager == "array"


@pytest.fixture
def using_copy_on_write():
"""
Fixture to check if Copy-on-Write is enabled.
"""
return False
95 changes: 95 additions & 0 deletions pandas/tests/copy_view/test_setitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import numpy as np

from pandas import (
DataFrame,
Index,
RangeIndex,
Series,
)
import pandas._testing as tm

# -----------------------------------------------------------------------------
# Copy/view behaviour for the values that are set in a DataFrame


def test_set_column_with_array():
# Case: setting an array as a new column (df[col] = arr) copies that data
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
arr = np.array([1, 2, 3])

df["c"] = arr

# the array data is copied
assert not np.shares_memory(df["c"].values, arr)
# and thus modifying the array does not modify the DataFrame
arr[0] = 0
tm.assert_series_equal(df["c"], Series([1, 2, 3], name="c"))


def test_set_column_with_series(using_copy_on_write):
# Case: setting a series as a new column (df[col] = s) copies that data
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
s = Series([1, 2, 3])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick s->ser pls


df["c"] = s

if using_copy_on_write:
# with CoW we can delay the copy
assert np.shares_memory(df["c"].values, s.values)
else:
# the series data is copied
assert not np.shares_memory(df["c"].values, s.values)

# and modifying the series does not modify the DataFrame
s.iloc[0] = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also assert s.iloc[0] == 0, as failure for that to hold would be another way the next assertion could pass

tm.assert_series_equal(df["c"], Series([1, 2, 3], name="c"))


def test_set_column_with_index(using_copy_on_write):
# Case: setting an index as a new column (df[col] = idx) copies that data
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
idx = Index([1, 2, 3])

df["c"] = idx

# the index data is copied
assert not np.shares_memory(df["c"].values, idx.values)

# and thus modifying the index does not modify the DataFrame
idx.values[0] = 0
tm.assert_series_equal(df["c"], Series([1, 2, 3], name="c"))

# however, in case of a RangeIndex, we currently don't copy the cached
# "materialized" values
idx = RangeIndex(1, 4)
arr = idx.values

df["d"] = idx

if using_copy_on_write:
assert not np.shares_memory(df["d"].values, arr)
arr[0] = 0
tm.assert_series_equal(df["d"], Series([1, 2, 3], name="d"))
else:
assert np.shares_memory(df["d"].values, arr)
arr[0] = 0
tm.assert_series_equal(df["d"], Series([0, 2, 3], name="d"))


def test_set_columns_with_dataframe(using_copy_on_write):
# Case: setting a DataFrame as new columns copies that data
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
df2 = DataFrame({"c": [7, 8, 9], "d": [10, 11, 12]})

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)
else:
# the data is copied
assert not np.shares_memory(df["c"].values, df2["c"].values)

# and modifying the set DataFrame does not modify the original DataFrame
df2.iloc[0, 0] = 0
tm.assert_series_equal(df["c"], Series([7, 8, 9], name="c"))