Skip to content

BUG: Bug in slicing a multi-index level with an empty-list (GH8737) #8739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.15.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Bug Fixes
- Bug in ``Categorical`` reflected comparison operator raising if the first argument was a numpy array scalar (e.g. np.int64) (:issue:`8658`)
- Bug in Panel indexing with a list-like (:issue:`8710`)
- Compat issue is ``DataFrame.dtypes`` when ``options.mode.use_inf_as_null`` is True (:issue:`8722`)

- Bug in slicing a multi-index level with an empty-list (:issue:`8737`)



Expand Down
6 changes: 4 additions & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4147,8 +4147,10 @@ def _convert_indexer(r):

# ignore not founds
continue

ranges.append(reduce(np.logical_or,indexers))
if len(k):
ranges.append(reduce(np.logical_or,indexers))
else:
ranges.append(tuple([]))

elif _is_null_slice(k):
# empty slice
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,16 @@ def f():
result = s.loc[idx[:,['foo','bah']]]
assert_series_equal(result,expected)

# GH 8737
# empty indexer
multi_index = pd.MultiIndex.from_product((['foo', 'bar', 'baz'], ['alpha', 'beta']))
df = DataFrame(np.random.randn(5, 6), index=range(5), columns=multi_index)
df = df.sortlevel(0, axis=1)

result = df.loc[:, ([], slice(None))]
expected = DataFrame(index=range(5),columns=multi_index.reindex([])[0])
assert_frame_equal(result, expected)

# regression from < 0.14.0
# GH 7914
df = DataFrame([[np.mean, np.median],['mean','median']],
Expand Down