| 586 | |
| 587 | class TestGetLoc: |
| 588 | def test_get_loc(self, idx): |
| 589 | assert idx.get_loc(("foo", "two")) == 1 |
| 590 | assert idx.get_loc(("baz", "two")) == 3 |
| 591 | with pytest.raises(KeyError, match=r"^\('bar', 'two'\)$"): |
| 592 | idx.get_loc(("bar", "two")) |
| 593 | with pytest.raises(KeyError, match=r"^'quux'$"): |
| 594 | idx.get_loc("quux") |
| 595 | |
| 596 | # 3 levels |
| 597 | index = MultiIndex( |
| 598 | levels=[Index(np.arange(4)), Index(np.arange(4)), Index(np.arange(4))], |
| 599 | codes=[ |
| 600 | np.array([0, 0, 1, 2, 2, 2, 3, 3]), |
| 601 | np.array([0, 1, 0, 0, 0, 1, 0, 1]), |
| 602 | np.array([1, 0, 1, 1, 0, 0, 1, 0]), |
| 603 | ], |
| 604 | ) |
| 605 | with pytest.raises(KeyError, match=r"^\(1, 1\)$"): |
| 606 | index.get_loc((1, 1)) |
| 607 | assert index.get_loc((2, 0)) == slice(3, 5) |
| 608 | |
| 609 | def test_get_loc_duplicates(self): |
| 610 | index = Index([2, 2, 2, 2]) |