-
-
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
Changes from all commits
0974cee
06cb448
1438459
84993d7
0974c3b
96eeed2
403789a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,11 +324,17 @@ 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 elements of array are list-like | ||
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. do we fully test this? (test with tuples as well) 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. @jreback: not fully. i've added a test that is basically a cut and paste from another test. could parameterise now or refactor in a follow-on PR. i prefer the later since some other refactoring of the tests may be possible and may detract from the current change. |
||
for array in arrays: | ||
if not is_list_like(array): | ||
raise TypeError(error_msg) | ||
|
||
# Check if lengths of all arrays are equal or not, | ||
# raise ValueError, if not | ||
for i in range(1, len(arrays)): | ||
|
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.