|
1 | 1 | import numpy as np
|
2 | 2 |
|
| 3 | +import pandas.util._test_decorators as td |
| 4 | + |
3 | 5 | from pandas import (
|
4 | 6 | DataFrame,
|
5 | 7 | Series,
|
6 | 8 | concat,
|
| 9 | + merge, |
7 | 10 | )
|
8 | 11 | import pandas._testing as tm
|
9 | 12 | from pandas.tests.copy_view.util import get_array
|
@@ -177,3 +180,63 @@ def test_concat_mixed_series_frame(using_copy_on_write):
|
177 | 180 | if using_copy_on_write:
|
178 | 181 | assert not np.shares_memory(get_array(result, "a"), get_array(df, "a"))
|
179 | 182 | tm.assert_frame_equal(result, expected)
|
| 183 | + |
| 184 | + |
| 185 | +@td.skip_copy_on_write_not_yet_implemented # TODO(CoW) |
| 186 | +def test_merge_on_key(using_copy_on_write): |
| 187 | + df1 = DataFrame({"key": ["a", "b", "c"], "a": [1, 2, 3]}) |
| 188 | + df2 = DataFrame({"key": ["a", "b", "c"], "b": [4, 5, 6]}) |
| 189 | + df1_orig = df1.copy() |
| 190 | + df2_orig = df2.copy() |
| 191 | + |
| 192 | + result = merge(df1, df2, on="key") |
| 193 | + |
| 194 | + if using_copy_on_write: |
| 195 | + assert np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 196 | + assert np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 197 | + assert not np.shares_memory(get_array(result, "key"), get_array(df1, "key")) |
| 198 | + assert not np.shares_memory(get_array(result, "key"), get_array(df2, "key")) |
| 199 | + else: |
| 200 | + assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 201 | + assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 202 | + |
| 203 | + result.iloc[0, 1] = 0 |
| 204 | + if using_copy_on_write: |
| 205 | + assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 206 | + assert np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 207 | + |
| 208 | + result.iloc[0, 2] = 0 |
| 209 | + if using_copy_on_write: |
| 210 | + assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 211 | + tm.assert_frame_equal(df1, df1_orig) |
| 212 | + tm.assert_frame_equal(df2, df2_orig) |
| 213 | + |
| 214 | + |
| 215 | +def test_merge_on_index(using_copy_on_write): |
| 216 | + df1 = DataFrame({"a": [1, 2, 3]}) |
| 217 | + df2 = DataFrame({"b": [4, 5, 6]}) |
| 218 | + df1_orig = df1.copy() |
| 219 | + df2_orig = df2.copy() |
| 220 | + |
| 221 | + result = merge(df1, df2, left_index=True, right_index=True) |
| 222 | + |
| 223 | + if using_copy_on_write: |
| 224 | + assert np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 225 | + assert np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 226 | + else: |
| 227 | + assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 228 | + assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 229 | + |
| 230 | + result.iloc[0, 0] = 0 |
| 231 | + if using_copy_on_write: |
| 232 | + assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 233 | + assert np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 234 | + |
| 235 | + result.iloc[0, 1] = 0 |
| 236 | + if using_copy_on_write: |
| 237 | + assert not np.shares_memory(get_array(result, "b"), get_array(df2, "b")) |
| 238 | + tm.assert_frame_equal(df1, df1_orig) |
| 239 | + tm.assert_frame_equal(df2, df2_orig) |
| 240 | + |
| 241 | + |
| 242 | +# TODO(CoW) add merge tests where one of left/right isn't copied |
0 commit comments