forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_methods.py
300 lines (245 loc) · 10.5 KB
/
test_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import numpy as np
import pytest
from pandas import (
DataFrame,
Series,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array
def test_copy(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_copy = df.copy()
# the deep copy doesn't share memory
assert not np.shares_memory(get_array(df_copy, "a"), get_array(df, "a"))
if using_copy_on_write:
assert df_copy._mgr.refs is None
# mutating copy doesn't mutate original
df_copy.iloc[0, 0] = 0
assert df.iloc[0, 0] == 1
def test_copy_shallow(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_copy = df.copy(deep=False)
# the shallow copy still shares memory
assert np.shares_memory(get_array(df_copy, "a"), get_array(df, "a"))
if using_copy_on_write:
assert df_copy._mgr.refs is not None
if using_copy_on_write:
# mutating shallow copy doesn't mutate original
df_copy.iloc[0, 0] = 0
assert df.iloc[0, 0] == 1
# mutating triggered a copy-on-write -> no longer shares memory
assert not np.shares_memory(get_array(df_copy, "a"), get_array(df, "a"))
# but still shares memory for the other columns/blocks
assert np.shares_memory(get_array(df_copy, "c"), get_array(df, "c"))
else:
# mutating shallow copy does mutate original
df_copy.iloc[0, 0] = 0
assert df.iloc[0, 0] == 0
# and still shares memory
assert np.shares_memory(get_array(df_copy, "a"), get_array(df, "a"))
# -----------------------------------------------------------------------------
# DataFrame methods returning new DataFrame using shallow copy
def test_reset_index(using_copy_on_write):
# Case: resetting the index (i.e. adding a new column) + mutating the
# resulting dataframe
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=[10, 11, 12]
)
df_orig = df.copy()
df2 = df.reset_index()
df2._mgr._verify_integrity()
if using_copy_on_write:
# still shares memory (df2 is a shallow copy)
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
# mutating df2 triggers a copy-on-write for that column / block
df2.iloc[0, 2] = 0
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
tm.assert_frame_equal(df, df_orig)
def test_rename_columns(using_copy_on_write):
# Case: renaming columns returns a new dataframe
# + afterwards modifying the result
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = df.rename(columns=str.upper)
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "A"), get_array(df, "a"))
df2.iloc[0, 0] = 0
assert not np.shares_memory(get_array(df2, "A"), get_array(df, "a"))
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "C"), get_array(df, "c"))
expected = DataFrame({"A": [0, 2, 3], "B": [4, 5, 6], "C": [0.1, 0.2, 0.3]})
tm.assert_frame_equal(df2, expected)
tm.assert_frame_equal(df, df_orig)
def test_rename_columns_modify_parent(using_copy_on_write):
# Case: renaming columns returns a new dataframe
# + afterwards modifying the original (parent) dataframe
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df2 = df.rename(columns=str.upper)
df2_orig = df2.copy()
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "A"), get_array(df, "a"))
else:
assert not np.shares_memory(get_array(df2, "A"), get_array(df, "a"))
df.iloc[0, 0] = 0
assert not np.shares_memory(get_array(df2, "A"), get_array(df, "a"))
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "C"), get_array(df, "c"))
expected = DataFrame({"a": [0, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
tm.assert_frame_equal(df, expected)
tm.assert_frame_equal(df2, df2_orig)
def test_reindex_columns(using_copy_on_write):
# Case: reindexing the column returns a new dataframe
# + afterwards modifying the result
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = df.reindex(columns=["a", "c"])
if using_copy_on_write:
# still shares memory (df2 is a shallow copy)
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
# mutating df2 triggers a copy-on-write for that column
df2.iloc[0, 0] = 0
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
tm.assert_frame_equal(df, df_orig)
def test_drop_on_column(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = df.drop(columns="a")
df2._mgr._verify_integrity()
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
else:
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
df2.iloc[0, 0] = 0
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
tm.assert_frame_equal(df, df_orig)
def test_select_dtypes(using_copy_on_write):
# Case: selecting columns using `select_dtypes()` returns a new dataframe
# + afterwards modifying the result
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = df.select_dtypes("int64")
df2._mgr._verify_integrity()
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
# mutating df2 triggers a copy-on-write for that column/block
df2.iloc[0, 0] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)
def test_to_frame(using_copy_on_write):
# Case: converting a Series to a DataFrame with to_frame
ser = Series([1, 2, 3])
ser_orig = ser.copy()
df = ser[:].to_frame()
# currently this always returns a "view"
assert np.shares_memory(ser.values, get_array(df, 0))
df.iloc[0, 0] = 0
if using_copy_on_write:
# mutating df triggers a copy-on-write for that column
assert not np.shares_memory(ser.values, get_array(df, 0))
tm.assert_series_equal(ser, ser_orig)
else:
# but currently select_dtypes() actually returns a view -> mutates parent
expected = ser_orig.copy()
expected.iloc[0] = 0
tm.assert_series_equal(ser, expected)
# modify original series -> don't modify dataframe
df = ser[:].to_frame()
ser.iloc[0] = 0
if using_copy_on_write:
tm.assert_frame_equal(df, ser_orig.to_frame())
else:
expected = ser_orig.copy().to_frame()
expected.iloc[0, 0] = 0
tm.assert_frame_equal(df, expected)
@pytest.mark.parametrize(
"method, idx",
[
(lambda df: df.copy(deep=False).copy(deep=False), 0),
(lambda df: df.reset_index().reset_index(), 2),
(lambda df: df.rename(columns=str.upper).rename(columns=str.lower), 0),
(lambda df: df.copy(deep=False).select_dtypes(include="number"), 0),
],
ids=["shallow-copy", "reset_index", "rename", "select_dtypes"],
)
def test_chained_methods(request, method, idx, using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
# when not using CoW, only the copy() variant actually gives a view
df2_is_view = not using_copy_on_write and request.node.callspec.id == "shallow-copy"
# modify df2 -> don't modify df
df2 = method(df)
df2.iloc[0, idx] = 0
if not df2_is_view:
tm.assert_frame_equal(df, df_orig)
# modify df -> don't modify df2
df2 = method(df)
df.iloc[0, 0] = 0
if not df2_is_view:
tm.assert_frame_equal(df2.iloc[:, idx:], df_orig)
def test_set_index(using_copy_on_write):
# GH 49473
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = df.set_index("a")
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
else:
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
# mutating df2 triggers a copy-on-write for that column / block
df2.iloc[0, 1] = 0
assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
tm.assert_frame_equal(df, df_orig)
@pytest.mark.parametrize(
"method",
[
lambda df: df.head(),
lambda df: df.head(2),
lambda df: df.tail(),
lambda df: df.tail(3),
],
)
def test_head_tail(method, using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = method(df)
df2._mgr._verify_integrity()
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
# modify df2 to trigger CoW for that block
df2.iloc[0, 0] = 0
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
# without CoW enabled, head and tail return views. Mutating df2 also mutates df.
df2.iloc[0, 0] = 1
tm.assert_frame_equal(df, df_orig)
def test_assign(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3]})
df_orig = df.copy()
df2 = df.assign()
df2._mgr._verify_integrity()
if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
# modify df2 to trigger CoW for that block
df2.iloc[0, 0] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)