Skip to content

Commit 1ce8f8e

Browse files
sahildua2305jreback
authored andcommitted
ERR: Add check for input array lengths in from_arrays method (GH13599)
closes #13599 Author: Sahil Dua <[email protected]> Closes #13728 from sahildua2305/multi-from-arrays-bug and squashes the following commits: dbd3ab8 [Sahil Dua] BUG: Add check for input array lengths in from_arrays method (GH13599)
1 parent aa3ece3 commit 1ce8f8e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/whatsnew/v0.19.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ Bug Fixes
681681
- Bug in ``DataFrame.to_csv()`` in which float values were being quoted even though quotations were specified for non-numeric values only (:issue:`12922`, :issue:`13259`)
682682
- Bug in ``MultiIndex`` slicing where extra elements were returned when level is non-unique (:issue:`12896`)
683683
- Bug in ``.str.replace`` does not raise ``TypeError`` for invalid replacement (:issue:`13438`)
684+
- Bug in ``MultiIndex.from_arrays`` which didn't check for input array lengths matching (:issue:`13599`)
684685

685686

686687
- Bug in ``pd.read_csv()`` with ``engine='python'`` in which ``NaN`` values weren't being detected after data was converted to numeric values (:issue:`13314`)

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,23 @@ 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+
idx1 = []
643+
idx2 = ['a', 'b']
644+
assertRaisesRegexp(ValueError, '^all arrays must be same length$',
645+
MultiIndex.from_arrays, [idx1, idx2])
646+
647+
idx1 = [1, 2, 3]
648+
idx2 = []
649+
assertRaisesRegexp(ValueError, '^all arrays must be same length$',
650+
MultiIndex.from_arrays, [idx1, idx2])
651+
635652
def test_from_product(self):
636653

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

0 commit comments

Comments
 (0)