|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pandas import ( |
| 4 | + DataFrame, |
| 5 | + Series, |
| 6 | + concat, |
| 7 | +) |
| 8 | +import pandas._testing as tm |
| 9 | +from pandas.tests.copy_view.util import get_array |
| 10 | + |
| 11 | + |
| 12 | +def test_concat_frames(using_copy_on_write): |
| 13 | + df = DataFrame({"b": ["a"] * 3}) |
| 14 | + df2 = DataFrame({"a": ["a"] * 3}) |
| 15 | + df_orig = df.copy() |
| 16 | + result = concat([df, df2], axis=1) |
| 17 | + |
| 18 | + if using_copy_on_write: |
| 19 | + assert np.shares_memory(get_array(result, "b"), get_array(df, "b")) |
| 20 | + assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 21 | + else: |
| 22 | + assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) |
| 23 | + assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 24 | + |
| 25 | + result.iloc[0, 0] = "d" |
| 26 | + if using_copy_on_write: |
| 27 | + assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) |
| 28 | + assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 29 | + |
| 30 | + result.iloc[0, 1] = "d" |
| 31 | + if using_copy_on_write: |
| 32 | + assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 33 | + tm.assert_frame_equal(df, df_orig) |
| 34 | + |
| 35 | + |
| 36 | +def test_concat_frames_updating_input(using_copy_on_write): |
| 37 | + df = DataFrame({"b": ["a"] * 3}) |
| 38 | + df2 = DataFrame({"a": ["a"] * 3}) |
| 39 | + result = concat([df, df2], axis=1) |
| 40 | + |
| 41 | + if using_copy_on_write: |
| 42 | + assert np.shares_memory(get_array(result, "b"), get_array(df, "b")) |
| 43 | + assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 44 | + else: |
| 45 | + assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) |
| 46 | + assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 47 | + |
| 48 | + expected = result.copy() |
| 49 | + df.iloc[0, 0] = "d" |
| 50 | + if using_copy_on_write: |
| 51 | + assert not np.shares_memory(get_array(result, "b"), get_array(df, "b")) |
| 52 | + assert np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 53 | + |
| 54 | + df2.iloc[0, 0] = "d" |
| 55 | + if using_copy_on_write: |
| 56 | + assert not np.shares_memory(get_array(result, "a"), get_array(df2, "a")) |
| 57 | + tm.assert_frame_equal(result, expected) |
| 58 | + |
| 59 | + |
| 60 | +def test_concat_series(using_copy_on_write): |
| 61 | + ser = Series([1, 2], name="a") |
| 62 | + ser2 = Series([3, 4], name="b") |
| 63 | + ser_orig = ser.copy() |
| 64 | + ser2_orig = ser2.copy() |
| 65 | + result = concat([ser, ser2], axis=1) |
| 66 | + |
| 67 | + if using_copy_on_write: |
| 68 | + assert np.shares_memory(get_array(result, "a"), ser.values) |
| 69 | + assert np.shares_memory(get_array(result, "b"), ser2.values) |
| 70 | + else: |
| 71 | + assert not np.shares_memory(get_array(result, "a"), ser.values) |
| 72 | + assert not np.shares_memory(get_array(result, "b"), ser2.values) |
| 73 | + |
| 74 | + result.iloc[0, 0] = 100 |
| 75 | + if using_copy_on_write: |
| 76 | + assert not np.shares_memory(get_array(result, "a"), ser.values) |
| 77 | + assert np.shares_memory(get_array(result, "b"), ser2.values) |
| 78 | + |
| 79 | + result.iloc[0, 1] = 1000 |
| 80 | + if using_copy_on_write: |
| 81 | + assert not np.shares_memory(get_array(result, "b"), ser2.values) |
| 82 | + tm.assert_series_equal(ser, ser_orig) |
| 83 | + tm.assert_series_equal(ser2, ser2_orig) |
| 84 | + |
| 85 | + |
| 86 | +def test_concat_frames_chained(using_copy_on_write): |
| 87 | + df1 = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]}) |
| 88 | + df2 = DataFrame({"c": [4, 5, 6]}) |
| 89 | + df3 = DataFrame({"d": [4, 5, 6]}) |
| 90 | + result = concat([concat([df1, df2], axis=1), df3], axis=1) |
| 91 | + expected = result.copy() |
| 92 | + |
| 93 | + if using_copy_on_write: |
| 94 | + assert np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 95 | + assert np.shares_memory(get_array(result, "c"), get_array(df2, "c")) |
| 96 | + assert np.shares_memory(get_array(result, "d"), get_array(df3, "d")) |
| 97 | + else: |
| 98 | + assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 99 | + assert not np.shares_memory(get_array(result, "c"), get_array(df2, "c")) |
| 100 | + assert not np.shares_memory(get_array(result, "d"), get_array(df3, "d")) |
| 101 | + |
| 102 | + df1.iloc[0, 0] = 100 |
| 103 | + if using_copy_on_write: |
| 104 | + assert not np.shares_memory(get_array(result, "a"), get_array(df1, "a")) |
| 105 | + |
| 106 | + tm.assert_frame_equal(result, expected) |
| 107 | + |
| 108 | + |
| 109 | +def test_concat_series_chained(using_copy_on_write): |
| 110 | + ser1 = Series([1, 2, 3], name="a") |
| 111 | + ser2 = Series([4, 5, 6], name="c") |
| 112 | + ser3 = Series([4, 5, 6], name="d") |
| 113 | + result = concat([concat([ser1, ser2], axis=1), ser3], axis=1) |
| 114 | + expected = result.copy() |
| 115 | + |
| 116 | + if using_copy_on_write: |
| 117 | + assert np.shares_memory(get_array(result, "a"), get_array(ser1, "a")) |
| 118 | + assert np.shares_memory(get_array(result, "c"), get_array(ser2, "c")) |
| 119 | + assert np.shares_memory(get_array(result, "d"), get_array(ser3, "d")) |
| 120 | + else: |
| 121 | + assert not np.shares_memory(get_array(result, "a"), get_array(ser1, "a")) |
| 122 | + assert not np.shares_memory(get_array(result, "c"), get_array(ser2, "c")) |
| 123 | + assert not np.shares_memory(get_array(result, "d"), get_array(ser3, "d")) |
| 124 | + |
| 125 | + ser1.iloc[0] = 100 |
| 126 | + if using_copy_on_write: |
| 127 | + assert not np.shares_memory(get_array(result, "a"), get_array(ser1, "a")) |
| 128 | + |
| 129 | + tm.assert_frame_equal(result, expected) |
| 130 | + |
| 131 | + |
| 132 | +def test_concat_series_updating_input(using_copy_on_write): |
| 133 | + ser = Series([1, 2], name="a") |
| 134 | + ser2 = Series([3, 4], name="b") |
| 135 | + expected = DataFrame({"a": [1, 2], "b": [3, 4]}) |
| 136 | + result = concat([ser, ser2], axis=1) |
| 137 | + |
| 138 | + if using_copy_on_write: |
| 139 | + assert np.shares_memory(get_array(result, "a"), get_array(ser, "a")) |
| 140 | + assert np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) |
| 141 | + else: |
| 142 | + assert not np.shares_memory(get_array(result, "a"), get_array(ser, "a")) |
| 143 | + assert not np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) |
| 144 | + |
| 145 | + ser.iloc[0] = 100 |
| 146 | + if using_copy_on_write: |
| 147 | + assert not np.shares_memory(get_array(result, "a"), get_array(ser, "a")) |
| 148 | + assert np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) |
| 149 | + tm.assert_frame_equal(result, expected) |
| 150 | + |
| 151 | + ser2.iloc[0] = 1000 |
| 152 | + if using_copy_on_write: |
| 153 | + assert not np.shares_memory(get_array(result, "b"), get_array(ser2, "b")) |
| 154 | + tm.assert_frame_equal(result, expected) |
| 155 | + |
| 156 | + |
| 157 | +def test_concat_mixed_series_frame(using_copy_on_write): |
| 158 | + df = DataFrame({"a": [1, 2, 3], "c": 1}) |
| 159 | + ser = Series([4, 5, 6], name="d") |
| 160 | + result = concat([df, ser], axis=1) |
| 161 | + expected = result.copy() |
| 162 | + |
| 163 | + if using_copy_on_write: |
| 164 | + assert np.shares_memory(get_array(result, "a"), get_array(df, "a")) |
| 165 | + assert np.shares_memory(get_array(result, "c"), get_array(df, "c")) |
| 166 | + assert np.shares_memory(get_array(result, "d"), get_array(ser, "d")) |
| 167 | + else: |
| 168 | + assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) |
| 169 | + assert not np.shares_memory(get_array(result, "c"), get_array(df, "c")) |
| 170 | + assert not np.shares_memory(get_array(result, "d"), get_array(ser, "d")) |
| 171 | + |
| 172 | + ser.iloc[0] = 100 |
| 173 | + if using_copy_on_write: |
| 174 | + assert not np.shares_memory(get_array(result, "d"), get_array(ser, "d")) |
| 175 | + |
| 176 | + df.iloc[0, 0] = 100 |
| 177 | + if using_copy_on_write: |
| 178 | + assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) |
| 179 | + tm.assert_frame_equal(result, expected) |
0 commit comments