Skip to content

BUG: raise when wrong level name is passed to "unstack" #27631

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

Merged
merged 1 commit into from
Jul 29, 2019
Merged
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.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^

-
- A ``KeyError`` is now raised if ``.unstack()`` is called on a :class:`Series` or :class:`DataFrame` with a flat :class:`Index` passing a name which is not the correct one (:issue:`18303`)
-
-

Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,11 @@ def _validate_index_level(self, level):
"Too many levels:" " Index has only 1 level, not %d" % (level + 1)
)
elif level != self.name:
raise KeyError("Level %s must be same as name (%s)" % (level, self.name))
raise KeyError(
"Requested level ({}) does not match index name ({})".format(
level, self.name
)
)

def _get_level_number(self, level):
self._validate_index_level(level)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ensure_platform_int,
is_bool_dtype,
is_extension_array_dtype,
is_integer,
is_integer_dtype,
is_list_like,
is_object_dtype,
Expand Down Expand Up @@ -402,6 +403,10 @@ def unstack(obj, level, fill_value=None):
else:
level = level[0]

# Prioritize integer interpretation (GH #21677):
if not is_integer(level) and not level == "__placeholder__":
level = obj.index._get_level_number(level)

if isinstance(obj, DataFrame):
if isinstance(obj.index, MultiIndex):
return _unstack_frame(obj, level, fill_value=fill_value)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ def test_reset_index_level(self):

# Missing levels - for both MultiIndex and single-level Index:
for idx_lev in ["A", "B"], ["A"]:
with pytest.raises(KeyError, match="Level E "):
with pytest.raises(KeyError, match=r"(L|l)evel \(?E\)?"):
df.set_index(idx_lev).reset_index(level=["A", "E"])
with pytest.raises(IndexError, match="Too many levels"):
df.set_index(idx_lev).reset_index(level=[0, 1, 2])
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ def test_isin_level_kwarg_bad_label_raises(self, label, indices):
msg = "'Level {} not found'"
else:
index = index.rename("foo")
msg = r"'Level {} must be same as name \(foo\)'"
msg = r"Requested level \({}\) does not match index name \(foo\)"
with pytest.raises(KeyError, match=msg.format(label)):
index.isin([], level=label)

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def test_droplevel(self, indices):

for level in "wrong", ["wrong"]:
with pytest.raises(
KeyError, match=re.escape("'Level wrong must be same as name (None)'")
KeyError,
match=r"'Requested level \(wrong\) does not match index name \(None\)'",
):
indices.droplevel(level)

Expand Down Expand Up @@ -200,7 +201,7 @@ def test_unique(self, indices):
with pytest.raises(IndexError, match=msg):
indices.unique(level=3)

msg = r"Level wrong must be same as name \({}\)".format(
msg = r"Requested level \(wrong\) does not match index name \({}\)".format(
re.escape(indices.name.__repr__())
)
with pytest.raises(KeyError, match=msg):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ def test_reset_index_drop_errors(self):

# KeyError raised for series index when passed level name is missing
s = Series(range(4))
with pytest.raises(KeyError, match="must be same as name"):
with pytest.raises(KeyError, match="does not match index name"):
s.reset_index("wrong", drop=True)
with pytest.raises(KeyError, match="must be same as name"):
with pytest.raises(KeyError, match="does not match index name"):
s.reset_index("wrong")

# KeyError raised for series when level to be dropped is missing
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,22 @@ def test_stack_unstack_preserve_names(self):
restacked = unstacked.stack()
assert restacked.index.names == self.frame.index.names

@pytest.mark.parametrize("method", ["stack", "unstack"])
def test_stack_unstack_wrong_level_name(self, method):
# GH 18303 - wrong level name should raise

# A DataFrame with flat axes:
df = self.frame.loc["foo"]

with pytest.raises(KeyError, match="does not match index name"):
getattr(df, method)("mistake")

if method == "unstack":
# Same on a Series:
s = df.iloc[:, 0]
with pytest.raises(KeyError, match="does not match index name"):
getattr(s, method)("mistake")

def test_unstack_level_name(self):
result = self.frame.unstack("second")
expected = self.frame.unstack(level=1)
Expand Down