|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pandas import ( |
| 5 | + DataFrame, |
| 6 | + Series, |
| 7 | +) |
| 8 | +import pandas._testing as tm |
| 9 | +from pandas.tests.copy_view.util import get_array |
| 10 | + |
| 11 | +# ----------------------------------------------------------------------------- |
| 12 | +# Copy/view behaviour for accessing underlying array of Series/DataFrame |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "method", |
| 17 | + [lambda ser: ser.values, lambda ser: np.asarray(ser)], |
| 18 | + ids=["values", "asarray"], |
| 19 | +) |
| 20 | +def test_series_values(using_copy_on_write, method): |
| 21 | + ser = Series([1, 2, 3], name="name") |
| 22 | + ser_orig = ser.copy() |
| 23 | + |
| 24 | + arr = method(ser) |
| 25 | + |
| 26 | + if using_copy_on_write: |
| 27 | + # .values still gives a view but is read-only |
| 28 | + assert np.shares_memory(arr, get_array(ser, "name")) |
| 29 | + assert arr.flags.writeable is False |
| 30 | + |
| 31 | + # mutating series through arr therefore doesn't work |
| 32 | + with pytest.raises(ValueError, match="read-only"): |
| 33 | + arr[0] = 0 |
| 34 | + tm.assert_series_equal(ser, ser_orig) |
| 35 | + |
| 36 | + # mutating the series itself still works |
| 37 | + ser.iloc[0] = 0 |
| 38 | + assert ser.values[0] == 0 |
| 39 | + else: |
| 40 | + assert arr.flags.writeable is True |
| 41 | + arr[0] = 0 |
| 42 | + assert ser.iloc[0] == 0 |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "method", |
| 47 | + [lambda df: df.values, lambda df: np.asarray(df)], |
| 48 | + ids=["values", "asarray"], |
| 49 | +) |
| 50 | +def test_dataframe_values(using_copy_on_write, using_array_manager, method): |
| 51 | + df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) |
| 52 | + df_orig = df.copy() |
| 53 | + |
| 54 | + arr = method(df) |
| 55 | + |
| 56 | + if using_copy_on_write: |
| 57 | + # .values still gives a view but is read-only |
| 58 | + assert np.shares_memory(arr, get_array(df, "a")) |
| 59 | + assert arr.flags.writeable is False |
| 60 | + |
| 61 | + # mutating series through arr therefore doesn't work |
| 62 | + with pytest.raises(ValueError, match="read-only"): |
| 63 | + arr[0, 0] = 0 |
| 64 | + tm.assert_frame_equal(df, df_orig) |
| 65 | + |
| 66 | + # mutating the series itself still works |
| 67 | + df.iloc[0, 0] = 0 |
| 68 | + assert df.values[0, 0] == 0 |
| 69 | + else: |
| 70 | + assert arr.flags.writeable is True |
| 71 | + arr[0, 0] = 0 |
| 72 | + if not using_array_manager: |
| 73 | + assert df.iloc[0, 0] == 0 |
| 74 | + else: |
| 75 | + tm.assert_frame_equal(df, df_orig) |
| 76 | + |
| 77 | + |
| 78 | +def test_series_to_numpy(using_copy_on_write): |
| 79 | + ser = Series([1, 2, 3], name="name") |
| 80 | + ser_orig = ser.copy() |
| 81 | + |
| 82 | + # default: copy=False, no dtype or NAs |
| 83 | + arr = ser.to_numpy() |
| 84 | + if using_copy_on_write: |
| 85 | + # to_numpy still gives a view but is read-only |
| 86 | + assert np.shares_memory(arr, get_array(ser, "name")) |
| 87 | + assert arr.flags.writeable is False |
| 88 | + |
| 89 | + # mutating series through arr therefore doesn't work |
| 90 | + with pytest.raises(ValueError, match="read-only"): |
| 91 | + arr[0] = 0 |
| 92 | + tm.assert_series_equal(ser, ser_orig) |
| 93 | + |
| 94 | + # mutating the series itself still works |
| 95 | + ser.iloc[0] = 0 |
| 96 | + assert ser.values[0] == 0 |
| 97 | + else: |
| 98 | + assert arr.flags.writeable is True |
| 99 | + arr[0] = 0 |
| 100 | + assert ser.iloc[0] == 0 |
| 101 | + |
| 102 | + # specify copy=False gives a writeable array |
| 103 | + ser = Series([1, 2, 3], name="name") |
| 104 | + arr = ser.to_numpy(copy=True) |
| 105 | + assert not np.shares_memory(arr, get_array(ser, "name")) |
| 106 | + assert arr.flags.writeable is True |
| 107 | + |
| 108 | + # specifying a dtype that already causes a copy also gives a writeable array |
| 109 | + ser = Series([1, 2, 3], name="name") |
| 110 | + arr = ser.to_numpy(dtype="float64") |
| 111 | + assert not np.shares_memory(arr, get_array(ser, "name")) |
| 112 | + assert arr.flags.writeable is True |
0 commit comments