forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_assert_index_equal.py
258 lines (187 loc) · 7.97 KB
/
test_assert_index_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import numpy as np
import pytest
from pandas import (
Categorical,
CategoricalIndex,
Index,
MultiIndex,
NaT,
RangeIndex,
)
import pandas._testing as tm
def test_index_equal_levels_mismatch():
msg = """Index are different
Index levels are different
\\[left\\]: 1, Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
\\[right\\]: 2, MultiIndex\\(\\[\\('A', 1\\),
\\('A', 2\\),
\\('B', 3\\),
\\('B', 4\\)\\],
\\)"""
idx1 = Index([1, 2, 3])
idx2 = MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 3), ("B", 4)])
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, exact=False)
def test_index_equal_values_mismatch(check_exact):
msg = """MultiIndex level \\[1\\] are different
MultiIndex level \\[1\\] values are different \\(25\\.0 %\\)
\\[left\\]: Int64Index\\(\\[2, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
idx1 = MultiIndex.from_tuples([("A", 2), ("A", 2), ("B", 3), ("B", 4)])
idx2 = MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 3), ("B", 4)])
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, check_exact=check_exact)
def test_index_equal_length_mismatch(check_exact):
msg = """Index are different
Index length are different
\\[left\\]: 3, Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
\\[right\\]: 4, Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
idx1 = Index([1, 2, 3])
idx2 = Index([1, 2, 3, 4])
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, check_exact=check_exact)
@pytest.mark.parametrize("exact", [False, "equiv"])
def test_index_equal_class(exact):
idx1 = Index([0, 1, 2])
idx2 = RangeIndex(3)
tm.assert_index_equal(idx1, idx2, exact=exact)
@pytest.mark.parametrize(
"idx_values, msg_str",
[
[[1, 2, 3.0], "Float64Index\\(\\[1\\.0, 2\\.0, 3\\.0\\], dtype='float64'\\)"],
[range(3), "RangeIndex\\(start=0, stop=3, step=1\\)"],
],
)
def test_index_equal_class_mismatch(check_exact, idx_values, msg_str):
msg = f"""Index are different
Index classes are different
\\[left\\]: Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
\\[right\\]: {msg_str}"""
idx1 = Index([1, 2, 3])
idx2 = Index(idx_values)
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, exact=True, check_exact=check_exact)
def test_index_equal_values_close(check_exact):
idx1 = Index([1, 2, 3.0])
idx2 = Index([1, 2, 3.0000000001])
if check_exact:
msg = """Index are different
Index values are different \\(33\\.33333 %\\)
\\[left\\]: Float64Index\\(\\[1.0, 2.0, 3.0], dtype='float64'\\)
\\[right\\]: Float64Index\\(\\[1.0, 2.0, 3.0000000001\\], dtype='float64'\\)"""
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, check_exact=check_exact)
else:
tm.assert_index_equal(idx1, idx2, check_exact=check_exact)
def test_index_equal_values_less_close(check_exact, rtol):
idx1 = Index([1, 2, 3.0])
idx2 = Index([1, 2, 3.0001])
kwargs = {"check_exact": check_exact, "rtol": rtol}
if check_exact or rtol < 0.5e-3:
msg = """Index are different
Index values are different \\(33\\.33333 %\\)
\\[left\\]: Float64Index\\(\\[1.0, 2.0, 3.0], dtype='float64'\\)
\\[right\\]: Float64Index\\(\\[1.0, 2.0, 3.0001\\], dtype='float64'\\)"""
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, **kwargs)
else:
tm.assert_index_equal(idx1, idx2, **kwargs)
def test_index_equal_values_too_far(check_exact, rtol):
idx1 = Index([1, 2, 3])
idx2 = Index([1, 2, 4])
kwargs = {"check_exact": check_exact, "rtol": rtol}
msg = """Index are different
Index values are different \\(33\\.33333 %\\)
\\[left\\]: Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
\\[right\\]: Int64Index\\(\\[1, 2, 4\\], dtype='int64'\\)"""
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, **kwargs)
@pytest.mark.parametrize("check_order", [True, False])
def test_index_equal_value_oder_mismatch(check_exact, rtol, check_order):
idx1 = Index([1, 2, 3])
idx2 = Index([3, 2, 1])
msg = """Index are different
Index values are different \\(66\\.66667 %\\)
\\[left\\]: Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
\\[right\\]: Int64Index\\(\\[3, 2, 1\\], dtype='int64'\\)"""
if check_order:
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(
idx1, idx2, check_exact=check_exact, rtol=rtol, check_order=True
)
else:
tm.assert_index_equal(
idx1, idx2, check_exact=check_exact, rtol=rtol, check_order=False
)
def test_index_equal_level_values_mismatch(check_exact, rtol):
idx1 = MultiIndex.from_tuples([("A", 2), ("A", 2), ("B", 3), ("B", 4)])
idx2 = MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 3), ("B", 4)])
kwargs = {"check_exact": check_exact, "rtol": rtol}
msg = """MultiIndex level \\[1\\] are different
MultiIndex level \\[1\\] values are different \\(25\\.0 %\\)
\\[left\\]: Int64Index\\(\\[2, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, **kwargs)
@pytest.mark.parametrize(
"name1,name2",
[(None, "x"), ("x", "x"), (np.nan, np.nan), (NaT, NaT), (np.nan, NaT)],
)
def test_index_equal_names(name1, name2):
idx1 = Index([1, 2, 3], name=name1)
idx2 = Index([1, 2, 3], name=name2)
if name1 == name2 or name1 is name2:
tm.assert_index_equal(idx1, idx2)
else:
name1 = "'x'" if name1 == "x" else name1
name2 = "'x'" if name2 == "x" else name2
msg = f"""Index are different
Attribute "names" are different
\\[left\\]: \\[{name1}\\]
\\[right\\]: \\[{name2}\\]"""
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2)
def test_index_equal_category_mismatch(check_categorical):
msg = """Index are different
Attribute "dtype" are different
\\[left\\]: CategoricalDtype\\(categories=\\['a', 'b'\\], ordered=False\\)
\\[right\\]: CategoricalDtype\\(categories=\\['a', 'b', 'c'\\], \
ordered=False\\)"""
idx1 = Index(Categorical(["a", "b"]))
idx2 = Index(Categorical(["a", "b"], categories=["a", "b", "c"]))
if check_categorical:
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(idx1, idx2, check_categorical=check_categorical)
else:
tm.assert_index_equal(idx1, idx2, check_categorical=check_categorical)
@pytest.mark.parametrize("exact", [False, True])
def test_index_equal_range_categories(check_categorical, exact):
# GH41263
msg = """\
Index are different
Index classes are different
\\[left\\]: RangeIndex\\(start=0, stop=10, step=1\\)
\\[right\\]: Int64Index\\(\\[0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\], dtype='int64'\\)"""
rcat = CategoricalIndex(RangeIndex(10))
icat = CategoricalIndex(list(range(10)))
if check_categorical and exact:
with pytest.raises(AssertionError, match=msg):
tm.assert_index_equal(rcat, icat, check_categorical=True, exact=True)
else:
tm.assert_index_equal(
rcat, icat, check_categorical=check_categorical, exact=exact
)
def test_assert_index_equal_mixed_dtype():
# GH#39168
idx = Index(["foo", "bar", 42])
tm.assert_index_equal(idx, idx, check_order=False)
def test_assert_index_equal_ea_dtype_order_false(any_numeric_ea_dtype):
# GH#47207
idx1 = Index([1, 3], dtype=any_numeric_ea_dtype)
idx2 = Index([3, 1], dtype=any_numeric_ea_dtype)
tm.assert_index_equal(idx1, idx2, check_order=False)
def test_assert_index_equal_object_ints_order_false():
# GH#47207
idx1 = Index([1, 3], dtype="object")
idx2 = Index([3, 1], dtype="object")
tm.assert_index_equal(idx1, idx2, check_order=False)