forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_assert_numpy_array_equal.py
223 lines (154 loc) · 6.47 KB
/
test_assert_numpy_array_equal.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import copy
import numpy as np
import pytest
import pandas as pd
from pandas import Timestamp
import pandas._testing as tm
def test_assert_numpy_array_equal_shape_mismatch():
msg = """numpy array are different
numpy array shapes are different
\\[left\\]: \\(2L*,\\)
\\[right\\]: \\(3L*,\\)"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(np.array([1, 2]), np.array([3, 4, 5]))
def test_assert_numpy_array_equal_bad_type():
expected = "Expected type"
with pytest.raises(AssertionError, match=expected):
tm.assert_numpy_array_equal(1, 2)
@pytest.mark.parametrize(
"a,b,klass1,klass2",
[(np.array([1]), 1, "ndarray", "int"), (1, np.array([1]), "int", "ndarray")],
)
def test_assert_numpy_array_equal_class_mismatch(a, b, klass1, klass2):
msg = f"""numpy array are different
numpy array classes are different
\\[left\\]: {klass1}
\\[right\\]: {klass2}"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(a, b)
def test_assert_numpy_array_equal_value_mismatch1():
msg = """numpy array are different
numpy array values are different \\(66\\.66667 %\\)
\\[left\\]: \\[nan, 2\\.0, 3\\.0\\]
\\[right\\]: \\[1\\.0, nan, 3\\.0\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))
def test_assert_numpy_array_equal_value_mismatch2():
msg = """numpy array are different
numpy array values are different \\(50\\.0 %\\)
\\[left\\]: \\[1, 2\\]
\\[right\\]: \\[1, 3\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(np.array([1, 2]), np.array([1, 3]))
def test_assert_numpy_array_equal_value_mismatch3():
msg = """numpy array are different
numpy array values are different \\(16\\.66667 %\\)
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\], \\[5, 6\\]\\]
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(
np.array([[1, 2], [3, 4], [5, 6]]), np.array([[1, 3], [3, 4], [5, 6]])
)
def test_assert_numpy_array_equal_value_mismatch4():
msg = """numpy array are different
numpy array values are different \\(50\\.0 %\\)
\\[left\\]: \\[1\\.1, 2\\.000001\\]
\\[right\\]: \\[1\\.1, 2.0\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(np.array([1.1, 2.000001]), np.array([1.1, 2.0]))
def test_assert_numpy_array_equal_value_mismatch5():
msg = """numpy array are different
numpy array values are different \\(16\\.66667 %\\)
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\], \\[5, 6\\]\\]
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(
np.array([[1, 2], [3, 4], [5, 6]]), np.array([[1, 3], [3, 4], [5, 6]])
)
def test_assert_numpy_array_equal_value_mismatch6():
msg = """numpy array are different
numpy array values are different \\(25\\.0 %\\)
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\]\\]
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\]\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(
np.array([[1, 2], [3, 4]]), np.array([[1, 3], [3, 4]])
)
def test_assert_numpy_array_equal_shape_mismatch_override():
msg = """Index are different
Index shapes are different
\\[left\\]: \\(2L*,\\)
\\[right\\]: \\(3L*,\\)"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(np.array([1, 2]), np.array([3, 4, 5]), obj="Index")
def test_numpy_array_equal_unicode():
# see gh-20503
#
# Test ensures that `tm.assert_numpy_array_equals` raises the right
# exception when comparing np.arrays containing differing unicode objects.
msg = """numpy array are different
numpy array values are different \\(33\\.33333 %\\)
\\[left\\]: \\[á, à, ä\\]
\\[right\\]: \\[á, à, å\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(
np.array(["á", "à", "ä"]), np.array(["á", "à", "å"])
)
def test_numpy_array_equal_object():
a = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-01")])
b = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-02")])
msg = """numpy array are different
numpy array values are different \\(50\\.0 %\\)
\\[left\\]: \\[2011-01-01 00:00:00, 2011-01-01 00:00:00\\]
\\[right\\]: \\[2011-01-01 00:00:00, 2011-01-02 00:00:00\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(a, b)
@pytest.mark.parametrize("other_type", ["same", "copy"])
@pytest.mark.parametrize("check_same", ["same", "copy"])
def test_numpy_array_equal_copy_flag(other_type, check_same):
a = np.array([1, 2, 3])
msg = None
if other_type == "same":
other = a.view()
else:
other = a.copy()
if check_same != other_type:
msg = (
r"array\(\[1, 2, 3\]\) is not array\(\[1, 2, 3\]\)"
if check_same == "same"
else r"array\(\[1, 2, 3\]\) is array\(\[1, 2, 3\]\)"
)
if msg is not None:
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(a, other, check_same=check_same)
else:
tm.assert_numpy_array_equal(a, other, check_same=check_same)
def test_numpy_array_equal_contains_na():
# https://github.com/pandas-dev/pandas/issues/31881
a = np.array([True, False])
b = np.array([True, pd.NA], dtype=object)
msg = """numpy array are different
numpy array values are different \\(50.0 %\\)
\\[left\\]: \\[True, False\\]
\\[right\\]: \\[True, <NA>\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(a, b)
def test_numpy_array_equal_identical_na(nulls_fixture):
a = np.array([nulls_fixture], dtype=object)
tm.assert_numpy_array_equal(a, a)
# matching but not the identical object
if hasattr(nulls_fixture, "copy"):
other = nulls_fixture.copy()
else:
other = copy.copy(nulls_fixture)
b = np.array([other], dtype=object)
tm.assert_numpy_array_equal(a, b)
def test_numpy_array_equal_different_na():
a = np.array([np.nan], dtype=object)
b = np.array([pd.NA], dtype=object)
msg = """numpy array are different
numpy array values are different \\(100.0 %\\)
\\[left\\]: \\[nan\\]
\\[right\\]: \\[<NA>\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_numpy_array_equal(a, b)