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