Skip to content
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

allow set_index to set a multiindex from a tuple, not just a list #59295

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 pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5913,7 +5913,7 @@ def set_index(
"""
inplace = validate_bool_kwarg(inplace, "inplace")
self._check_inplace_and_allows_duplicate_labels(inplace)
if not isinstance(keys, list):
if not is_list_like(keys):
keys = [keys]

err_msg = (
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/copy_view/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ def test_set_index_series():
ser.iloc[0] = 100
tm.assert_index_equal(df.index, expected)

class TestSetMultiIndex:
df = DataFrame({"a": [1, 2], "b": 1.5, "c": [3, 4]})

def test_from_list(self):
df = self.df.set_index(["a", "b"])
self._assert(df)

def test_from_tuple(self):
df = self.df.set_index(("a", "b"))
self._assert(df)

def _assert(self, df):
assert isinstance(df.index, pd.MultiIndex)
assert df.index.names == ["a", "b"]


def test_assign_index_as_series():
df = DataFrame({"a": [1, 2], "b": 1.5})
Expand Down
Loading