Skip to content

Commit

Permalink
BUG: Let MultiIndex.set_levels accept any iterable (#23273) (#23291)
Browse files Browse the repository at this point in the history
  • Loading branch information
imankulov authored and jreback committed Oct 25, 2018
1 parent a20b097 commit 6b8e5e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ Indexing
- Bug in :meth:`DataFrame.loc` when indexing with an :class:`IntervalIndex` (:issue:`19977`)
- :class:`Index` no longer mangles ``None``, ``NaN`` and ``NaT``, i.e. they are treated as three different keys. However, for numeric Index all three are still coerced to a ``NaN`` (:issue:`22332`)
- Bug in `scalar in Index` if scalar is a float while the ``Index`` is of integer dtype (:issue:`22085`)
- Bug in `MultiIndex.set_levels` when levels value is not subscriptable (:issue:`23273`)

Missing
^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ def set_levels(self, levels, level=None, inplace=False,
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
names=[u'foo', u'bar'])
"""
if is_list_like(levels) and not isinstance(levels, Index):
levels = list(levels)

if level is not None and not is_list_like(level):
if not is_list_like(levels):
raise TypeError("Levels must be list-like")
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/multi/test_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,17 @@ def test_set_value_keeps_names():
df.at[('grethe', '4'), 'one'] = 99.34
assert df._is_copy is None
assert df.index.names == ('Name', 'Number')


def test_set_levels_with_iterable():
# GH23273
sizes = [1, 2, 3]
colors = ['black'] * 3
index = pd.MultiIndex.from_arrays([sizes, colors], names=['size', 'color'])

result = index.set_levels(map(int, ['3', '2', '1']), level='size')

expected_sizes = [3, 2, 1]
expected = pd.MultiIndex.from_arrays([expected_sizes, colors],
names=['size', 'color'])
tm.assert_index_equal(result, expected)

0 comments on commit 6b8e5e8

Please sign in to comment.