Skip to content

Commit dbd3ab8

Browse files
committed
BUG: Add check for input array lengths in from_arrays method (GH13599)
1 parent 210fea9 commit dbd3ab8

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

doc/source/whatsnew/v0.19.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,3 +728,5 @@ Bug Fixes
728728
- Bug where ``pd.read_gbq()`` could throw ``ImportError: No module named discovery`` as a result of a naming conflict with another python package called apiclient (:issue:`13454`)
729729
- Bug in ``Index.union`` returns an incorrect result with a named empty index (:issue:`13432`)
730730
- Bugs in ``Index.difference`` and ``DataFrame.join`` raise in Python3 when using mixed-integer indexes (:issue:`13432`, :issue:`12814`)
731+
732+
- Bug in ``MultiIndex.from_arrays`` didn't check for input array lengths (:issue:`13599`)

pandas/indexes/multi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,12 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
848848
name = None if names is None else names[0]
849849
return Index(arrays[0], name=name)
850850

851+
# Check if lengths of all arrays are equal or not,
852+
# raise ValueError, if not
853+
for i in range(1, len(arrays)):
854+
if len(arrays[i]) != len(arrays[i - 1]):
855+
raise ValueError('all arrays must be same length')
856+
851857
cats = [Categorical.from_array(arr, ordered=True) for arr in arrays]
852858
levels = [c.categories for c in cats]
853859
labels = [c.codes for c in cats]

pandas/tests/indexes/test_multi.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,27 @@ def test_from_arrays_index_series_period(self):
632632

633633
tm.assert_index_equal(result, result2)
634634

635+
def test_from_arrays_different_lengths(self):
636+
# GH13599
637+
idx1 = [1, 2, 3]
638+
idx2 = ['a', 'b']
639+
assertRaisesRegexp(ValueError, '^all arrays must be same length$',
640+
MultiIndex.from_arrays, [idx1, idx2])
641+
642+
def test_from_arrays_different_lengths_first_array_zero_length(self):
643+
# GH13599
644+
idx1 = []
645+
idx2 = ['a', 'b']
646+
assertRaisesRegexp(ValueError, '^all arrays must be same length$',
647+
MultiIndex.from_arrays, [idx1, idx2])
648+
649+
def test_from_arrays_different_lengths_second_array_zero_length(self):
650+
# GH13599
651+
idx1 = [1, 2, 3]
652+
idx2 = []
653+
assertRaisesRegexp(ValueError, '^all arrays must be same length$',
654+
MultiIndex.from_arrays, [idx1, idx2])
655+
635656
def test_from_product(self):
636657

637658
first = ['foo', 'bar', 'buz']

0 commit comments

Comments
 (0)