Repository navigation
Closed
Description
I just realized my CI broke because, e.g., this does not work since the latest release (worked before):
# Example: xarray Dataset with CF-compliant time bounds using cftime
import numpy as np
import xarray as xr
import cf_xarray as cfxr
import cftime
# Create cftime objects for monthly bounds
periods = 3
start = cftime.DatetimeGregorian(2000, 1, 1)
edges = [cftime.DatetimeGregorian(2000, m, 1) for m in range(1, periods + 2)]
# Bounds as [start, end) for each month
bnds = np.array([[edges[i], edges[i+1]] for i in range(periods)])
mid = np.array([edges[i] + (edges[i+1] - edges[i]) / 2 for i in range(periods)])
# Sample data
values = xr.DataArray(np.arange(periods, dtype=float), dims=("time",), coords={"time": mid})
# Build dataset with CF-style bounds
ds = xr.Dataset(
{"foo": values},
coords={
"time": ("time", mid, {"bounds": "time_bnds"}),
"time_bnds": (("time", "bnds"), bnds),
"bnds": ("bnds", [0, 1])
}
)
# Create vertices from bounds
cfxr.bounds_to_vertices(ds.time_bnds, "bnds")gives...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], [line 30](vscode-notebook-cell:?execution_count=3&line=30)
20 ds = xr.Dataset(
21 {"foo": values},
22 coords={
(...) 26 }
27 )
29 # Create vertices from bounds
---> 30 cfxr.bounds_to_vertices(ds.time_bnds, "bnds")
File ~/python/packages/cf-xarray/cf_xarray/helpers.py:181, in bounds_to_vertices(bounds, bounds_dim, core_dims, order)
174 raise ValueError(
175 f"Bounds format not understood. Got {bounds.dims} with shape {bounds.shape}."
176 )
178 core_dim_coords = {
179 dim: bounds.coords[dim].values for dim in core_dims if dim in bounds.coords
180 }
--> 181 core_dim_orders = _get_core_dim_orders(core_dim_coords)
183 return xr.apply_ufunc(
184 _bounds_helper,
185 bounds,
(...) 196 output_dtypes=[bounds.dtype],
197 )
File ~/python/packages/cf-xarray/cf_xarray/helpers.py:240, in _get_core_dim_orders(core_dim_coords)
237 if nonzero_diffs.size == 0:
238 # All values are equal, treat as ascending
239 core_dim_orders[dim] = "ascending"
--> 240 elif np.all(nonzero_diffs > 0):
241 core_dim_orders[dim] = "ascending"
242 elif np.all(nonzero_diffs < 0):
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
It still, however, works with usual datetime objects.
Metadata
Metadata
Assignees
Labels
No labels
Activity
larsbuntemeyer commentedon Aug 15, 2025
It seems with #579, we need to determine the order of an array of cftime objects or datetime timedelta respectivley in here
cf-xarray/cf_xarray/helpers.py
Lines 200 to 247 in cfe87d0
timedelta does not cast to float which seems to be the problem here...
larsbuntemeyer commentedon Aug 15, 2025
Seems like we could use total_seconds to cast cftime diffs to float.