Skip to content

BUG: Add check for array lengths in from_arrays method (GH13599) #13633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ff5ef13
BUG: Add check for array lengths in from_arrays method (GH13599)
sahildua2305 Jul 12, 2016
db98e32
BUG: Add test for array length mismatch
sahildua2305 Jul 13, 2016
5e7bd92
BUG: Fix minor issue with new test for from_arrays
sahildua2305 Jul 13, 2016
27d2915
CLN: Fix compile time warnings
yui-knk Jul 13, 2016
06103dd
Pin IPython for doc build to 4.x (see #13639)
jorisvandenbossche Jul 13, 2016
7dd4091
CLN: reorg type inference & introspection
jreback Jul 13, 2016
20de266
BLD: included pandas.api.* in setup.py (#13640)
gfyoung Jul 13, 2016
4a9e66e
Minor fix for linter
sahildua2305 Jul 13, 2016
94b829d
Update whatsnew entry
sahildua2305 Jul 13, 2016
44f3229
DOC/BLD: pin IPython version to 4.2.0 (#13639) (#13647)
jorisvandenbossche Jul 14, 2016
6f0a020
TST: reorganize tools.tests (#13619)
sinhrks Jul 14, 2016
a711b42
BF(TST): allow AttributeError being raised (in addition to TypeError)…
yarikoptic Jul 14, 2016
084ceae
API, DEPR: Raise and Deprecate Reshape for Pandas Objects
gfyoung Jul 14, 2016
3f6d4bd
CLN: Fix compile time warnings
yui-knk Jul 14, 2016
c9a27ed
CLN: fix some issues in asv benchmark suite (#13630)
jorisvandenbossche Jul 14, 2016
05b976c
TST: add tests for Timestamp.toordinal/fromordinal
sinhrks Jul 15, 2016
71a0675
CLN: Initialization coincides with mapping, hence with uniqueness check
toobaz Jul 15, 2016
0a70b5f
API: Change Period('NAT') to return NaT
sinhrks Jul 15, 2016
1bee56e
BUG: construction of Series with integers on windows not default to i…
jreback Jul 15, 2016
d7c028d
CLN: Removed levels attribute from Categorical
gfyoung Jul 15, 2016
91691de
Fix minor typo
sahildua2305 Jul 16, 2016
043879f
DOC: Add reference of DataFrame.rename_axis and Series.rename_axis to…
shawnheide Jul 17, 2016
76d7e77
DOC: correct template for .cum* descriptions (#13683)
shawnheide Jul 17, 2016
ada6bf3
DOC: fix a keyword coerce in array_to_timedelta64 (#13686)
yui-knk Jul 17, 2016
6b9cd15
TST: assert message shows unnecessary diff (#13676)
sinhrks Jul 18, 2016
694fe61
ENH: Series.append now has ignore_index kw
sinhrks Jul 19, 2016
5a52171
BUG: Add type check for width parameter in str.pad method GH13598
wcwagner Jul 19, 2016
9f635cd
BUG: Cast a key to NaT before get loc from Index
yui-knk Jul 19, 2016
b054536
BUG: merge_asof not handling allow_exact_matches and tolerance on fir…
jreback Jul 19, 2016
361a2b4
CLN: removed pandas.sandbox
gfyoung Jul 19, 2016
1e1e9b3
DEPR: Remove legacy offsets
sinhrks Jul 19, 2016
006bd0b
CLN: removed setter method of categorical's ordered attribute
gfyoung Jul 19, 2016
b225cac
BUG/PERF: Sort mixed-int in Py3, fix Index.difference
pijucha Jul 19, 2016
fafef5d
ENH: Add support for writing variable labels to Stata files
bashtage Jul 19, 2016
506520b
API: Index doesn't results in PeriodIndex if Period contains NaT
sinhrks Jul 19, 2016
31c2e5f
PERF: improve DTI string parse
sinhrks Jul 19, 2016
4c9ae94
DOC: resample warnings
chris-b1 Jul 19, 2016
8acfad3
CLN: Removed the flavor='mysql' option and deprecate flavor in DataFr…
gfyoung Jul 19, 2016
786edc7
ENH: add time-window capability to .rolling
jreback Jul 20, 2016
57b373c
CLN: Remove a test case about Timestamp to TestTimestamp (#13722)
yui-knk Jul 20, 2016
b25a2a1
DOC/DEPR: pivot_annual
sinhrks Jul 20, 2016
016b352
PERF: Improve Period hashing
sinhrks Jul 20, 2016
4962131
MAINT: Removed some warnings in tests
gfyoung Jul 20, 2016
634e95d
CLN: removed the 'diff' method for Index
gfyoung Jul 20, 2016
a2e1917
BUG: Add check for array lengths in from_arrays method (GH13599)
sahildua2305 Jul 12, 2016
e401cf1
BUG: Add test for array length mismatch
sahildua2305 Jul 13, 2016
93296ff
BUG: Fix minor issue with new test for from_arrays
sahildua2305 Jul 13, 2016
72fb52d
Minor fix for linter
sahildua2305 Jul 13, 2016
57d5250
Update whatsnew entry
sahildua2305 Jul 13, 2016
bb6a952
Fix minor typo
sahildua2305 Jul 16, 2016
6ff0ce1
Remove minor typo
sahildua2305 Jul 20, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,12 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
name = None if names is None else names[0]
return Index(arrays[0], name=name)

# Check if lengths of all arrays are equal or not,
# raise ValueError, if not
for i in range(1, len(arrays)):
if len(arrays[i]) != len(arrays[i-1]):
raise ValueError('all arrays must be same length')

cats = [Categorical.from_array(arr, ordered=True) for arr in arrays]
levels = [c.categories for c in cats]
labels = [c.codes for c in cats]
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,13 @@ def test_from_arrays_index_series_period(self):

tm.assert_index_equal(result, result2)

def test_from_arrays_different_lengths(self):
# GH13599
idx1 = [1, 2, 3]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a positive example (e.g. it doesn't raise)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if I can assert explicitly that the ValueError isn't raised.

Should I simply add an example where array lengths are same and assert the response is the MultiIndex object with valid data?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you simply construct an expected MultiIndex and use .equals that it is equal to a result that uses .from_arrays. I am sure there are other examples to model from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback is that test really required? The same input array length case is anyway getting checked in all other from_arrays related tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better yet make sure to check with a 0-len array (check for all 2 cases, idx1 or idx2 is 0-len)

idx2 = ['a', 'b']
assertRaisesRegexp(ValueError, '^all arrays must be same length$',
MultiIndex.from_arrays, [idx1, idx2])

def test_from_product(self):

first = ['foo', 'bar', 'buz']
Expand Down