Skip to content

document limitations of cftime arithmetics #10653

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

larsbuntemeyer
Copy link

@larsbuntemeyer larsbuntemeyer commented Aug 18, 2025

@larsbuntemeyer
Copy link
Author

larsbuntemeyer commented Aug 18, 2025

I have a code example that inentionally should fail, i used the notation

.. code-block:: python

for that part.

@larsbuntemeyer larsbuntemeyer changed the title document limitations of ctime arithmetics document limitations of cftime arithmetics Aug 18, 2025
@larsbuntemeyer larsbuntemeyer marked this pull request as ready for review August 18, 2025 12:18
@larsbuntemeyer
Copy link
Author

I'm not sure why the readthedocs fails, my local build seems to work.

Copy link
Member

@spencerkclark spencerkclark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @larsbuntemeyer! It would be nice to get this working cleanly someday, especially now that xarray supports coarser resolution np.timedelta64 values.

You could also consider including a more verbose, but also more robust, workaround that adds the two DataArrays via apply_ufunc:

def add_cftime_and_timedelta64(cftime_da, timedelta64_da, join="inner"):
    def func(cftime_array, timedelta64_array):
        shape = cftime_array.shape
        cftime_array = cftime_array.ravel()
        timedelta64_array = timedelta64_array.ravel()
        timedelta_array = pd.to_timedelta(timedelta64_array).to_pytimedelta()
        return (cftime_array + timedelta_array).reshape(shape)
    
    return xr.apply_ufunc(
        func, 
        cftime_da,
        timedelta64_da,
        dask="parallelized",
        output_dtypes=[cftime_da.dtype],
        join=join
    )

This function has the advantage that it preserves the coordinate alignment, as well as multi-dimensional and dask compatibility features of xarray.

import cftime

time = xr.DataArray(
xr.cftime_range("2000", periods=3, freq="MS", calendar="noleap"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should fix the doc build—xarray.cftime_range has been deprecated since it is redundant as public API, so using it emits a warning:

Suggested change
xr.cftime_range("2000", periods=3, freq="MS", calendar="noleap"),
xr.date_range("2000", periods=3, freq="MS", calendar="noleap"),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document cftime arithmetic limitations
3 participants