forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_functions.py
317 lines (235 loc) · 11.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import numpy as np
import pytest
from pandas._config import using_string_dtype
from pandas.compat import HAS_PYARROW
from pandas import (
DataFrame,
Index,
Series,
concat,
merge,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array
def test_concat_frames():
df = DataFrame({"b": ["a"] * 3}, dtype=object)
df2 = DataFrame({"a": ["a"] * 3}, dtype=object)
df_orig = df.copy()
result = concat([df, df2], axis=1)
assert 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, 0] = "d"
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"
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():
df = DataFrame({"b": ["a"] * 3}, dtype=object)
df2 = DataFrame({"a": ["a"] * 3}, dtype=object)
result = concat([df, df2], axis=1)
assert np.shares_memory(get_array(result, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
expected = result.copy()
df.iloc[0, 0] = "d"
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"
assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a"))
tm.assert_frame_equal(result, expected)
def test_concat_series():
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)
assert np.shares_memory(get_array(result, "a"), ser.values)
assert np.shares_memory(get_array(result, "b"), ser2.values)
result.iloc[0, 0] = 100
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
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():
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()
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"))
df1.iloc[0, 0] = 100
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
tm.assert_frame_equal(result, expected)
def test_concat_series_chained():
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()
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"))
ser1.iloc[0] = 100
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():
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)
assert np.shares_memory(get_array(result, "a"), get_array(ser, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(ser2, "b"))
ser.iloc[0] = 100
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
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():
df = DataFrame({"a": [1, 2, 3], "c": 1})
ser = Series([4, 5, 6], name="d")
result = concat([df, ser], axis=1)
expected = result.copy()
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"))
ser.iloc[0] = 100
assert not np.shares_memory(get_array(result, "d"), get_array(ser, "d"))
df.iloc[0, 0] = 100
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a"))
tm.assert_frame_equal(result, expected)
def test_concat_copy_keyword():
df = DataFrame({"a": [1, 2]})
df2 = DataFrame({"b": [1.5, 2.5]})
result = concat([df, df2], axis=1)
assert np.shares_memory(get_array(df, "a"), get_array(result, "a"))
assert np.shares_memory(get_array(df2, "b"), get_array(result, "b"))
@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(func):
df1 = DataFrame({"key": Series(["a", "b", "c"], dtype=object), "a": [1, 2, 3]})
df2 = DataFrame({"key": Series(["a", "b", "c"], dtype=object), "b": [4, 5, 6]})
df1_orig = df1.copy()
df2_orig = df2.copy()
result = func(df1, df2, on="key")
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"))
result.iloc[0, 1] = 0
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
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():
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)
assert 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, 0] = 0
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
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(func, how):
df1 = DataFrame({"key": Series(["a", "b", "c"], dtype=object), "a": [1, 2, 3]})
df2 = DataFrame({"key": Series(["a", "b"], dtype=object), "b": [4, 5]})
df1_orig = df1.copy()
df2_orig = df2.copy()
result = func(df1, df2, how=how)
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"))
if how == "left":
result.iloc[0, 1] = 0
else:
result.iloc[0, 2] = 0
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)
def test_merge_copy_keyword():
df = DataFrame({"a": [1, 2]})
df2 = DataFrame({"b": [3, 4.5]})
result = df.merge(df2, left_index=True, right_index=True)
assert np.shares_memory(get_array(df, "a"), get_array(result, "a"))
assert np.shares_memory(get_array(df2, "b"), get_array(result, "b"))
@pytest.mark.xfail(
using_string_dtype() and HAS_PYARROW,
reason="TODO(infer_string); result.index infers str dtype while both "
"df1 and df2 index are object.",
)
def test_join_on_key():
df_index = Index(["a", "b", "c"], name="key", dtype=object)
df1 = DataFrame({"a": [1, 2, 3]}, index=df_index.copy(deep=True))
df2 = DataFrame({"b": [4, 5, 6]}, index=df_index.copy(deep=True))
df1_orig = df1.copy()
df2_orig = df2.copy()
result = df1.join(df2, on="key")
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.index), get_array(df1.index))
assert not np.shares_memory(get_array(result.index), get_array(df2.index))
result.iloc[0, 0] = 0
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
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_join_multiple_dataframes_on_key():
df_index = Index(["a", "b", "c"], name="key", dtype=object)
df1 = DataFrame({"a": [1, 2, 3]}, index=df_index.copy(deep=True))
dfs_list = [
DataFrame({"b": [4, 5, 6]}, index=df_index.copy(deep=True)),
DataFrame({"c": [7, 8, 9]}, index=df_index.copy(deep=True)),
]
df1_orig = df1.copy()
dfs_list_orig = [df.copy() for df in dfs_list]
result = df1.join(dfs_list)
assert np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(dfs_list[0], "b"))
assert np.shares_memory(get_array(result, "c"), get_array(dfs_list[1], "c"))
assert np.shares_memory(get_array(result.index), get_array(df1.index))
assert not np.shares_memory(get_array(result.index), get_array(dfs_list[0].index))
assert not np.shares_memory(get_array(result.index), get_array(dfs_list[1].index))
result.iloc[0, 0] = 0
assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a"))
assert np.shares_memory(get_array(result, "b"), get_array(dfs_list[0], "b"))
assert np.shares_memory(get_array(result, "c"), get_array(dfs_list[1], "c"))
result.iloc[0, 1] = 0
assert not np.shares_memory(get_array(result, "b"), get_array(dfs_list[0], "b"))
assert np.shares_memory(get_array(result, "c"), get_array(dfs_list[1], "c"))
result.iloc[0, 2] = 0
assert not np.shares_memory(get_array(result, "c"), get_array(dfs_list[1], "c"))
tm.assert_frame_equal(df1, df1_orig)
for df, df_orig in zip(dfs_list, dfs_list_orig):
tm.assert_frame_equal(df, df_orig)