Skip to content

Commit 0bd7abc

Browse files
committed
Merge pull request #7867 from jreback/mi_loc
BUG: Bug in multi-index slicing with missing indexers (GH7866)
2 parents c3d31c7 + 23c052d commit 0bd7abc

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ Bug Fixes
285285
- Bug in ``DatetimeIndex`` and ``PeriodIndex`` in-place addition and subtraction cause different result from normal one (:issue:`6527`)
286286
- Bug in adding and subtracting ``PeriodIndex`` with ``PeriodIndex`` raise ``TypeError`` (:issue:`7741`)
287287
- Bug in ``combine_first`` with ``PeriodIndex`` data raises ``TypeError`` (:issue:`3367`)
288-
288+
- Bug in multi-index slicing with missing indexers (:issue:`7866`)
289289

290290

291291
- Bug in pickles contains ``DateOffset`` may raise ``AttributeError`` when ``normalize`` attribute is reffered internally (:issue:`7748`)

pandas/core/index.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -3774,9 +3774,17 @@ def _convert_indexer(r):
37743774
ranges.append(k)
37753775
elif com.is_list_like(k):
37763776
# a collection of labels to include from this level (these are or'd)
3777-
ranges.append(reduce(
3778-
np.logical_or,[ _convert_indexer(self._get_level_indexer(x, level=i)
3779-
) for x in k ]))
3777+
indexers = []
3778+
for x in k:
3779+
try:
3780+
indexers.append(_convert_indexer(self._get_level_indexer(x, level=i)))
3781+
except (KeyError):
3782+
3783+
# ignore not founds
3784+
continue
3785+
3786+
ranges.append(reduce(np.logical_or,indexers))
3787+
37803788
elif _is_null_slice(k):
37813789
# empty slice
37823790
pass

pandas/tests/test_indexing.py

+33
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,39 @@ def f():
19411941
df.val['X']
19421942
self.assertRaises(KeyError, f)
19431943

1944+
1945+
# GH 7866
1946+
# multi-index slicing with missing indexers
1947+
s = pd.Series(np.arange(9),
1948+
index=pd.MultiIndex.from_product([['A','B','C'],['foo','bar','baz']],
1949+
names=['one','two'])
1950+
).sortlevel()
1951+
1952+
expected = pd.Series(np.arange(3),
1953+
index=pd.MultiIndex.from_product([['A'],['foo','bar','baz']],
1954+
names=['one','two'])
1955+
).sortlevel()
1956+
1957+
result = s.loc[['A']]
1958+
assert_series_equal(result,expected)
1959+
result = s.loc[['A','D']]
1960+
assert_series_equal(result,expected)
1961+
1962+
# empty series
1963+
result = s.loc[['D']]
1964+
expected = s.loc[[]]
1965+
assert_series_equal(result,expected)
1966+
1967+
idx = pd.IndexSlice
1968+
expected = pd.Series([0,3,6],
1969+
index=pd.MultiIndex.from_product([['A','B','C'],['foo']],
1970+
names=['one','two'])
1971+
).sortlevel()
1972+
result = s.loc[idx[:,['foo']]]
1973+
assert_series_equal(result,expected)
1974+
result = s.loc[idx[:,['foo','bah']]]
1975+
assert_series_equal(result,expected)
1976+
19441977
def test_setitem_dtype_upcast(self):
19451978

19461979
# GH3216

0 commit comments

Comments
 (0)