test_base.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import numpy as np
  2. import pytest
  3. from pandas import IntervalIndex
  4. import pandas._testing as tm
  5. from pandas.tests.indexes.common import Base
  6. class TestBase(Base):
  7. """
  8. Tests specific to the shared common index tests; unrelated tests should be placed
  9. in test_interval.py or the specific test file (e.g. test_astype.py)
  10. """
  11. _index_cls = IntervalIndex
  12. @pytest.fixture
  13. def simple_index(self) -> IntervalIndex:
  14. return self._index_cls.from_breaks(range(11), closed="right")
  15. @pytest.fixture
  16. def index(self):
  17. return tm.makeIntervalIndex(10)
  18. def create_index(self, *, closed="right"):
  19. return IntervalIndex.from_breaks(range(11), closed=closed)
  20. def test_repr_max_seq_item_setting(self):
  21. # override base test: not a valid repr as we use interval notation
  22. pass
  23. def test_repr_roundtrip(self):
  24. # override base test: not a valid repr as we use interval notation
  25. pass
  26. def test_take(self, closed):
  27. index = self.create_index(closed=closed)
  28. result = index.take(range(10))
  29. tm.assert_index_equal(result, index)
  30. result = index.take([0, 0, 1])
  31. expected = IntervalIndex.from_arrays([0, 0, 1], [1, 1, 2], closed=closed)
  32. tm.assert_index_equal(result, expected)
  33. def test_where(self, simple_index, listlike_box):
  34. klass = listlike_box
  35. idx = simple_index
  36. cond = [True] * len(idx)
  37. expected = idx
  38. result = expected.where(klass(cond))
  39. tm.assert_index_equal(result, expected)
  40. cond = [False] + [True] * len(idx[1:])
  41. expected = IntervalIndex([np.nan] + idx[1:].tolist())
  42. result = idx.where(klass(cond))
  43. tm.assert_index_equal(result, expected)
  44. def test_getitem_2d_deprecated(self, simple_index):
  45. # GH#30588 multi-dim indexing is deprecated, but raising is also acceptable
  46. idx = simple_index
  47. with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
  48. idx[:, None]
  49. with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
  50. # GH#44051
  51. idx[True]
  52. with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
  53. # GH#44051
  54. idx[False]