Skip to content

Commit

Permalink
allow set_index to set a multiindex from a tuple, not just a list
Browse files Browse the repository at this point in the history
  • Loading branch information
tadamcz committed Jul 21, 2024
1 parent 18a3eec commit f5b2857
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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

0 comments on commit f5b2857

Please sign in to comment.