-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement cftime vectorization as discussed in PR #8322 #8324
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
Merged
spencerkclark
merged 13 commits into
pydata:main
from
antscloud:vectorization_of_encode_cftime
May 30, 2025
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3f6bf49
feat: implement as discussed in PR #8322
antscloud f28fc7d
fix: Stick closer to the original function and add PR suggestions
antscloud dc2609c
Merge branch 'main' into vectorization_of_encode_cftime
Illviljan 06af81a
Update xarray/coding/times.py
dcherian 05428fd
Merge branch 'main' into vectorization_of_encode_cftime
dcherian 80bb5fe
Update xarray/coding/times.py
dcherian f4828a2
Merge branch 'main' into vectorization_of_encode_cftime
dcherian 8be73cf
Merge branch 'main' into vectorization_of_encode_cftime
kmuehlbauer 042638d
Update xarray/coding/times.py
kmuehlbauer 147ea67
Merge branch 'main' into vectorization_of_encode_cftime
spencerkclark c83f76a
Add what's new entry
spencerkclark 6d4dc3b
Merge branch 'main' into vectorization_of_encode_cftime
spencerkclark c37f1f1
Merge branch 'main' into vectorization_of_encode_cftime
spencerkclark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -625,25 +625,38 @@ def _encode_datetime_with_cftime(dates, units: str, calendar: str) -> np.ndarray | |
if cftime is None: | ||
raise ModuleNotFoundError("No module named 'cftime'") | ||
|
||
dates = np.array(dates) | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
original_shape = dates.shape | ||
|
||
if np.issubdtype(dates.dtype, np.datetime64): | ||
# numpy's broken datetime conversion only works for us precision | ||
dates = dates.astype("M8[us]").astype(datetime) | ||
|
||
def encode_datetime(d): | ||
# Since netCDF files do not support storing float128 values, we ensure | ||
# that float64 values are used by setting longdouble=False in num2date. | ||
# This try except logic can be removed when xarray's minimum version of | ||
# cftime is at least 1.6.2. | ||
try: | ||
return ( | ||
np.nan | ||
if d is None | ||
else cftime.date2num(d, units, calendar, longdouble=False) | ||
) | ||
except TypeError: | ||
return np.nan if d is None else cftime.date2num(d, units, calendar) | ||
dates = np.atleast_1d(dates) | ||
|
||
# Find all the None position | ||
none_position = np.equal(dates, None) | ||
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. mypy complains here.
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
filtered_dates = dates[~none_position] | ||
|
||
# Since netCDF files do not support storing float128 values, we ensure | ||
# that float64 values are used by setting longdouble=False in num2date. | ||
# This try except logic can be removed when xarray's minimum version of | ||
# cftime is at least 1.6.2. | ||
try: | ||
encoded_nums = cftime.date2num( | ||
filtered_dates, units, calendar, longdouble=False | ||
) | ||
except TypeError: | ||
encoded_nums = cftime.date2num(filtered_dates, units, calendar) | ||
|
||
if filtered_dates.size == none_position.size: | ||
return encoded_nums.reshape(original_shape) | ||
|
||
return reshape(np.array([encode_datetime(d) for d in ravel(dates)]), dates.shape) | ||
# Create a full matrix of NaN | ||
# And fill the num dates in the not NaN or None position | ||
result = np.full(dates.shape, np.nan) | ||
spencerkclark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result[np.nonzero(~none_position)] = encoded_nums | ||
return result.reshape(original_shape) | ||
|
||
|
||
def cast_to_int_if_safe(num) -> np.ndarray: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.