Skip to content

Commit 1c94949

Browse files
committed
changes as per comments
1 parent 389b819 commit 1c94949

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ MultiIndex
626626

627627
- 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`)
628628
- :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`)
629-
-
629+
- Fix issue when creating :class:`MultiIndex` from mixed type arrays including tuples. In some cases df.set_index(['A','B']) raised a ``TypeError unorderable types`` when trying to set multiple columns as indices. (:issue:'15457')
630630

631631
I/O
632632
^^^

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2540,7 +2540,7 @@ def _factorize_from_iterable(values):
25402540
else:
25412541
# The value of ordered is irrelevant since we don't use cat as such,
25422542
# but only the resulting categories, the order of which is independent
2543-
# from ordered. Set ordered to False as default. See GH #15457"
2543+
# from ordered. Set ordered to False as default. See GH #15457
25442544
cat = Categorical(values, ordered=False)
25452545
categories = cat.categories
25462546
codes = cat.codes

pandas/tests/frame/test_alter_axes.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def test_set_index_cast_datetimeindex(self):
261261
comp = comp.tz_localize(None)
262262
tm.assert_numpy_array_equal(result.values, comp.values)
263263

264-
# list of datetimes with a tz
264+
# list of datetimes with a tzg
265265
df['D'] = i.to_pydatetime()
266266
result = df['D']
267267
assert_series_equal(result, expected, check_names=False)
@@ -392,12 +392,6 @@ def test_dti_set_index_reindex(self):
392392

393393
assert new_index.freq == index.freq
394394

395-
def test_set_index_tuple(self):
396-
# test for fix of issue #15457, where the following raised a TypeError
397-
expected = pd.DataFrame([[2, 1], [4, (1, 2)]]).set_index([0, 1]).index
398-
result = pd.MultiIndex.from_tuples([(2, 1), (4, (1, 2))], names=(0, 1))
399-
tm.assert_index_equal(expected, result)
400-
401395
# Renaming
402396

403397
def test_rename(self):

pandas/tests/indexes/multi/test_constructor.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import re
44

55
import numpy as np
6+
import pytest
7+
from pandas._libs.tslib import Timestamp
8+
69
import pandas as pd
710
import pandas.util.testing as tm
8-
import pytest
911
from pandas import Index, MultiIndex, date_range
10-
from pandas._libs.tslib import Timestamp
1112
from pandas.compat import lrange, range
1213
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
1314

@@ -23,7 +24,7 @@ def test_constructor_single_level():
2324

2425
def test_constructor_no_levels():
2526
tm.assert_raises_regex(ValueError, "non-zero number "
26-
"of levels/labels",
27+
"of levels/labels",
2728
MultiIndex, levels=[], labels=[])
2829
both_re = re.compile('Must pass both levels and labels')
2930
with tm.assert_raises_regex(TypeError, both_re):
@@ -56,7 +57,7 @@ def test_constructor_mismatched_label_levels(idx):
5657
labels = [np.array([1]), np.array([2]), np.array([3])]
5758
levels = ["a"]
5859
tm.assert_raises_regex(ValueError, "Length of levels and labels "
59-
"must be the same", MultiIndex,
60+
"must be the same", MultiIndex,
6061
levels=levels, labels=labels)
6162
length_error = re.compile('>= length of level')
6263
label_error = re.compile(r'Unequal label lengths: \[4, 2\]')
@@ -259,13 +260,13 @@ def test_from_arrays_invalid_input(invalid_array):
259260
def test_from_arrays_different_lengths(idx1, idx2):
260261
# see gh-13599
261262
tm.assert_raises_regex(ValueError, '^all arrays must '
262-
'be same length$',
263+
'be same length$',
263264
MultiIndex.from_arrays, [idx1, idx2])
264265

265266

266267
def test_from_tuples():
267268
tm.assert_raises_regex(TypeError, 'Cannot infer number of levels '
268-
'from empty list',
269+
'from empty list',
269270
MultiIndex.from_tuples, [])
270271

271272
expected = MultiIndex(levels=[[1, 3], [2, 4]],
@@ -390,7 +391,6 @@ def test_from_product_index_series_categorical(ordered, f):
390391

391392

392393
def test_from_product():
393-
394394
first = ['foo', 'bar', 'buz']
395395
second = ['a', 'b', 'c']
396396
names = ['first', 'second']
@@ -425,7 +425,6 @@ def test_from_product_iterator():
425425

426426

427427
def test_create_index_existing_name(idx):
428-
429428
# GH11193, when an existing index is passed, and a new name is not
430429
# specified, the new index should inherit the previous object name
431430
index = idx
@@ -463,3 +462,11 @@ def test_tuples_with_name_string():
463462
pd.Index(li, name='abc')
464463
with pytest.raises(ValueError):
465464
pd.Index(li, name='a')
465+
466+
467+
def test_from_tuples_with_tuple_label():
468+
# test for fix of issue #15457, where the following raised a TypeError
469+
expected = pd.DataFrame([[2, 1, 2], [4, (1, 2), 3]], columns=['a', 'b', 'c']).set_index(['a', 'b'])
470+
idx = pd.MultiIndex.from_tuples([(2, 1), (4, (1, 2))], names=('a', 'b'))
471+
result = pd.DataFrame([2, 3], columns=['c'], index=idx)
472+
tm.assert_frame_equal(expected, result)

0 commit comments

Comments
 (0)