-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
MAINT: Speed up normalize_axis_tuple by about 30% #11518
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -1509,11 +1509,14 @@ def normalize_axis_tuple(axis, ndim, argname=None, allow_duplicate=False): | |
-------- | ||
normalize_axis_index : normalizing a single scalar axis | ||
""" | ||
try: | ||
axis = [operator.index(axis)] | ||
except TypeError: | ||
axis = tuple(axis) | ||
axis = tuple(normalize_axis_index(ax, ndim, argname) for ax in axis) | ||
# Speed-up most common cases. | ||
if not isinstance(axis, (list, tuple)): | ||
try: | ||
axis = [operator.index(axis)] | ||
except TypeError: | ||
pass | ||
# Going via an iterator directly is slower than via list comprehension. | ||
axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis]) | ||
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. I don't understand why this is the case... 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. I think list comprehension is special-cased, not constructing an iterator but an actual |
||
if not allow_duplicate and len(set(axis)) != len(axis): | ||
if argname: | ||
raise ValueError('repeated axis in `{}` argument'.format(argname)) | ||
|
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.
Would be more correct to check
type(axis) in (list, tuple)
, since subclasses might override__index__
.I was tempted to change it to just
type(axis) is tuple
, to encourage people to use tuple rather than list axis arguments....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 think if someone subclasses
list
ortuple
, one should reasonably expect that the object can be iterated over, not also treated as an integer! (I'd actually prefer to just test for something being iterable, but things likenp.iterable
are way slower than theisinstance
check.)As for
tuple
- I can at least change the order!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.
Reasonable or not, I'd prefer that we don't introduce an unnecessary behavior change now.
type(axis) in (tuple, list)
sounds like a good approach to me