Skip to content

CoW: Remove more using_copy_on_write fixtures #57368

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 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 10 additions & 17 deletions pandas/tests/copy_view/index/test_datetimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,43 @@


@pytest.mark.parametrize("box", [lambda x: x, DatetimeIndex])
def test_datetimeindex(using_copy_on_write, box):
def test_datetimeindex(box):
dt = date_range("2019-12-31", periods=3, freq="D")
ser = Series(dt)
idx = box(DatetimeIndex(ser))
expected = idx.copy(deep=True)
ser.iloc[0] = Timestamp("2020-12-31")
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
tm.assert_index_equal(idx, expected)


def test_datetimeindex_tz_convert(using_copy_on_write):
def test_datetimeindex_tz_convert():
dt = date_range("2019-12-31", periods=3, freq="D", tz="Europe/Berlin")
ser = Series(dt)
idx = DatetimeIndex(ser).tz_convert("US/Eastern")
expected = idx.copy(deep=True)
ser.iloc[0] = Timestamp("2020-12-31", tz="Europe/Berlin")
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
tm.assert_index_equal(idx, expected)


def test_datetimeindex_tz_localize(using_copy_on_write):
def test_datetimeindex_tz_localize():
dt = date_range("2019-12-31", periods=3, freq="D")
ser = Series(dt)
idx = DatetimeIndex(ser).tz_localize("Europe/Berlin")
expected = idx.copy(deep=True)
ser.iloc[0] = Timestamp("2020-12-31")
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
tm.assert_index_equal(idx, expected)


def test_datetimeindex_isocalendar(using_copy_on_write):
def test_datetimeindex_isocalendar():
dt = date_range("2019-12-31", periods=3, freq="D")
ser = Series(dt)
df = DatetimeIndex(ser).isocalendar()
expected = df.index.copy(deep=True)
ser.iloc[0] = Timestamp("2020-12-31")
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
tm.assert_index_equal(df.index, expected)


def test_index_values(using_copy_on_write):
def test_index_values():
idx = date_range("2019-12-31", periods=3, freq="D")
result = idx.values
if using_copy_on_write:
assert result.flags.writeable is False
else:
assert result.flags.writeable is True
assert result.flags.writeable is False
72 changes: 23 additions & 49 deletions pandas/tests/copy_view/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ def index_view(index_data):
return idx, view


def test_set_index_update_column(using_copy_on_write):
def test_set_index_update_column():
df = DataFrame({"a": [1, 2], "b": 1})
df = df.set_index("a", drop=False)
expected = df.index.copy(deep=True)
df.iloc[0, 0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
tm.assert_index_equal(df.index, Index([100, 2], name="a"))
tm.assert_index_equal(df.index, expected)


def test_set_index_drop_update_column(using_copy_on_write):
def test_set_index_drop_update_column():
df = DataFrame({"a": [1, 2], "b": 1.5})
view = df[:]
df = df.set_index("a", drop=True)
Expand All @@ -39,73 +36,58 @@ def test_set_index_drop_update_column(using_copy_on_write):
tm.assert_index_equal(df.index, expected)


def test_set_index_series(using_copy_on_write):
def test_set_index_series():
df = DataFrame({"a": [1, 2], "b": 1.5})
ser = Series([10, 11])
df = df.set_index(ser)
expected = df.index.copy(deep=True)
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
tm.assert_index_equal(df.index, Index([100, 11]))
tm.assert_index_equal(df.index, expected)


def test_assign_index_as_series(using_copy_on_write):
def test_assign_index_as_series():
df = DataFrame({"a": [1, 2], "b": 1.5})
ser = Series([10, 11])
df.index = ser
expected = df.index.copy(deep=True)
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
tm.assert_index_equal(df.index, Index([100, 11]))
tm.assert_index_equal(df.index, expected)


def test_assign_index_as_index(using_copy_on_write):
def test_assign_index_as_index():
df = DataFrame({"a": [1, 2], "b": 1.5})
ser = Series([10, 11])
rhs_index = Index(ser)
df.index = rhs_index
rhs_index = None # overwrite to clear reference
expected = df.index.copy(deep=True)
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
tm.assert_index_equal(df.index, Index([100, 11]))
tm.assert_index_equal(df.index, expected)


def test_index_from_series(using_copy_on_write):
def test_index_from_series():
ser = Series([1, 2])
idx = Index(ser)
expected = idx.copy(deep=True)
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
else:
tm.assert_index_equal(idx, Index([100, 2]))
tm.assert_index_equal(idx, expected)


def test_index_from_series_copy(using_copy_on_write):
def test_index_from_series_copy():
ser = Series([1, 2])
idx = Index(ser, copy=True) # noqa: F841
arr = get_array(ser)
ser.iloc[0] = 100
assert np.shares_memory(get_array(ser), arr)


def test_index_from_index(using_copy_on_write):
def test_index_from_index():
ser = Series([1, 2])
idx = Index(ser)
idx = Index(idx)
expected = idx.copy(deep=True)
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
else:
tm.assert_index_equal(idx, Index([100, 2]))
tm.assert_index_equal(idx, expected)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -135,44 +117,36 @@ def test_index_from_index(using_copy_on_write):
"astype",
],
)
def test_index_ops(using_copy_on_write, func, request):
def test_index_ops(func, request):
idx, view_ = index_view([1, 2])
expected = idx.copy(deep=True)
if "astype" in request.node.callspec.id:
expected = expected.astype("Int64")
idx = func(idx)
view_.iloc[0, 0] = 100
if using_copy_on_write:
tm.assert_index_equal(idx, expected, check_names=False)
tm.assert_index_equal(idx, expected, check_names=False)


def test_infer_objects(using_copy_on_write):
def test_infer_objects():
idx, view_ = index_view(["a", "b"])
expected = idx.copy(deep=True)
idx = idx.infer_objects(copy=False)
view_.iloc[0, 0] = "aaaa"
if using_copy_on_write:
tm.assert_index_equal(idx, expected, check_names=False)
tm.assert_index_equal(idx, expected, check_names=False)


def test_index_to_frame(using_copy_on_write):
def test_index_to_frame():
idx = Index([1, 2, 3], name="a")
expected = idx.copy(deep=True)
df = idx.to_frame()
if using_copy_on_write:
assert np.shares_memory(get_array(df, "a"), idx._values)
assert not df._mgr._has_no_reference(0)
else:
assert not np.shares_memory(get_array(df, "a"), idx._values)
assert np.shares_memory(get_array(df, "a"), idx._values)
assert not df._mgr._has_no_reference(0)

df.iloc[0, 0] = 100
tm.assert_index_equal(idx, expected)


def test_index_values(using_copy_on_write):
def test_index_values():
idx = Index([1, 2, 3])
result = idx.values
if using_copy_on_write:
assert result.flags.writeable is False
else:
assert result.flags.writeable is True
assert result.flags.writeable is False
5 changes: 2 additions & 3 deletions pandas/tests/copy_view/index/test_periodindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@


@pytest.mark.parametrize("box", [lambda x: x, PeriodIndex])
def test_periodindex(using_copy_on_write, box):
def test_periodindex(box):
dt = period_range("2019-12-31", periods=3, freq="D")
ser = Series(dt)
idx = box(PeriodIndex(ser))
expected = idx.copy(deep=True)
ser.iloc[0] = Period("2020-12-31")
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
tm.assert_index_equal(idx, expected)
5 changes: 2 additions & 3 deletions pandas/tests/copy_view/index/test_timedeltaindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
lambda x: TimedeltaIndex(TimedeltaIndex(x)),
],
)
def test_timedeltaindex(using_copy_on_write, cons):
def test_timedeltaindex(cons):
dt = timedelta_range("1 day", periods=3)
ser = Series(dt)
idx = cons(ser)
expected = idx.copy(deep=True)
ser.iloc[0] = Timedelta("5 days")
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
tm.assert_index_equal(idx, expected)
Loading