Skip to content

Conversation

@larsbuntemeyer
Copy link
Contributor

@larsbuntemeyer larsbuntemeyer commented Aug 18, 2025

@larsbuntemeyer
Copy link
Contributor 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
Contributor 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.

@larsbuntemeyer
Copy link
Contributor Author

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

I agree! That would be really helpful!

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
    )

Thanks for this solution! I'll add this to the docs.

@larsbuntemeyer
Copy link
Contributor Author

@spencerkclark I added your workaround to the docs. Thanks a lot for that!

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!

@spencerkclark spencerkclark enabled auto-merge (squash) December 3, 2025 22:10
@spencerkclark spencerkclark merged commit 50218d0 into pydata:main Dec 3, 2025
39 checks passed
@welcome
Copy link

welcome bot commented Dec 3, 2025

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

@larsbuntemeyer larsbuntemeyer deleted the cftime-docs branch December 3, 2025 22:34
dcherian added a commit to dcherian/xarray that referenced this pull request Dec 4, 2025
* main:
  Document limitations of cftime arithmetic (pydata#10653)
  Remove RTD htmlzip output format (pydata#10977)
  Support decoding unsigned integers to timedelta (pydata#10972)
  Use `._data` in `Variable._replace` (pydata#10969)
  small changes to the pixi env definitions (pydata#10976)
  Add GitHub Codespaces config for Pixi (pydata#10929)
  Fix workflow name to embed `matrix.pytest-addopts` (pydata#10970)
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