Skip to content

Commit 1887ba5

Browse files
committed
BUG: Bug in slicing a multi-index level with an empty-list (GH8737)
1 parent 0f72d3d commit 1887ba5

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

doc/source/whatsnew/v0.15.1.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Bug Fixes
257257
- Bug in Panel indexing with a list-like (:issue:`8710`)
258258
- Compat issue is ``DataFrame.dtypes`` when ``options.mode.use_inf_as_null`` is True (:issue:`8722`)
259259
- Bug in ``read_csv``, ``dialect`` parameter would not take a string (:issue: `8703`)
260-
260+
- Bug in slicing a multi-index level with an empty-list (:issue:`8737`)
261261

262262

263263

@@ -300,4 +300,3 @@ Bug Fixes
300300
- Bug in ``date_range`` where partially-specified dates would incorporate current date (:issue:`6961`)
301301

302302
- Bug in Setting by indexer to a scalar value with a mixed-dtype `Panel4d` was failing (:issue:`8702`)
303-

pandas/core/index.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4147,8 +4147,10 @@ def _convert_indexer(r):
41474147

41484148
# ignore not founds
41494149
continue
4150-
4151-
ranges.append(reduce(np.logical_or,indexers))
4150+
if len(k):
4151+
ranges.append(reduce(np.logical_or,indexers))
4152+
else:
4153+
ranges.append(tuple([]))
41524154

41534155
elif _is_null_slice(k):
41544156
# empty slice

pandas/tests/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,16 @@ def f():
21332133
result = s.loc[idx[:,['foo','bah']]]
21342134
assert_series_equal(result,expected)
21352135

2136+
# GH 8737
2137+
# empty indexer
2138+
multi_index = pd.MultiIndex.from_product((['foo', 'bar', 'baz'], ['alpha', 'beta']))
2139+
df = DataFrame(np.random.randn(5, 6), index=range(5), columns=multi_index)
2140+
df = df.sortlevel(0, axis=1)
2141+
2142+
result = df.loc[:, ([], slice(None))]
2143+
expected = DataFrame(index=range(5),columns=multi_index.reindex([])[0])
2144+
assert_frame_equal(result, expected)
2145+
21362146
# regression from < 0.14.0
21372147
# GH 7914
21382148
df = DataFrame([[np.mean, np.median],['mean','median']],

0 commit comments

Comments
 (0)