Skip to content

Commit 1ce73f6

Browse files
jschendelproost
authored andcommitted
TST: Reorganize IntervalIndex tests (pandas-dev#28800)
1 parent 2c5ed40 commit 1ce73f6

File tree

4 files changed

+410
-409
lines changed

4 files changed

+410
-409
lines changed
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import IntervalIndex, Series, date_range
5+
from pandas.tests.indexes.common import Base
6+
import pandas.util.testing as tm
7+
8+
9+
class TestBase(Base):
10+
"""
11+
Tests specific to the shared common index tests; unrelated tests should be placed
12+
in test_interval.py or the specific test file (e.g. test_astype.py)
13+
"""
14+
15+
_holder = IntervalIndex
16+
17+
def setup_method(self, method):
18+
self.index = IntervalIndex.from_arrays([0, 1], [1, 2])
19+
self.index_with_nan = IntervalIndex.from_tuples([(0, 1), np.nan, (1, 2)])
20+
self.indices = dict(intervalIndex=tm.makeIntervalIndex(10))
21+
22+
def create_index(self, closed="right"):
23+
return IntervalIndex.from_breaks(range(11), closed=closed)
24+
25+
def test_equals(self, closed):
26+
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed)
27+
assert expected.equals(expected)
28+
assert expected.equals(expected.copy())
29+
30+
assert not expected.equals(expected.astype(object))
31+
assert not expected.equals(np.array(expected))
32+
assert not expected.equals(list(expected))
33+
34+
assert not expected.equals([1, 2])
35+
assert not expected.equals(np.array([1, 2]))
36+
assert not expected.equals(date_range("20130101", periods=2))
37+
38+
expected_name1 = IntervalIndex.from_breaks(
39+
np.arange(5), closed=closed, name="foo"
40+
)
41+
expected_name2 = IntervalIndex.from_breaks(
42+
np.arange(5), closed=closed, name="bar"
43+
)
44+
assert expected.equals(expected_name1)
45+
assert expected_name1.equals(expected_name2)
46+
47+
for other_closed in {"left", "right", "both", "neither"} - {closed}:
48+
expected_other_closed = IntervalIndex.from_breaks(
49+
np.arange(5), closed=other_closed
50+
)
51+
assert not expected.equals(expected_other_closed)
52+
53+
def test_repr_max_seq_item_setting(self):
54+
# override base test: not a valid repr as we use interval notation
55+
pass
56+
57+
def test_repr_roundtrip(self):
58+
# override base test: not a valid repr as we use interval notation
59+
pass
60+
61+
def test_take(self, closed):
62+
index = self.create_index(closed=closed)
63+
64+
result = index.take(range(10))
65+
tm.assert_index_equal(result, index)
66+
67+
result = index.take([0, 0, 1])
68+
expected = IntervalIndex.from_arrays([0, 0, 1], [1, 1, 2], closed=closed)
69+
tm.assert_index_equal(result, expected)
70+
71+
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
72+
def test_where(self, closed, klass):
73+
idx = self.create_index(closed=closed)
74+
cond = [True] * len(idx)
75+
expected = idx
76+
result = expected.where(klass(cond))
77+
tm.assert_index_equal(result, expected)
78+
79+
cond = [False] + [True] * len(idx[1:])
80+
expected = IntervalIndex([np.nan] + idx[1:].tolist())
81+
result = idx.where(klass(cond))
82+
tm.assert_index_equal(result, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import DataFrame, IntervalIndex, Series, Timedelta, Timestamp
5+
import pandas.util.testing as tm
6+
7+
8+
class TestIntervalIndexRendering:
9+
def test_frame_repr(self):
10+
# https://github.com/pandas-dev/pandas/pull/24134/files
11+
df = DataFrame(
12+
{"A": [1, 2, 3, 4]}, index=IntervalIndex.from_breaks([0, 1, 2, 3, 4])
13+
)
14+
result = repr(df)
15+
expected = " A\n(0, 1] 1\n(1, 2] 2\n(2, 3] 3\n(3, 4] 4"
16+
assert result == expected
17+
18+
@pytest.mark.parametrize(
19+
"constructor,expected",
20+
[
21+
(
22+
Series,
23+
(
24+
"(0.0, 1.0] a\n"
25+
"NaN b\n"
26+
"(2.0, 3.0] c\n"
27+
"dtype: object"
28+
),
29+
),
30+
(DataFrame, (" 0\n(0.0, 1.0] a\nNaN b\n(2.0, 3.0] c")),
31+
],
32+
)
33+
def test_repr_missing(self, constructor, expected):
34+
# GH 25984
35+
index = IntervalIndex.from_tuples([(0, 1), np.nan, (2, 3)])
36+
obj = constructor(list("abc"), index=index)
37+
result = repr(obj)
38+
assert result == expected
39+
40+
@pytest.mark.parametrize(
41+
"tuples, closed, expected_data",
42+
[
43+
([(0, 1), (1, 2), (2, 3)], "left", ["[0, 1)", "[1, 2)", "[2, 3)"]),
44+
(
45+
[(0.5, 1.0), np.nan, (2.0, 3.0)],
46+
"right",
47+
["(0.5, 1.0]", "NaN", "(2.0, 3.0]"],
48+
),
49+
(
50+
[
51+
(Timestamp("20180101"), Timestamp("20180102")),
52+
np.nan,
53+
((Timestamp("20180102"), Timestamp("20180103"))),
54+
],
55+
"both",
56+
["[2018-01-01, 2018-01-02]", "NaN", "[2018-01-02, 2018-01-03]"],
57+
),
58+
(
59+
[
60+
(Timedelta("0 days"), Timedelta("1 days")),
61+
(Timedelta("1 days"), Timedelta("2 days")),
62+
np.nan,
63+
],
64+
"neither",
65+
[
66+
"(0 days 00:00:00, 1 days 00:00:00)",
67+
"(1 days 00:00:00, 2 days 00:00:00)",
68+
"NaN",
69+
],
70+
),
71+
],
72+
)
73+
def test_to_native_types(self, tuples, closed, expected_data):
74+
# GH 28210
75+
index = IntervalIndex.from_tuples(tuples, closed=closed)
76+
result = index.to_native_types()
77+
expected = np.array(expected_data)
78+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)