Skip to content

Commit

Permalink
BUG: fix for "TypeError: unorderable types" when creating MultiIndex …
Browse files Browse the repository at this point in the history
…with mixed dtypes (pandas-dev#22072)

closes pandas-dev#15457
  • Loading branch information
dickreuter authored and victor committed Sep 30, 2018
1 parent d394855 commit dded930
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Expand Up @@ -626,7 +626,7 @@ MultiIndex

- Removed compatibility for :class:`MultiIndex` pickles prior to version 0.8.0; compatibility with :class:`MultiIndex` pickles from version 0.13 forward is maintained (:issue:`21654`)
- :meth:`MultiIndex.get_loc_level` (and as a consequence, ``.loc`` on a :class:``MultiIndex``ed object) will now raise a ``KeyError``, rather than returning an empty ``slice``, if asked a label which is present in the ``levels`` but is unused (:issue:`22221`)
-
- Fix ``TypeError`` in Python 3 when creating :class:`MultiIndex` in which some levels have mixed types, e.g. when some labels are tuples (:issue:`15457`)

I/O
^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/categorical.py
Expand Up @@ -2538,7 +2538,10 @@ def _factorize_from_iterable(values):
ordered=values.ordered)
codes = values.codes
else:
cat = Categorical(values, ordered=True)
# The value of ordered is irrelevant since we don't use cat as such,
# but only the resulting categories, the order of which is independent
# from ordered. Set ordered to False as default. See GH #15457
cat = Categorical(values, ordered=False)
categories = cat.categories
codes = cat.codes
return codes, categories
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/multi/test_constructor.py
Expand Up @@ -463,3 +463,12 @@ def test_tuples_with_name_string():
pd.Index(li, name='abc')
with pytest.raises(ValueError):
pd.Index(li, name='a')


def test_from_tuples_with_tuple_label():
# GH 15457
expected = pd.DataFrame([[2, 1, 2], [4, (1, 2), 3]],
columns=['a', 'b', 'c']).set_index(['a', 'b'])
idx = pd.MultiIndex.from_tuples([(2, 1), (4, (1, 2))], names=('a', 'b'))
result = pd.DataFrame([2, 3], columns=['c'], index=idx)
tm.assert_frame_equal(expected, result)

0 comments on commit dded930

Please sign in to comment.