Skip to content

Commit 23c052d

Browse files
committed
BUG: Bug in multi-index slicing with missing indexers (GH7866)
1 parent 381a289 commit 23c052d

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
@@ -274,7 +274,7 @@ Bug Fixes
274274
- Bug in ``DatetimeIndex`` and ``PeriodIndex`` in-place addition and subtraction cause different result from normal one (:issue:`6527`)
275275
- Bug in adding and subtracting ``PeriodIndex`` with ``PeriodIndex`` raise ``TypeError`` (:issue:`7741`)
276276
- Bug in ``combine_first`` with ``PeriodIndex`` data raises ``TypeError`` (:issue:`3367`)
277-
277+
- Bug in multi-index slicing with missing indexers (:issue:`7866`)
278278

279279

280280
- 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
@@ -3638,9 +3638,17 @@ def _convert_indexer(r):
36383638
ranges.append(k)
36393639
elif com.is_list_like(k):
36403640
# a collection of labels to include from this level (these are or'd)
3641-
ranges.append(reduce(
3642-
np.logical_or,[ _convert_indexer(self._get_level_indexer(x, level=i)
3643-
) for x in k ]))
3641+
indexers = []
3642+
for x in k:
3643+
try:
3644+
indexers.append(_convert_indexer(self._get_level_indexer(x, level=i)))
3645+
except (KeyError):
3646+
3647+
# ignore not founds
3648+
continue
3649+
3650+
ranges.append(reduce(np.logical_or,indexers))
3651+
36443652
elif _is_null_slice(k):
36453653
# empty slice
36463654
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)