|
| 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) |
0 commit comments