Skip to content

Commit bca6bd8

Browse files
simonjayhawkinsPingviinituutti
authored andcommitted
API: more consistent error message for MultiIndex.from_arrays (pandas-dev#25189)
1 parent b265a98 commit bca6bd8

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pandas/core/indexes/multi.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,17 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
324324
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
325325
names=['number', 'color'])
326326
"""
327+
error_msg = "Input must be a list / sequence of array-likes."
327328
if not is_list_like(arrays):
328-
raise TypeError("Input must be a list / sequence of array-likes.")
329+
raise TypeError(error_msg)
329330
elif is_iterator(arrays):
330331
arrays = list(arrays)
331332

333+
# Check if elements of array are list-like
334+
for array in arrays:
335+
if not is_list_like(array):
336+
raise TypeError(error_msg)
337+
332338
# Check if lengths of all arrays are equal or not,
333339
# raise ValueError, if not
334340
for i in range(1, len(arrays)):

pandas/tests/indexes/multi/test_constructor.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ def test_from_arrays_iterator(idx):
142142
MultiIndex.from_arrays(0)
143143

144144

145+
def test_from_arrays_tuples(idx):
146+
arrays = tuple(tuple(np.asarray(lev).take(level_codes))
147+
for lev, level_codes in zip(idx.levels, idx.codes))
148+
149+
# tuple of tuples as input
150+
result = MultiIndex.from_arrays(arrays, names=idx.names)
151+
tm.assert_index_equal(result, idx)
152+
153+
145154
def test_from_arrays_index_series_datetimetz():
146155
idx1 = pd.date_range('2015-01-01 10:00', freq='D', periods=3,
147156
tz='US/Eastern')
@@ -254,11 +263,13 @@ def test_from_arrays_empty():
254263

255264

256265
@pytest.mark.parametrize('invalid_sequence_of_arrays', [
257-
1, [1], [1, 2], [[1], 2], 'a', ['a'], ['a', 'b'], [['a'], 'b']])
266+
1, [1], [1, 2], [[1], 2], [1, [2]], 'a', ['a'], ['a', 'b'], [['a'], 'b'],
267+
(1,), (1, 2), ([1], 2), (1, [2]), 'a', ('a',), ('a', 'b'), (['a'], 'b'),
268+
[(1,), 2], [1, (2,)], [('a',), 'b'],
269+
((1,), 2), (1, (2,)), (('a',), 'b')
270+
])
258271
def test_from_arrays_invalid_input(invalid_sequence_of_arrays):
259-
msg = (r"Input must be a list / sequence of array-likes|"
260-
r"Input must be list-like|"
261-
r"object of type 'int' has no len\(\)")
272+
msg = "Input must be a list / sequence of array-likes"
262273
with pytest.raises(TypeError, match=msg):
263274
MultiIndex.from_arrays(arrays=invalid_sequence_of_arrays)
264275

0 commit comments

Comments
 (0)