Skip to content

TST/REF: collect Index.equals tests #38011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,77 +381,6 @@ def test_ensure_copied_data(self, index):
result = CategoricalIndex(index.values, copy=False)
assert _base(index.values) is _base(result.values)

def test_equals_categorical(self):
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)

assert ci1.equals(ci1)
assert not ci1.equals(ci2)
assert ci1.equals(ci1.astype(object))
assert ci1.astype(object).equals(ci1)

assert (ci1 == ci1).all()
assert not (ci1 != ci1).all()
assert not (ci1 > ci1).all()
assert not (ci1 < ci1).all()
assert (ci1 <= ci1).all()
assert (ci1 >= ci1).all()

assert not (ci1 == 1).all()
assert (ci1 == Index(["a", "b"])).all()
assert (ci1 == ci1.values).all()

# invalid comparisons
with pytest.raises(ValueError, match="Lengths must match"):
ci1 == Index(["a", "b", "c"])

msg = "Categoricals can only be compared if 'categories' are the same"
with pytest.raises(TypeError, match=msg):
ci1 == ci2
with pytest.raises(TypeError, match=msg):
ci1 == Categorical(ci1.values, ordered=False)
with pytest.raises(TypeError, match=msg):
ci1 == Categorical(ci1.values, categories=list("abc"))

# tests
# make sure that we are testing for category inclusion properly
ci = CategoricalIndex(list("aabca"), categories=["c", "a", "b"])
assert not ci.equals(list("aabca"))
# Same categories, but different order
# Unordered
assert ci.equals(CategoricalIndex(list("aabca")))
# Ordered
assert not ci.equals(CategoricalIndex(list("aabca"), ordered=True))
assert ci.equals(ci.copy())

ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
assert not ci.equals(list("aabca"))
assert not ci.equals(CategoricalIndex(list("aabca")))
assert ci.equals(ci.copy())

ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
assert not ci.equals(list("aabca") + [np.nan])
assert ci.equals(CategoricalIndex(list("aabca") + [np.nan]))
assert not ci.equals(CategoricalIndex(list("aabca") + [np.nan], ordered=True))
assert ci.equals(ci.copy())

def test_equals_categorical_unordered(self):
# https://github.com/pandas-dev/pandas/issues/16603
a = CategoricalIndex(["A"], categories=["A", "B"])
b = CategoricalIndex(["A"], categories=["B", "A"])
c = CategoricalIndex(["C"], categories=["B", "A"])
assert a.equals(b)
assert not a.equals(c)
assert not b.equals(c)

def test_equals_non_category(self):
# GH#37667 Case where other contains a value not among ci's
# categories ("D") and also contains np.nan
ci = CategoricalIndex(["A", "B", np.nan, np.nan])
other = Index(["A", "B", "D", np.nan])

assert not ci.equals(other)

def test_frame_repr(self):
df = pd.DataFrame({"A": [1, 2, 3]}, index=CategoricalIndex(["a", "b", "c"]))
result = repr(df)
Expand Down
77 changes: 77 additions & 0 deletions pandas/tests/indexes/categorical/test_equals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import numpy as np
import pytest

from pandas import Categorical, CategoricalIndex, Index


class TestEquals:
def test_equals_categorical(self):
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)

assert ci1.equals(ci1)
assert not ci1.equals(ci2)
assert ci1.equals(ci1.astype(object))
assert ci1.astype(object).equals(ci1)

assert (ci1 == ci1).all()
assert not (ci1 != ci1).all()
assert not (ci1 > ci1).all()
assert not (ci1 < ci1).all()
assert (ci1 <= ci1).all()
assert (ci1 >= ci1).all()

assert not (ci1 == 1).all()
assert (ci1 == Index(["a", "b"])).all()
assert (ci1 == ci1.values).all()

# invalid comparisons
with pytest.raises(ValueError, match="Lengths must match"):
ci1 == Index(["a", "b", "c"])

msg = "Categoricals can only be compared if 'categories' are the same"
with pytest.raises(TypeError, match=msg):
ci1 == ci2
with pytest.raises(TypeError, match=msg):
ci1 == Categorical(ci1.values, ordered=False)
with pytest.raises(TypeError, match=msg):
ci1 == Categorical(ci1.values, categories=list("abc"))

# tests
# make sure that we are testing for category inclusion properly
ci = CategoricalIndex(list("aabca"), categories=["c", "a", "b"])
assert not ci.equals(list("aabca"))
# Same categories, but different order
# Unordered
assert ci.equals(CategoricalIndex(list("aabca")))
# Ordered
assert not ci.equals(CategoricalIndex(list("aabca"), ordered=True))
assert ci.equals(ci.copy())

ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
assert not ci.equals(list("aabca"))
assert not ci.equals(CategoricalIndex(list("aabca")))
assert ci.equals(ci.copy())

ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
assert not ci.equals(list("aabca") + [np.nan])
assert ci.equals(CategoricalIndex(list("aabca") + [np.nan]))
assert not ci.equals(CategoricalIndex(list("aabca") + [np.nan], ordered=True))
assert ci.equals(ci.copy())

def test_equals_categorical_unordered(self):
# https://github.com/pandas-dev/pandas/issues/16603
a = CategoricalIndex(["A"], categories=["A", "B"])
b = CategoricalIndex(["A"], categories=["B", "A"])
c = CategoricalIndex(["C"], categories=["B", "A"])
assert a.equals(b)
assert not a.equals(c)
assert not b.equals(c)

def test_equals_non_category(self):
# GH#37667 Case where other contains a value not among ci's
# categories ("D") and also contains np.nan
ci = CategoricalIndex(["A", "B", np.nan, np.nan])
other = Index(["A", "B", "D", np.nan])

assert not ci.equals(other)
22 changes: 1 addition & 21 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" generic datetimelike tests """

import numpy as np
import pytest

Expand Down Expand Up @@ -109,27 +110,6 @@ def test_getitem_preserves_freq(self):
result = index[:]
assert result.freq == index.freq

def test_not_equals_numeric(self):
index = self.create_index()

assert not index.equals(pd.Index(index.asi8))
assert not index.equals(pd.Index(index.asi8.astype("u8")))
assert not index.equals(pd.Index(index.asi8).astype("f8"))

def test_equals(self):
index = self.create_index()

assert index.equals(index.astype(object))
assert index.equals(pd.CategoricalIndex(index))
assert index.equals(pd.CategoricalIndex(index.astype(object)))

def test_not_equals_strings(self):
index = self.create_index()

other = pd.Index([str(x) for x in index], dtype=object)
assert not index.equals(other)
assert not index.equals(pd.CategoricalIndex(other))

def test_where_cast_str(self):
index = self.create_index()

Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,6 @@ def test_misc_coverage(self):
result = rng.groupby(rng.day)
assert isinstance(list(result.values())[0][0], Timestamp)

idx = DatetimeIndex(["2000-01-03", "2000-01-01", "2000-01-02"])
assert not idx.equals(list(idx))

non_datetime = Index(list("abc"))
assert not idx.equals(list(non_datetime))

def test_string_index_series_name_converted(self):
# #1644
df = DataFrame(np.random.randn(10, 4), index=date_range("1/1/2000", periods=10))
Expand Down
47 changes: 0 additions & 47 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,47 +325,6 @@ def test_nat(self, tz_naive_fixture):
assert idx.hasnans is True
tm.assert_numpy_array_equal(idx._nan_idxs, np.array([1], dtype=np.intp))

def test_equals(self):
# GH 13107
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"])
assert idx.equals(idx)
assert idx.equals(idx.copy())
assert idx.equals(idx.astype(object))
assert idx.astype(object).equals(idx)
assert idx.astype(object).equals(idx.astype(object))
assert not idx.equals(list(idx))
assert not idx.equals(Series(idx))

idx2 = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"], tz="US/Pacific")
assert not idx.equals(idx2)
assert not idx.equals(idx2.copy())
assert not idx.equals(idx2.astype(object))
assert not idx.astype(object).equals(idx2)
assert not idx.equals(list(idx2))
assert not idx.equals(Series(idx2))

# same internal, different tz
idx3 = DatetimeIndex(idx.asi8, tz="US/Pacific")
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
assert not idx.equals(idx3)
assert not idx.equals(idx3.copy())
assert not idx.equals(idx3.astype(object))
assert not idx.astype(object).equals(idx3)
assert not idx.equals(list(idx3))
assert not idx.equals(Series(idx3))

# check that we do not raise when comparing with OutOfBounds objects
oob = Index([datetime(2500, 1, 1)] * 3, dtype=object)
assert not idx.equals(oob)
assert not idx2.equals(oob)
assert not idx3.equals(oob)

# check that we do not raise when comparing with OutOfBounds dt64
oob2 = oob.map(np.datetime64)
assert not idx.equals(oob2)
assert not idx2.equals(oob2)
assert not idx3.equals(oob2)

@pytest.mark.parametrize("values", [["20180101", "20180103", "20180105"], []])
@pytest.mark.parametrize("freq", ["2D", Day(2), "2B", BDay(2), "48H", Hour(48)])
@pytest.mark.parametrize("tz", [None, "US/Eastern"])
Expand Down Expand Up @@ -429,9 +388,6 @@ def test_copy(self):
repr(cp)
tm.assert_index_equal(cp, self.rng)

def test_equals(self):
assert not self.rng.equals(list(self.rng))

def test_identical(self):
t1 = self.rng.copy()
t2 = self.rng.copy()
Expand Down Expand Up @@ -465,6 +421,3 @@ def test_copy(self):
cp = self.rng.copy()
repr(cp)
tm.assert_index_equal(cp, self.rng)

def test_equals(self):
assert not self.rng.equals(list(self.rng))
28 changes: 0 additions & 28 deletions pandas/tests/indexes/interval/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,6 @@ def index(self):
def create_index(self, closed="right"):
return IntervalIndex.from_breaks(range(11), closed=closed)

def test_equals(self, closed):
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed)
assert expected.equals(expected)
assert expected.equals(expected.copy())

assert not expected.equals(expected.astype(object))
assert not expected.equals(np.array(expected))
assert not expected.equals(list(expected))

assert not expected.equals([1, 2])
assert not expected.equals(np.array([1, 2]))
assert not expected.equals(date_range("20130101", periods=2))

expected_name1 = IntervalIndex.from_breaks(
np.arange(5), closed=closed, name="foo"
)
expected_name2 = IntervalIndex.from_breaks(
np.arange(5), closed=closed, name="bar"
)
assert expected.equals(expected_name1)
assert expected_name1.equals(expected_name2)

for other_closed in {"left", "right", "both", "neither"} - {closed}:
expected_other_closed = IntervalIndex.from_breaks(
np.arange(5), closed=other_closed
)
assert not expected.equals(expected_other_closed)

def test_repr_max_seq_item_setting(self):
# override base test: not a valid repr as we use interval notation
pass
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/indexes/interval/test_equals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np

from pandas import IntervalIndex, date_range


class TestEquals:
def test_equals(self, closed):
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed)
assert expected.equals(expected)
assert expected.equals(expected.copy())

assert not expected.equals(expected.astype(object))
assert not expected.equals(np.array(expected))
assert not expected.equals(list(expected))

assert not expected.equals([1, 2])
assert not expected.equals(np.array([1, 2]))
assert not expected.equals(date_range("20130101", periods=2))

expected_name1 = IntervalIndex.from_breaks(
np.arange(5), closed=closed, name="foo"
)
expected_name2 = IntervalIndex.from_breaks(
np.arange(5), closed=closed, name="bar"
)
assert expected.equals(expected_name1)
assert expected_name1.equals(expected_name2)

for other_closed in {"left", "right", "both", "neither"} - {closed}:
expected_other_closed = IntervalIndex.from_breaks(
np.arange(5), closed=other_closed
)
assert not expected.equals(expected_other_closed)
32 changes: 0 additions & 32 deletions pandas/tests/indexes/period/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,38 +287,6 @@ def test_nat(self):
assert idx.hasnans is True
tm.assert_numpy_array_equal(idx._nan_idxs, np.array([1], dtype=np.intp))

@pytest.mark.parametrize("freq", ["D", "M"])
def test_equals(self, freq):
# GH#13107
idx = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq=freq)
assert idx.equals(idx)
assert idx.equals(idx.copy())
assert idx.equals(idx.astype(object))
assert idx.astype(object).equals(idx)
assert idx.astype(object).equals(idx.astype(object))
assert not idx.equals(list(idx))
assert not idx.equals(Series(idx))

idx2 = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq="H")
assert not idx.equals(idx2)
assert not idx.equals(idx2.copy())
assert not idx.equals(idx2.astype(object))
assert not idx.astype(object).equals(idx2)
assert not idx.equals(list(idx2))
assert not idx.equals(Series(idx2))

# same internal, different tz
idx3 = PeriodIndex._simple_new(
idx._values._simple_new(idx._values.asi8, freq="H")
)
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
assert not idx.equals(idx3)
assert not idx.equals(idx3.copy())
assert not idx.equals(idx3.astype(object))
assert not idx.astype(object).equals(idx3)
assert not idx.equals(list(idx3))
assert not idx.equals(Series(idx3))

def test_freq_setter_deprecated(self):
# GH 20678
idx = pd.period_range("2018Q1", periods=4, freq="Q")
Expand Down
Loading