-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix for scalar detection #8821
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
Fix for scalar detection #8821
Conversation
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.
Thanks for digging into this @huard and @aulemahal. This looks reasonable to me based on the NumPy documentation you referenced, though I probably defer to others on whether there are any subtleties we need to worry about.
Is the inclusion of the hasattr(array, "dtype")
condition to avoid casting zero-dimensional non-NumPy array types to NumPy arrays?
It was rather to avoid casting 0-dimensional non-scalar numpy objects. >>> a = np.array("abc")
>>> np.isscalar(a)
False
>>> np.ndim(a)
0 |
Hi there, I have a library that could benefit from this fix, any updates here? |
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.
thanks for the ping, @dcherian, and apologies for saying I would look into this and then promptly forgetting about it. I believe it was good to wait until after numpy=2.1
, though, since that appears to have broken is_duck_array
.
Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
@keewis I applied your suggestion and it seems the issue is fixed! Failures in the tests do not seem related to this PR, I see that the |
sorry for missing this a month ago. Could you merge in |
@keewis Done. Thanks again! |
_wrap_numpy_scalars
fix* main: dev whats-new (pydata#10294) Add SeasonGrouper, SeasonResampler (pydata#9524) (fix): remove `PandasExtensionArray` from repr (pydata#10291) Do not rely on `np.broadcast_to` to perform trivial dimension insertion (pydata#10277) DOC: Remove reference to absolufy (pydata#10290) Fix for scalar detection (pydata#8821) Add Index.validate_dataarray_coord (pydata#10137) add redirect for contributing guide (pydata#10282) Update pre-commit hooks (pydata#10288) Adding xarray-eopf to ecosystem.rst (pydata#10289) Add public typing.py module (pydata#10215) Rename Twitter to X (pydata#10283) Add `xarray-lmfit` extension for curve fitting to ecosystem documentation (pydata#10262)
_wrap_numpy_scalars
relies onnp.isscalar
, which incorrectly labels a single cftime object as not a scalar.The PR adds logic to handle non-numpy objects using the
np.ndim
function. The logic for built-ins and numpy objects should remain the same.The function logic could possibly be rewritten more clearly as
whats-new.rst
api.rst