Skip to content

Commit d1b7244

Browse files
TST: test explicit copy keyword with CoW enabled (pandas-dev#50536)
1 parent 185c53f commit d1b7244

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

pandas/tests/copy_view/test_methods.py

+73
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Series,
1010
Timestamp,
1111
date_range,
12+
period_range,
1213
)
1314
import pandas._testing as tm
1415
from pandas.tests.copy_view.util import get_array
@@ -53,6 +54,78 @@ def test_copy_shallow(using_copy_on_write):
5354
assert np.shares_memory(get_array(df_copy, "a"), get_array(df, "a"))
5455

5556

57+
@pytest.mark.parametrize("copy", [True, None, False])
58+
@pytest.mark.parametrize(
59+
"method",
60+
[
61+
lambda df, copy: df.rename(columns=str.lower, copy=copy),
62+
lambda df, copy: df.reindex(columns=["a", "c"], copy=copy),
63+
lambda df, copy: df.reindex_like(df, copy=copy),
64+
lambda df, copy: df.set_axis(["a", "b", "c"], axis="index", copy=copy),
65+
lambda df, copy: df.rename_axis(index="test", copy=copy),
66+
lambda df, copy: df.rename_axis(columns="test", copy=copy),
67+
# lambda df, copy: df.astype({'b': 'int64'}, copy=copy),
68+
# lambda df, copy: df.swaplevel(0, 0, copy=copy),
69+
lambda df, copy: df.swapaxes(0, 0, copy=copy),
70+
lambda df, copy: df.truncate(0, 5, copy=copy),
71+
# lambda df, copy: df.infer_objects(copy=copy)
72+
lambda df, copy: df.to_timestamp(copy=copy),
73+
lambda df, copy: df.to_period(freq="D", copy=copy),
74+
lambda df, copy: df.tz_localize("US/Central", copy=copy),
75+
lambda df, copy: df.tz_convert("US/Central", copy=copy),
76+
lambda df, copy: df.set_flags(allows_duplicate_labels=False, copy=copy),
77+
],
78+
ids=[
79+
"rename",
80+
"reindex",
81+
"reindex_like",
82+
"set_axis",
83+
"rename_axis0",
84+
"rename_axis1",
85+
# "astype", # CoW not yet implemented
86+
# "swaplevel", # only series
87+
"swapaxes",
88+
"truncate",
89+
# "infer_objects", # CoW not yet implemented
90+
"to_timestamp",
91+
"to_period",
92+
"tz_localize",
93+
"tz_convert",
94+
"set_flags",
95+
],
96+
)
97+
def test_methods_copy_keyword(
98+
request, method, copy, using_copy_on_write, using_array_manager
99+
):
100+
index = None
101+
if "to_timestamp" in request.node.callspec.id:
102+
index = period_range("2012-01-01", freq="D", periods=3)
103+
elif "to_period" in request.node.callspec.id:
104+
index = date_range("2012-01-01", freq="D", periods=3)
105+
elif "tz_localize" in request.node.callspec.id:
106+
index = date_range("2012-01-01", freq="D", periods=3)
107+
elif "tz_convert" in request.node.callspec.id:
108+
index = date_range("2012-01-01", freq="D", periods=3, tz="Europe/Brussels")
109+
110+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=index)
111+
df2 = method(df, copy=copy)
112+
113+
share_memory = (using_copy_on_write and copy is not True) or copy is False
114+
115+
if request.node.callspec.id.startswith("reindex-"):
116+
# TODO copy=False without CoW still returns a copy in this case
117+
if not using_copy_on_write and not using_array_manager and copy is False:
118+
share_memory = False
119+
# TODO copy=True with CoW still returns a view
120+
if using_copy_on_write:
121+
share_memory = True
122+
123+
if share_memory:
124+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
125+
else:
126+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
127+
128+
56129
# -----------------------------------------------------------------------------
57130
# DataFrame methods returning new DataFrame using shallow copy
58131

0 commit comments

Comments
 (0)