def test_get_loc(self, idx): assert idx.get_loc(("foo", "two")) == 1 assert idx.get_loc(("baz", "two")) == 3 with pytest.raises(KeyError, match=r"^10$"): idx.get_loc(("bar", "two")) with pytest.raises(KeyError, match=r"^'quux'$"): idx.get_loc("quux") msg = "only the default get_loc method is currently supported for MultiIndex" with pytest.raises(NotImplementedError, match=msg): idx.get_loc("foo", method="nearest") # 3 levels index = MultiIndex( levels=[ Index(np.arange(4)), Index(np.arange(4)), Index(np.arange(4)) ], codes=[ np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array([0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0]), ], ) with pytest.raises(KeyError, match=r"^\(1, 1\)$"): index.get_loc((1, 1)) assert index.get_loc((2, 0)) == slice(3, 5)
def test_get_loc(idx): assert idx.get_loc(('foo', 'two')) == 1 assert idx.get_loc(('baz', 'two')) == 3 with pytest.raises(KeyError, match=r"^10$"): idx.get_loc(('bar', 'two')) with pytest.raises(KeyError, match=r"^'quux'$"): idx.get_loc('quux') msg = ("only the default get_loc method is currently supported for" " MultiIndex") with pytest.raises(NotImplementedError, match=msg): idx.get_loc('foo', method='nearest') # 3 levels index = MultiIndex( levels=[Index(lrange(4)), Index(lrange(4)), Index(lrange(4))], codes=[ np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array([0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0]) ]) with pytest.raises(KeyError, match=r"^\(1, 1\)$"): index.get_loc((1, 1)) assert index.get_loc((2, 0)) == slice(3, 5)
def test_get_value_duplicates(): index = MultiIndex( levels=[["D", "B", "C"], [0, 26, 27, 37, 57, 67, 75, 82]], codes=[[0, 0, 0, 1, 2, 2, 2, 2, 2, 2], [1, 3, 4, 6, 0, 2, 2, 3, 5, 7]], names=["tag", "day"], ) assert index.get_loc("D") == slice(0, 3)
def test_get_loc_duplicates2(self): # TODO: de-duplicate with test_get_loc_duplicates above? index = MultiIndex( levels=[["D", "B", "C"], [0, 26, 27, 37, 57, 67, 75, 82]], codes=[[0, 0, 0, 1, 2, 2, 2, 2, 2, 2], [1, 3, 4, 6, 0, 2, 2, 3, 5, 7]], names=["tag", "day"], ) assert index.get_loc("D") == slice(0, 3)
def test_get_value_duplicates(): index = MultiIndex( levels=[["D", "B", "C"], [0, 26, 27, 37, 57, 67, 75, 82]], codes=[[0, 0, 0, 1, 2, 2, 2, 2, 2, 2], [1, 3, 4, 6, 0, 2, 2, 3, 5, 7]], names=["tag", "day"], ) assert index.get_loc("D") == slice(0, 3) with pytest.raises(KeyError, match=r"^'D'$"): index._engine.get_value(np.array([]), "D")
def test_get_value_duplicates(): index = MultiIndex(levels=[['D', 'B', 'C'], [0, 26, 27, 37, 57, 67, 75, 82]], codes=[[0, 0, 0, 1, 2, 2, 2, 2, 2, 2], [1, 3, 4, 6, 0, 2, 2, 3, 5, 7]], names=['tag', 'day']) assert index.get_loc('D') == slice(0, 3) with pytest.raises(KeyError, match=r"^'D'$"): index._engine.get_value(np.array([]), 'D')
def test_get_value_duplicates(): index = MultiIndex(levels=[['D', 'B', 'C'], [0, 26, 27, 37, 57, 67, 75, 82]], labels=[[0, 0, 0, 1, 2, 2, 2, 2, 2, 2], [1, 3, 4, 6, 0, 2, 2, 3, 5, 7]], names=['tag', 'day']) assert index.get_loc('D') == slice(0, 3) with pytest.raises(KeyError): index._engine.get_value(np.array([]), 'D')
def test_get_loc(idx): assert idx.get_loc(('foo', 'two')) == 1 assert idx.get_loc(('baz', 'two')) == 3 with pytest.raises(KeyError, match=r"^10$"): idx.get_loc(('bar', 'two')) with pytest.raises(KeyError, match=r"^'quux'$"): idx.get_loc('quux') msg = ("only the default get_loc method is currently supported for" " MultiIndex") with pytest.raises(NotImplementedError, match=msg): idx.get_loc('foo', method='nearest') # 3 levels index = MultiIndex(levels=[Index(lrange(4)), Index(lrange(4)), Index( lrange(4))], codes=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array( [0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0])]) with pytest.raises(KeyError, match=r"^\(1, 1\)$"): index.get_loc((1, 1)) assert index.get_loc((2, 0)) == slice(3, 5)
def test_get_loc_past_lexsort_depth(self): # GH#30053 idx = MultiIndex( levels=[["a"], [0, 7], [1]], codes=[[0, 0], [1, 0], [0, 0]], names=["x", "y", "z"], sortorder=0, ) key = ("a", 7) with tm.assert_produces_warning(PerformanceWarning): # PerformanceWarning: indexing past lexsort depth may impact performance result = idx.get_loc(key) assert result == slice(0, 1, None)
def test_get_loc(idx): assert idx.get_loc(('foo', 'two')) == 1 assert idx.get_loc(('baz', 'two')) == 3 pytest.raises(KeyError, idx.get_loc, ('bar', 'two')) pytest.raises(KeyError, idx.get_loc, 'quux') pytest.raises(NotImplementedError, idx.get_loc, 'foo', method='nearest') # 3 levels index = MultiIndex(levels=[Index(lrange(4)), Index(lrange(4)), Index( lrange(4))], labels=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array( [0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0])]) pytest.raises(KeyError, index.get_loc, (1, 1)) assert index.get_loc((2, 0)) == slice(3, 5)