|
4 | 4 | from pandas import (
|
5 | 5 | DataFrame,
|
6 | 6 | Series,
|
| 7 | + date_range, |
7 | 8 | )
|
8 | 9 | import pandas._testing as tm
|
9 | 10 | from pandas.tests.copy_view.util import get_array
|
@@ -119,3 +120,66 @@ def test_ravel_read_only(using_copy_on_write, order):
|
119 | 120 | if using_copy_on_write:
|
120 | 121 | assert arr.flags.writeable is False
|
121 | 122 | assert np.shares_memory(get_array(ser), arr)
|
| 123 | + |
| 124 | + |
| 125 | +def test_series_array_ea_dtypes(using_copy_on_write): |
| 126 | + ser = Series([1, 2, 3], dtype="Int64") |
| 127 | + arr = np.asarray(ser, dtype="int64") |
| 128 | + assert np.shares_memory(arr, get_array(ser)) |
| 129 | + if using_copy_on_write: |
| 130 | + assert arr.flags.writeable is False |
| 131 | + else: |
| 132 | + assert arr.flags.writeable is True |
| 133 | + |
| 134 | + arr = np.asarray(ser) |
| 135 | + assert not np.shares_memory(arr, get_array(ser)) |
| 136 | + assert arr.flags.writeable is True |
| 137 | + |
| 138 | + |
| 139 | +def test_dataframe_array_ea_dtypes(using_copy_on_write): |
| 140 | + df = DataFrame({"a": [1, 2, 3]}, dtype="Int64") |
| 141 | + arr = np.asarray(df, dtype="int64") |
| 142 | + # TODO: This should be able to share memory, but we are roundtripping |
| 143 | + # through object |
| 144 | + assert not np.shares_memory(arr, get_array(df, "a")) |
| 145 | + assert arr.flags.writeable is True |
| 146 | + |
| 147 | + arr = np.asarray(df) |
| 148 | + if using_copy_on_write: |
| 149 | + # TODO(CoW): This should be True |
| 150 | + assert arr.flags.writeable is False |
| 151 | + else: |
| 152 | + assert arr.flags.writeable is True |
| 153 | + |
| 154 | + |
| 155 | +def test_dataframe_array_string_dtype(using_copy_on_write, using_array_manager): |
| 156 | + df = DataFrame({"a": ["a", "b"]}, dtype="string") |
| 157 | + arr = np.asarray(df) |
| 158 | + if not using_array_manager: |
| 159 | + assert np.shares_memory(arr, get_array(df, "a")) |
| 160 | + if using_copy_on_write: |
| 161 | + assert arr.flags.writeable is False |
| 162 | + else: |
| 163 | + assert arr.flags.writeable is True |
| 164 | + |
| 165 | + |
| 166 | +def test_dataframe_multiple_numpy_dtypes(): |
| 167 | + df = DataFrame({"a": [1, 2, 3], "b": 1.5}) |
| 168 | + arr = np.asarray(df) |
| 169 | + assert not np.shares_memory(arr, get_array(df, "a")) |
| 170 | + assert arr.flags.writeable is True |
| 171 | + |
| 172 | + |
| 173 | +def test_values_is_ea(using_copy_on_write): |
| 174 | + df = DataFrame({"a": date_range("2012-01-01", periods=3)}) |
| 175 | + arr = np.asarray(df) |
| 176 | + if using_copy_on_write: |
| 177 | + assert arr.flags.writeable is False |
| 178 | + else: |
| 179 | + assert arr.flags.writeable is True |
| 180 | + |
| 181 | + |
| 182 | +def test_empty_dataframe(): |
| 183 | + df = DataFrame() |
| 184 | + arr = np.asarray(df) |
| 185 | + assert arr.flags.writeable is True |
0 commit comments