-
Notifications
You must be signed in to change notification settings - Fork 529
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
PERF: Optimize can_cast_dtype. #2726
Conversation
1. Don't cast to array 2. Avoid trial casting and comparing. Use numpy type hierarchy to determine safety.
Surprisingly, accessing the name attribute of a numpy dtype is extremely slow.
|
else: | ||
return np.array_equal(values, values.astype(dtype)) | ||
min_dtype = get_minimum_dtype(values) | ||
return np.can_cast(min_dtype, dtype_name, casting='safe') |
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 see in https://numpy.org/doc/stable/reference/generated/numpy.can_cast.html, that it can accept an array:
return np.can_cast(min_dtype, dtype_name, casting='safe') | |
return np.can_cast(values, dtype_name, casting='safe') |
Have you tried this out to see what the performance is like versus get_minimum_dtype
?
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 didn't do this because it won't allow you to downcast (ie go from 'float64' to 'float32' if all your data fits in float32).
Does this PR depend on #2724? |
This is essentially what dtype.name does, but avoids all the overhead in numpy for accessing dtype.name.
Some benchmarks:
|
|
@groutr yes, I realized yesterday that |
Closing in favor of #3003, which eliminates internal use of |
These two changes make
can_cast_array
smarter for larger datasets or quick checks.