Skip to content

Commit 3ba6cc6

Browse files
authored
TST/REF: collect Index.equals tests (#38011)
1 parent e4cf3ab commit 3ba6cc6

File tree

11 files changed

+285
-244
lines changed

11 files changed

+285
-244
lines changed

pandas/tests/indexes/categorical/test_category.py

-71
Original file line numberDiff line numberDiff line change
@@ -381,77 +381,6 @@ def test_ensure_copied_data(self, index):
381381
result = CategoricalIndex(index.values, copy=False)
382382
assert _base(index.values) is _base(result.values)
383383

384-
def test_equals_categorical(self):
385-
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
386-
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
387-
388-
assert ci1.equals(ci1)
389-
assert not ci1.equals(ci2)
390-
assert ci1.equals(ci1.astype(object))
391-
assert ci1.astype(object).equals(ci1)
392-
393-
assert (ci1 == ci1).all()
394-
assert not (ci1 != ci1).all()
395-
assert not (ci1 > ci1).all()
396-
assert not (ci1 < ci1).all()
397-
assert (ci1 <= ci1).all()
398-
assert (ci1 >= ci1).all()
399-
400-
assert not (ci1 == 1).all()
401-
assert (ci1 == Index(["a", "b"])).all()
402-
assert (ci1 == ci1.values).all()
403-
404-
# invalid comparisons
405-
with pytest.raises(ValueError, match="Lengths must match"):
406-
ci1 == Index(["a", "b", "c"])
407-
408-
msg = "Categoricals can only be compared if 'categories' are the same"
409-
with pytest.raises(TypeError, match=msg):
410-
ci1 == ci2
411-
with pytest.raises(TypeError, match=msg):
412-
ci1 == Categorical(ci1.values, ordered=False)
413-
with pytest.raises(TypeError, match=msg):
414-
ci1 == Categorical(ci1.values, categories=list("abc"))
415-
416-
# tests
417-
# make sure that we are testing for category inclusion properly
418-
ci = CategoricalIndex(list("aabca"), categories=["c", "a", "b"])
419-
assert not ci.equals(list("aabca"))
420-
# Same categories, but different order
421-
# Unordered
422-
assert ci.equals(CategoricalIndex(list("aabca")))
423-
# Ordered
424-
assert not ci.equals(CategoricalIndex(list("aabca"), ordered=True))
425-
assert ci.equals(ci.copy())
426-
427-
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
428-
assert not ci.equals(list("aabca"))
429-
assert not ci.equals(CategoricalIndex(list("aabca")))
430-
assert ci.equals(ci.copy())
431-
432-
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
433-
assert not ci.equals(list("aabca") + [np.nan])
434-
assert ci.equals(CategoricalIndex(list("aabca") + [np.nan]))
435-
assert not ci.equals(CategoricalIndex(list("aabca") + [np.nan], ordered=True))
436-
assert ci.equals(ci.copy())
437-
438-
def test_equals_categorical_unordered(self):
439-
# https://github.com/pandas-dev/pandas/issues/16603
440-
a = CategoricalIndex(["A"], categories=["A", "B"])
441-
b = CategoricalIndex(["A"], categories=["B", "A"])
442-
c = CategoricalIndex(["C"], categories=["B", "A"])
443-
assert a.equals(b)
444-
assert not a.equals(c)
445-
assert not b.equals(c)
446-
447-
def test_equals_non_category(self):
448-
# GH#37667 Case where other contains a value not among ci's
449-
# categories ("D") and also contains np.nan
450-
ci = CategoricalIndex(["A", "B", np.nan, np.nan])
451-
other = Index(["A", "B", "D", np.nan])
452-
453-
assert not ci.equals(other)
454-
455384
def test_frame_repr(self):
456385
df = pd.DataFrame({"A": [1, 2, 3]}, index=CategoricalIndex(["a", "b", "c"]))
457386
result = repr(df)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import Categorical, CategoricalIndex, Index
5+
6+
7+
class TestEquals:
8+
def test_equals_categorical(self):
9+
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
10+
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
11+
12+
assert ci1.equals(ci1)
13+
assert not ci1.equals(ci2)
14+
assert ci1.equals(ci1.astype(object))
15+
assert ci1.astype(object).equals(ci1)
16+
17+
assert (ci1 == ci1).all()
18+
assert not (ci1 != ci1).all()
19+
assert not (ci1 > ci1).all()
20+
assert not (ci1 < ci1).all()
21+
assert (ci1 <= ci1).all()
22+
assert (ci1 >= ci1).all()
23+
24+
assert not (ci1 == 1).all()
25+
assert (ci1 == Index(["a", "b"])).all()
26+
assert (ci1 == ci1.values).all()
27+
28+
# invalid comparisons
29+
with pytest.raises(ValueError, match="Lengths must match"):
30+
ci1 == Index(["a", "b", "c"])
31+
32+
msg = "Categoricals can only be compared if 'categories' are the same"
33+
with pytest.raises(TypeError, match=msg):
34+
ci1 == ci2
35+
with pytest.raises(TypeError, match=msg):
36+
ci1 == Categorical(ci1.values, ordered=False)
37+
with pytest.raises(TypeError, match=msg):
38+
ci1 == Categorical(ci1.values, categories=list("abc"))
39+
40+
# tests
41+
# make sure that we are testing for category inclusion properly
42+
ci = CategoricalIndex(list("aabca"), categories=["c", "a", "b"])
43+
assert not ci.equals(list("aabca"))
44+
# Same categories, but different order
45+
# Unordered
46+
assert ci.equals(CategoricalIndex(list("aabca")))
47+
# Ordered
48+
assert not ci.equals(CategoricalIndex(list("aabca"), ordered=True))
49+
assert ci.equals(ci.copy())
50+
51+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
52+
assert not ci.equals(list("aabca"))
53+
assert not ci.equals(CategoricalIndex(list("aabca")))
54+
assert ci.equals(ci.copy())
55+
56+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
57+
assert not ci.equals(list("aabca") + [np.nan])
58+
assert ci.equals(CategoricalIndex(list("aabca") + [np.nan]))
59+
assert not ci.equals(CategoricalIndex(list("aabca") + [np.nan], ordered=True))
60+
assert ci.equals(ci.copy())
61+
62+
def test_equals_categorical_unordered(self):
63+
# https://github.com/pandas-dev/pandas/issues/16603
64+
a = CategoricalIndex(["A"], categories=["A", "B"])
65+
b = CategoricalIndex(["A"], categories=["B", "A"])
66+
c = CategoricalIndex(["C"], categories=["B", "A"])
67+
assert a.equals(b)
68+
assert not a.equals(c)
69+
assert not b.equals(c)
70+
71+
def test_equals_non_category(self):
72+
# GH#37667 Case where other contains a value not among ci's
73+
# categories ("D") and also contains np.nan
74+
ci = CategoricalIndex(["A", "B", np.nan, np.nan])
75+
other = Index(["A", "B", "D", np.nan])
76+
77+
assert not ci.equals(other)

pandas/tests/indexes/datetimelike.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" generic datetimelike tests """
2+
23
import numpy as np
34
import pytest
45

@@ -109,27 +110,6 @@ def test_getitem_preserves_freq(self):
109110
result = index[:]
110111
assert result.freq == index.freq
111112

112-
def test_not_equals_numeric(self):
113-
index = self.create_index()
114-
115-
assert not index.equals(pd.Index(index.asi8))
116-
assert not index.equals(pd.Index(index.asi8.astype("u8")))
117-
assert not index.equals(pd.Index(index.asi8).astype("f8"))
118-
119-
def test_equals(self):
120-
index = self.create_index()
121-
122-
assert index.equals(index.astype(object))
123-
assert index.equals(pd.CategoricalIndex(index))
124-
assert index.equals(pd.CategoricalIndex(index.astype(object)))
125-
126-
def test_not_equals_strings(self):
127-
index = self.create_index()
128-
129-
other = pd.Index([str(x) for x in index], dtype=object)
130-
assert not index.equals(other)
131-
assert not index.equals(pd.CategoricalIndex(other))
132-
133113
def test_where_cast_str(self):
134114
index = self.create_index()
135115

pandas/tests/indexes/datetimes/test_datetime.py

-6
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,6 @@ def test_misc_coverage(self):
177177
result = rng.groupby(rng.day)
178178
assert isinstance(list(result.values())[0][0], Timestamp)
179179

180-
idx = DatetimeIndex(["2000-01-03", "2000-01-01", "2000-01-02"])
181-
assert not idx.equals(list(idx))
182-
183-
non_datetime = Index(list("abc"))
184-
assert not idx.equals(list(non_datetime))
185-
186180
def test_string_index_series_name_converted(self):
187181
# #1644
188182
df = DataFrame(np.random.randn(10, 4), index=date_range("1/1/2000", periods=10))

pandas/tests/indexes/datetimes/test_ops.py

-47
Original file line numberDiff line numberDiff line change
@@ -325,47 +325,6 @@ def test_nat(self, tz_naive_fixture):
325325
assert idx.hasnans is True
326326
tm.assert_numpy_array_equal(idx._nan_idxs, np.array([1], dtype=np.intp))
327327

328-
def test_equals(self):
329-
# GH 13107
330-
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"])
331-
assert idx.equals(idx)
332-
assert idx.equals(idx.copy())
333-
assert idx.equals(idx.astype(object))
334-
assert idx.astype(object).equals(idx)
335-
assert idx.astype(object).equals(idx.astype(object))
336-
assert not idx.equals(list(idx))
337-
assert not idx.equals(Series(idx))
338-
339-
idx2 = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"], tz="US/Pacific")
340-
assert not idx.equals(idx2)
341-
assert not idx.equals(idx2.copy())
342-
assert not idx.equals(idx2.astype(object))
343-
assert not idx.astype(object).equals(idx2)
344-
assert not idx.equals(list(idx2))
345-
assert not idx.equals(Series(idx2))
346-
347-
# same internal, different tz
348-
idx3 = DatetimeIndex(idx.asi8, tz="US/Pacific")
349-
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
350-
assert not idx.equals(idx3)
351-
assert not idx.equals(idx3.copy())
352-
assert not idx.equals(idx3.astype(object))
353-
assert not idx.astype(object).equals(idx3)
354-
assert not idx.equals(list(idx3))
355-
assert not idx.equals(Series(idx3))
356-
357-
# check that we do not raise when comparing with OutOfBounds objects
358-
oob = Index([datetime(2500, 1, 1)] * 3, dtype=object)
359-
assert not idx.equals(oob)
360-
assert not idx2.equals(oob)
361-
assert not idx3.equals(oob)
362-
363-
# check that we do not raise when comparing with OutOfBounds dt64
364-
oob2 = oob.map(np.datetime64)
365-
assert not idx.equals(oob2)
366-
assert not idx2.equals(oob2)
367-
assert not idx3.equals(oob2)
368-
369328
@pytest.mark.parametrize("values", [["20180101", "20180103", "20180105"], []])
370329
@pytest.mark.parametrize("freq", ["2D", Day(2), "2B", BDay(2), "48H", Hour(48)])
371330
@pytest.mark.parametrize("tz", [None, "US/Eastern"])
@@ -429,9 +388,6 @@ def test_copy(self):
429388
repr(cp)
430389
tm.assert_index_equal(cp, self.rng)
431390

432-
def test_equals(self):
433-
assert not self.rng.equals(list(self.rng))
434-
435391
def test_identical(self):
436392
t1 = self.rng.copy()
437393
t2 = self.rng.copy()
@@ -465,6 +421,3 @@ def test_copy(self):
465421
cp = self.rng.copy()
466422
repr(cp)
467423
tm.assert_index_equal(cp, self.rng)
468-
469-
def test_equals(self):
470-
assert not self.rng.equals(list(self.rng))

pandas/tests/indexes/interval/test_base.py

-28
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,6 @@ def index(self):
2121
def create_index(self, closed="right"):
2222
return IntervalIndex.from_breaks(range(11), closed=closed)
2323

24-
def test_equals(self, closed):
25-
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed)
26-
assert expected.equals(expected)
27-
assert expected.equals(expected.copy())
28-
29-
assert not expected.equals(expected.astype(object))
30-
assert not expected.equals(np.array(expected))
31-
assert not expected.equals(list(expected))
32-
33-
assert not expected.equals([1, 2])
34-
assert not expected.equals(np.array([1, 2]))
35-
assert not expected.equals(date_range("20130101", periods=2))
36-
37-
expected_name1 = IntervalIndex.from_breaks(
38-
np.arange(5), closed=closed, name="foo"
39-
)
40-
expected_name2 = IntervalIndex.from_breaks(
41-
np.arange(5), closed=closed, name="bar"
42-
)
43-
assert expected.equals(expected_name1)
44-
assert expected_name1.equals(expected_name2)
45-
46-
for other_closed in {"left", "right", "both", "neither"} - {closed}:
47-
expected_other_closed = IntervalIndex.from_breaks(
48-
np.arange(5), closed=other_closed
49-
)
50-
assert not expected.equals(expected_other_closed)
51-
5224
def test_repr_max_seq_item_setting(self):
5325
# override base test: not a valid repr as we use interval notation
5426
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
3+
from pandas import IntervalIndex, date_range
4+
5+
6+
class TestEquals:
7+
def test_equals(self, closed):
8+
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed)
9+
assert expected.equals(expected)
10+
assert expected.equals(expected.copy())
11+
12+
assert not expected.equals(expected.astype(object))
13+
assert not expected.equals(np.array(expected))
14+
assert not expected.equals(list(expected))
15+
16+
assert not expected.equals([1, 2])
17+
assert not expected.equals(np.array([1, 2]))
18+
assert not expected.equals(date_range("20130101", periods=2))
19+
20+
expected_name1 = IntervalIndex.from_breaks(
21+
np.arange(5), closed=closed, name="foo"
22+
)
23+
expected_name2 = IntervalIndex.from_breaks(
24+
np.arange(5), closed=closed, name="bar"
25+
)
26+
assert expected.equals(expected_name1)
27+
assert expected_name1.equals(expected_name2)
28+
29+
for other_closed in {"left", "right", "both", "neither"} - {closed}:
30+
expected_other_closed = IntervalIndex.from_breaks(
31+
np.arange(5), closed=other_closed
32+
)
33+
assert not expected.equals(expected_other_closed)

pandas/tests/indexes/period/test_ops.py

-32
Original file line numberDiff line numberDiff line change
@@ -287,38 +287,6 @@ def test_nat(self):
287287
assert idx.hasnans is True
288288
tm.assert_numpy_array_equal(idx._nan_idxs, np.array([1], dtype=np.intp))
289289

290-
@pytest.mark.parametrize("freq", ["D", "M"])
291-
def test_equals(self, freq):
292-
# GH#13107
293-
idx = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq=freq)
294-
assert idx.equals(idx)
295-
assert idx.equals(idx.copy())
296-
assert idx.equals(idx.astype(object))
297-
assert idx.astype(object).equals(idx)
298-
assert idx.astype(object).equals(idx.astype(object))
299-
assert not idx.equals(list(idx))
300-
assert not idx.equals(Series(idx))
301-
302-
idx2 = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq="H")
303-
assert not idx.equals(idx2)
304-
assert not idx.equals(idx2.copy())
305-
assert not idx.equals(idx2.astype(object))
306-
assert not idx.astype(object).equals(idx2)
307-
assert not idx.equals(list(idx2))
308-
assert not idx.equals(Series(idx2))
309-
310-
# same internal, different tz
311-
idx3 = PeriodIndex._simple_new(
312-
idx._values._simple_new(idx._values.asi8, freq="H")
313-
)
314-
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
315-
assert not idx.equals(idx3)
316-
assert not idx.equals(idx3.copy())
317-
assert not idx.equals(idx3.astype(object))
318-
assert not idx.astype(object).equals(idx3)
319-
assert not idx.equals(list(idx3))
320-
assert not idx.equals(Series(idx3))
321-
322290
def test_freq_setter_deprecated(self):
323291
# GH 20678
324292
idx = pd.period_range("2018Q1", periods=4, freq="Q")

0 commit comments

Comments
 (0)