forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_functions.py
282 lines (229 loc) · 10.6 KB
/
test_functions.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
import numpy as np
import pytest
from pandas import (
DataFrame,
Series,
concat,
merge,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array
def test_concat_frames(using_copy_on_write):
df = DataFrame({"b": ["a"] * 3})
df2 = DataFrame({"a": ["a"] * 3})
df_orig = df.copy()
result = concat([df, df2], axis=1)
if using_copy_on_write:
assert np.shares_memory(get_array(result, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
else:
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b"))
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
result.iloc[0, 0] = "d"
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
result.iloc[0, 1] = "d"
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
tm.assert_frame_equal(df, df_orig)
def test_concat_frames_updating_input(using_copy_on_write):
df = DataFrame({"b": ["a"] * 3})
df2 = DataFrame({"a": ["a"] * 3})
result = concat([df, df2], axis=1)
if using_copy_on_write:
assert np.shares_memory(get_array(result, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
else:
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b"))
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
expected = result.copy()
df.iloc[0, 0] = "d"
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
df2.iloc[0, 0] = "d"
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
tm.assert_frame_equal(result, expected)
def test_concat_series(using_copy_on_write):
ser = Series([1, 2], name="a")
ser2 = Series([3, 4], name="b")
ser_orig = ser.copy()
ser2_orig = ser2.copy()
result = concat([ser, ser2], axis=1)
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), ser.values)
assert np.shares_memory(get_array(result, "b"), ser2.values)
else:
assert not np.shares_memory(get_array(result, "a"), ser.values)
assert not np.shares_memory(get_array(result, "b"), ser2.values)
result.iloc[0, 0] = 100
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), ser.values)
assert np.shares_memory(get_array(result, "b"), ser2.values)
result.iloc[0, 1] = 1000
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "b"), ser2.values)
tm.assert_series_equal(ser, ser_orig)
tm.assert_series_equal(ser2, ser2_orig)
def test_concat_frames_chained(using_copy_on_write):
df1 = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
df2 = DataFrame({"c": [4, 5, 6]})
df3 = DataFrame({"d": [4, 5, 6]})
result = concat([concat([df1, df2], axis=1), df3], axis=1)
expected = result.copy()
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert np.shares_memory(get_array(result, "c"), get_array(df2, "c"))
assert np.shares_memory(get_array(result, "d"), get_array(df3, "d"))
else:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert not np.shares_memory(get_array(result, "c"), get_array(df2, "c"))
assert not np.shares_memory(get_array(result, "d"), get_array(df3, "d"))
df1.iloc[0, 0] = 100
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
tm.assert_frame_equal(result, expected)
def test_concat_series_chained(using_copy_on_write):
ser1 = Series([1, 2, 3], name="a")
ser2 = Series([4, 5, 6], name="c")
ser3 = Series([4, 5, 6], name="d")
result = concat([concat([ser1, ser2], axis=1), ser3], axis=1)
expected = result.copy()
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(ser1, "a"))
assert np.shares_memory(get_array(result, "c"), get_array(ser2, "c"))
assert np.shares_memory(get_array(result, "d"), get_array(ser3, "d"))
else:
assert not np.shares_memory(get_array(result, "a"), get_array(ser1, "a"))
assert not np.shares_memory(get_array(result, "c"), get_array(ser2, "c"))
assert not np.shares_memory(get_array(result, "d"), get_array(ser3, "d"))
ser1.iloc[0] = 100
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(ser1, "a"))
tm.assert_frame_equal(result, expected)
def test_concat_series_updating_input(using_copy_on_write):
ser = Series([1, 2], name="a")
ser2 = Series([3, 4], name="b")
expected = DataFrame({"a": [1, 2], "b": [3, 4]})
result = concat([ser, ser2], axis=1)
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(ser, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(ser2, "b"))
else:
assert not np.shares_memory(get_array(result, "a"), get_array(ser, "a"))
assert not np.shares_memory(get_array(result, "b"), get_array(ser2, "b"))
ser.iloc[0] = 100
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(ser, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(ser2, "b"))
tm.assert_frame_equal(result, expected)
ser2.iloc[0] = 1000
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "b"), get_array(ser2, "b"))
tm.assert_frame_equal(result, expected)
def test_concat_mixed_series_frame(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "c": 1})
ser = Series([4, 5, 6], name="d")
result = concat([df, ser], axis=1)
expected = result.copy()
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(df, "a"))
assert np.shares_memory(get_array(result, "c"), get_array(df, "c"))
assert np.shares_memory(get_array(result, "d"), get_array(ser, "d"))
else:
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a"))
assert not np.shares_memory(get_array(result, "c"), get_array(df, "c"))
assert not np.shares_memory(get_array(result, "d"), get_array(ser, "d"))
ser.iloc[0] = 100
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "d"), get_array(ser, "d"))
df.iloc[0, 0] = 100
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a"))
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize(
"func",
[
lambda df1, df2, **kwargs: df1.merge(df2, **kwargs),
lambda df1, df2, **kwargs: merge(df1, df2, **kwargs),
],
)
def test_merge_on_key(using_copy_on_write, func):
df1 = DataFrame({"key": ["a", "b", "c"], "a": [1, 2, 3]})
df2 = DataFrame({"key": ["a", "b", "c"], "b": [4, 5, 6]})
df1_orig = df1.copy()
df2_orig = df2.copy()
result = func(df1, df2, on="key")
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
assert np.shares_memory(get_array(result, "key"), get_array(df1, "key"))
assert not np.shares_memory(get_array(result, "key"), get_array(df2, "key"))
else:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
result.iloc[0, 1] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
result.iloc[0, 2] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
tm.assert_frame_equal(df1, df1_orig)
tm.assert_frame_equal(df2, df2_orig)
def test_merge_on_index(using_copy_on_write):
df1 = DataFrame({"a": [1, 2, 3]})
df2 = DataFrame({"b": [4, 5, 6]})
df1_orig = df1.copy()
df2_orig = df2.copy()
result = merge(df1, df2, left_index=True, right_index=True)
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
else:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
result.iloc[0, 0] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
result.iloc[0, 1] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
tm.assert_frame_equal(df1, df1_orig)
tm.assert_frame_equal(df2, df2_orig)
@pytest.mark.parametrize(
"func, how",
[
(lambda df1, df2, **kwargs: merge(df2, df1, on="key", **kwargs), "right"),
(lambda df1, df2, **kwargs: merge(df1, df2, on="key", **kwargs), "left"),
],
)
def test_merge_on_key_enlarging_one(using_copy_on_write, func, how):
df1 = DataFrame({"key": ["a", "b", "c"], "a": [1, 2, 3]})
df2 = DataFrame({"key": ["a", "b"], "b": [4, 5]})
df1_orig = df1.copy()
df2_orig = df2.copy()
result = func(df1, df2, how=how)
if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
assert df2._mgr._has_no_reference(1)
assert df2._mgr._has_no_reference(0)
assert np.shares_memory(get_array(result, "key"), get_array(df1, "key")) is (
how == "left"
)
assert not np.shares_memory(get_array(result, "key"), get_array(df2, "key"))
else:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b"))
if how == "left":
result.iloc[0, 1] = 0
else:
result.iloc[0, 2] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
tm.assert_frame_equal(df1, df1_orig)
tm.assert_frame_equal(df2, df2_orig)