-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
API: more consistent error message for MultiIndex.from_arrays #25189
Merged
WillAyd
merged 7 commits into
pandas-dev:master
from
simonjayhawkins:mi-constructor-error
Feb 20, 2019
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0974cee
more consistent error message for MultiIndex.from_arrays
simonjayhawkins 06cb448
add additional test case for loop check
simonjayhawkins 1438459
own loop for sequence element list-like check
simonjayhawkins 84993d7
test with tuples for invalid input
simonjayhawkins 0974c3b
test with tuples for valid input
simonjayhawkins 96eeed2
Merge remote-tracking branch 'upstream/master' into mi-constructor-error
simonjayhawkins 403789a
Merge remote-tracking branch 'upstream/master' into mi-constructor-error
simonjayhawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,20 +324,26 @@ def from_arrays(cls, arrays, sortorder=None, names=None): | |
codes=[[0, 0, 1, 1], [1, 0, 1, 0]], | ||
names=['number', 'color']) | ||
""" | ||
error_msg = "Input must be a list / sequence of array-likes." | ||
if not is_list_like(arrays): | ||
raise TypeError("Input must be a list / sequence of array-likes.") | ||
raise TypeError(error_msg) | ||
elif is_iterator(arrays): | ||
arrays = list(arrays) | ||
|
||
# Check if lengths of all arrays are equal or not, | ||
# raise ValueError, if not | ||
for i in range(1, len(arrays)): | ||
if not is_list_like(arrays[i]): | ||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError(error_msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm what was this doing before if not raising this message already? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it raises |
||
if len(arrays[i]) != len(arrays[i - 1]): | ||
raise ValueError('all arrays must be same length') | ||
|
||
from pandas.core.arrays.categorical import _factorize_from_iterables | ||
|
||
codes, levels = _factorize_from_iterables(arrays) | ||
try: | ||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
codes, levels = _factorize_from_iterables(arrays) | ||
except TypeError: | ||
raise TypeError(error_msg) | ||
if names is None: | ||
names = [getattr(arr, "name", None) for arr in arrays] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could maybe think about how to improve the actual message as well, because on a first read I was interpreting this as "Input must be [a list] or [a sequence of array-likes]" (while of course it is "[list or sequence] of array-likes"), which confused me at first ..
To be true to the code, what it actually needs to be is a "list-like of list-likes"? Which is also not that nice to write ..
I am wondering if a more strict error message (stricter than what we allow), something like "Input must be a list of arrays" is not actually easier to understand for users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also thinking that the messages should be changed from
Input must be...
to something along the lines of'arrays' parameter of MultiIndex.from_arrays must be...
and then regurgitate whatever is in the docstring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC the reason that a sequence is accepted is to provide backward compatibility with zip. So sequence does not necessarily need to be mentioned in the docstring.