Skip to content
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

Use numpy.can_cast instead of casting and checking #7834

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix groupby sum, prod for all-NaN groups with ``flox``. (:issue:`7808`).
By `Deepak Cherian <https://github.com/dcherian>`_.
- Use `numpy.can_cast` to avoid a RuntimeWarning from numpy. (:pull:`7834`).

Documentation
~~~~~~~~~~~~~
Expand Down
5 changes: 2 additions & 3 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,8 @@ def encode_datetime(d):


def cast_to_int_if_safe(num) -> np.ndarray:
int_num = np.asarray(num, dtype=np.int64)
if (num == int_num).all():
num = int_num
if np.can_cast(num, to=np.int64, casting="safe"):
return np.asarray(num, dtype=np.int64)
return num


Expand Down