Skip to content

Commit 56f1568

Browse files
committed
TST: Add droplevel tests for corner cases
1 parent fc6a297 commit 56f1568

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pandas/tests/indexes/test_base.py

+19
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,25 @@ def test_constructor_int_dtype_nan(self):
245245
result = Index(data, dtype='float')
246246
tm.assert_index_equal(result, expected)
247247

248+
def test_droplevel(self, indices):
249+
# GH 21115
250+
if isinstance(indices, MultiIndex):
251+
# Tested separately in test_multi.py
252+
return
253+
254+
assert indices.droplevel([]).equals(indices)
255+
256+
for level in indices.name, [indices.name]:
257+
if isinstance(indices.name, tuple) and level is indices.name:
258+
# GH 21121 : droplevel with tuple name
259+
continue
260+
with pytest.raises(ValueError):
261+
indices.droplevel(level)
262+
263+
for level in 'wrong', ['wrong']:
264+
with pytest.raises(KeyError):
265+
indices.droplevel(level)
266+
248267
@pytest.mark.parametrize("dtype", ['int64', 'uint64'])
249268
def test_constructor_int_dtype_nan_raises(self, dtype):
250269
# see gh-15187

pandas/tests/indexes/test_multi.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,7 @@ def test_droplevel_with_names(self):
20782078
expected = index.droplevel(1)
20792079
assert dropped.equals(expected)
20802080

2081-
def test_droplevel_multiple(self):
2081+
def test_droplevel_list(self):
20822082
index = MultiIndex(
20832083
levels=[Index(lrange(4)), Index(lrange(4)), Index(lrange(4))],
20842084
labels=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array(
@@ -2089,6 +2089,16 @@ def test_droplevel_multiple(self):
20892089
expected = index[:2].droplevel(2).droplevel(0)
20902090
assert dropped.equals(expected)
20912091

2092+
dropped = index[:2].droplevel([])
2093+
expected = index[:2]
2094+
assert dropped.equals(expected)
2095+
2096+
with pytest.raises(ValueError):
2097+
index[:2].droplevel(['one', 'two', 'three'])
2098+
2099+
with pytest.raises(KeyError):
2100+
index[:2].droplevel(['one', 'four'])
2101+
20922102
def test_drop_not_lexsorted(self):
20932103
# GH 12078
20942104

0 commit comments

Comments
 (0)