|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pandas.compat import pa_version_under7p0 |
| 5 | + |
| 6 | +from pandas import ( |
| 7 | + DataFrame, |
| 8 | + Series, |
| 9 | + Timestamp, |
| 10 | + date_range, |
| 11 | +) |
| 12 | +import pandas._testing as tm |
| 13 | +from pandas.tests.copy_view.util import get_array |
| 14 | + |
| 15 | + |
| 16 | +def test_astype_single_dtype(using_copy_on_write): |
| 17 | + df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": 1.5}) |
| 18 | + df_orig = df.copy() |
| 19 | + df2 = df.astype("float64") |
| 20 | + |
| 21 | + if using_copy_on_write: |
| 22 | + assert np.shares_memory(get_array(df2, "c"), get_array(df, "c")) |
| 23 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 24 | + else: |
| 25 | + assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c")) |
| 26 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 27 | + |
| 28 | + # mutating df2 triggers a copy-on-write for that column/block |
| 29 | + df2.iloc[0, 2] = 5.5 |
| 30 | + if using_copy_on_write: |
| 31 | + assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c")) |
| 32 | + tm.assert_frame_equal(df, df_orig) |
| 33 | + |
| 34 | + # mutating parent also doesn't update result |
| 35 | + df2 = df.astype("float64") |
| 36 | + df.iloc[0, 2] = 5.5 |
| 37 | + tm.assert_frame_equal(df2, df_orig.astype("float64")) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("dtype", ["int64", "Int64"]) |
| 41 | +@pytest.mark.parametrize("new_dtype", ["int64", "Int64", "int64[pyarrow]"]) |
| 42 | +def test_astype_avoids_copy(using_copy_on_write, dtype, new_dtype): |
| 43 | + if new_dtype == "int64[pyarrow]" and pa_version_under7p0: |
| 44 | + pytest.skip("pyarrow not installed") |
| 45 | + df = DataFrame({"a": [1, 2, 3]}, dtype=dtype) |
| 46 | + df_orig = df.copy() |
| 47 | + df2 = df.astype(new_dtype) |
| 48 | + |
| 49 | + if using_copy_on_write: |
| 50 | + assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 51 | + else: |
| 52 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 53 | + |
| 54 | + # mutating df2 triggers a copy-on-write for that column/block |
| 55 | + df2.iloc[0, 0] = 10 |
| 56 | + if using_copy_on_write: |
| 57 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 58 | + tm.assert_frame_equal(df, df_orig) |
| 59 | + |
| 60 | + # mutating parent also doesn't update result |
| 61 | + df2 = df.astype(new_dtype) |
| 62 | + df.iloc[0, 0] = 100 |
| 63 | + tm.assert_frame_equal(df2, df_orig.astype(new_dtype)) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("dtype", ["float64", "int32", "Int32", "int32[pyarrow]"]) |
| 67 | +def test_astype_different_target_dtype(using_copy_on_write, dtype): |
| 68 | + if dtype == "int32[pyarrow]" and pa_version_under7p0: |
| 69 | + pytest.skip("pyarrow not installed") |
| 70 | + df = DataFrame({"a": [1, 2, 3]}) |
| 71 | + df_orig = df.copy() |
| 72 | + df2 = df.astype(dtype) |
| 73 | + |
| 74 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 75 | + if using_copy_on_write: |
| 76 | + assert df2._mgr._has_no_reference(0) |
| 77 | + |
| 78 | + df2.iloc[0, 0] = 5 |
| 79 | + tm.assert_frame_equal(df, df_orig) |
| 80 | + |
| 81 | + # mutating parent also doesn't update result |
| 82 | + df2 = df.astype(dtype) |
| 83 | + df.iloc[0, 0] = 100 |
| 84 | + tm.assert_frame_equal(df2, df_orig.astype(dtype)) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "dtype, new_dtype", [("object", "string"), ("string", "object")] |
| 89 | +) |
| 90 | +def test_astype_string_and_object(using_copy_on_write, dtype, new_dtype): |
| 91 | + df = DataFrame({"a": ["a", "b", "c"]}, dtype=dtype) |
| 92 | + df_orig = df.copy() |
| 93 | + df2 = df.astype(new_dtype) |
| 94 | + |
| 95 | + if using_copy_on_write: |
| 96 | + assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 97 | + else: |
| 98 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 99 | + |
| 100 | + df2.iloc[0, 0] = "x" |
| 101 | + tm.assert_frame_equal(df, df_orig) |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.parametrize( |
| 105 | + "dtype, new_dtype", [("object", "string"), ("string", "object")] |
| 106 | +) |
| 107 | +def test_astype_string_and_object_update_original( |
| 108 | + using_copy_on_write, dtype, new_dtype |
| 109 | +): |
| 110 | + df = DataFrame({"a": ["a", "b", "c"]}, dtype=dtype) |
| 111 | + df2 = df.astype(new_dtype) |
| 112 | + df_orig = df2.copy() |
| 113 | + |
| 114 | + if using_copy_on_write: |
| 115 | + assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 116 | + else: |
| 117 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 118 | + |
| 119 | + df.iloc[0, 0] = "x" |
| 120 | + tm.assert_frame_equal(df2, df_orig) |
| 121 | + |
| 122 | + |
| 123 | +def test_astype_dict_dtypes(using_copy_on_write): |
| 124 | + df = DataFrame( |
| 125 | + {"a": [1, 2, 3], "b": [4, 5, 6], "c": Series([1.5, 1.5, 1.5], dtype="float64")} |
| 126 | + ) |
| 127 | + df_orig = df.copy() |
| 128 | + df2 = df.astype({"a": "float64", "c": "float64"}) |
| 129 | + |
| 130 | + if using_copy_on_write: |
| 131 | + assert np.shares_memory(get_array(df2, "c"), get_array(df, "c")) |
| 132 | + assert np.shares_memory(get_array(df2, "b"), get_array(df, "b")) |
| 133 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 134 | + else: |
| 135 | + assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c")) |
| 136 | + assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b")) |
| 137 | + assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) |
| 138 | + |
| 139 | + # mutating df2 triggers a copy-on-write for that column/block |
| 140 | + df2.iloc[0, 2] = 5.5 |
| 141 | + if using_copy_on_write: |
| 142 | + assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c")) |
| 143 | + |
| 144 | + df2.iloc[0, 1] = 10 |
| 145 | + if using_copy_on_write: |
| 146 | + assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b")) |
| 147 | + tm.assert_frame_equal(df, df_orig) |
| 148 | + |
| 149 | + |
| 150 | +def test_astype_different_datetime_resos(using_copy_on_write): |
| 151 | + df = DataFrame({"a": date_range("2019-12-31", periods=2, freq="D")}) |
| 152 | + result = df.astype("datetime64[ms]") |
| 153 | + |
| 154 | + assert not np.shares_memory(get_array(df, "a"), get_array(result, "a")) |
| 155 | + if using_copy_on_write: |
| 156 | + assert result._mgr._has_no_reference(0) |
| 157 | + |
| 158 | + |
| 159 | +def test_astype_different_timezones(using_copy_on_write): |
| 160 | + df = DataFrame( |
| 161 | + {"a": date_range("2019-12-31", periods=5, freq="D", tz="US/Pacific")} |
| 162 | + ) |
| 163 | + result = df.astype("datetime64[ns, Europe/Berlin]") |
| 164 | + if using_copy_on_write: |
| 165 | + assert not result._mgr._has_no_reference(0) |
| 166 | + assert np.shares_memory(get_array(df, "a").asi8, get_array(result, "a").asi8) |
| 167 | + |
| 168 | + |
| 169 | +def test_astype_different_timezones_different_reso(using_copy_on_write): |
| 170 | + df = DataFrame( |
| 171 | + {"a": date_range("2019-12-31", periods=5, freq="D", tz="US/Pacific")} |
| 172 | + ) |
| 173 | + result = df.astype("datetime64[ms, Europe/Berlin]") |
| 174 | + if using_copy_on_write: |
| 175 | + assert result._mgr._has_no_reference(0) |
| 176 | + assert not np.shares_memory( |
| 177 | + get_array(df, "a").asi8, get_array(result, "a").asi8 |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.skipif(pa_version_under7p0, reason="pyarrow not installed") |
| 182 | +def test_astype_arrow_timestamp(using_copy_on_write): |
| 183 | + df = DataFrame( |
| 184 | + { |
| 185 | + "a": [ |
| 186 | + Timestamp("2020-01-01 01:01:01.000001"), |
| 187 | + Timestamp("2020-01-01 01:01:01.000001"), |
| 188 | + ] |
| 189 | + }, |
| 190 | + dtype="M8[ns]", |
| 191 | + ) |
| 192 | + result = df.astype("timestamp[ns][pyarrow]") |
| 193 | + if using_copy_on_write: |
| 194 | + assert not result._mgr._has_no_reference(0) |
| 195 | + assert np.shares_memory(get_array(df, "a").asi8, get_array(result, "a")._data) |
0 commit comments