|
19 | 19 | "method", |
20 | 20 | [ |
21 | 21 | lambda ser: ser.values, |
22 | | - # lambda ser: np.asarray(ser.array), |
| 22 | + lambda ser: np.asarray(ser.array), |
23 | 23 | lambda ser: np.asarray(ser), |
24 | 24 | lambda ser: np.array(ser, copy=False), |
25 | 25 | ], |
26 | | - ids=["values", "np.asarray", "np.array"], |
| 26 | + ids=["values", "array", "np.asarray", "np.array"], |
27 | 27 | ) |
28 | | -def test_series_values(method): |
| 28 | +def test_series_values(request, method): |
29 | 29 | ser = Series([1, 2, 3], name="name") |
30 | 30 | ser_orig = ser.copy() |
31 | 31 |
|
32 | 32 | arr = method(ser) |
33 | 33 |
|
| 34 | + if request.node.callspec.id == "array": |
| 35 | + # https://github.com/pandas-dev/pandas/issues/63099 |
| 36 | + # .array for now does not return a read-only view |
| 37 | + assert arr.flags.writeable is True |
| 38 | + # updating the array updates the series |
| 39 | + arr[0] = 0 |
| 40 | + assert ser.iloc[0] == 0 |
| 41 | + return |
| 42 | + |
34 | 43 | # .values still gives a view but is read-only |
35 | 44 | assert np.shares_memory(arr, get_array(ser, "name")) |
36 | 45 | assert arr.flags.writeable is False |
@@ -109,20 +118,42 @@ def test_series_to_numpy(): |
109 | 118 | @pytest.mark.parametrize( |
110 | 119 | "method", |
111 | 120 | [ |
112 | | - # lambda ser: np.asarray(ser.array), |
| 121 | + lambda ser: np.asarray(ser.values), |
| 122 | + lambda ser: np.asarray(ser.array), |
113 | 123 | lambda ser: np.asarray(ser), |
114 | 124 | lambda ser: np.asarray(ser, dtype="int64"), |
115 | 125 | lambda ser: np.array(ser, copy=False), |
116 | 126 | ], |
117 | | - ids=["np.asarray", "np.asarray-dtype", "np.array"], |
| 127 | + ids=["values", "array", "np.asarray", "np.asarray-dtype", "np.array"], |
118 | 128 | ) |
119 | | -def test_series_values_ea_dtypes(method): |
| 129 | +def test_series_values_ea_dtypes(request, method): |
120 | 130 | ser = Series([1, 2, 3], dtype="Int64") |
| 131 | + ser_orig = ser.copy() |
| 132 | + |
121 | 133 | arr = method(ser) |
122 | 134 |
|
| 135 | + if request.node.callspec.id in ("values", "array"): |
| 136 | + # https://github.com/pandas-dev/pandas/issues/63099 |
| 137 | + # .array/values for now does not return a read-only view |
| 138 | + assert arr.flags.writeable is True |
| 139 | + # updating the array updates the series |
| 140 | + arr[0] = 0 |
| 141 | + assert ser.iloc[0] == 0 |
| 142 | + return |
| 143 | + |
| 144 | + # conversion to ndarray gives a view but is read-only |
123 | 145 | assert np.shares_memory(arr, get_array(ser)) |
124 | 146 | assert arr.flags.writeable is False |
125 | 147 |
|
| 148 | + # mutating series through arr therefore doesn't work |
| 149 | + with pytest.raises(ValueError, match="read-only"): |
| 150 | + arr[0] = 0 |
| 151 | + tm.assert_series_equal(ser, ser_orig) |
| 152 | + |
| 153 | + # mutating the series itself still works |
| 154 | + ser.iloc[0] = 0 |
| 155 | + assert ser.values[0] == 0 |
| 156 | + |
126 | 157 |
|
127 | 158 | @pytest.mark.parametrize( |
128 | 159 | "method", |
|
0 commit comments