Stream: xarray

Topic: xarray and cftime compatibility


view this post on Zulip Brian Bonnlander (Jan 19 2022 at 22:28):

I'm unable to run some calendar conversion code I wrote using xarray a while back. I'm trying to convert data on a 365-day calendar to a standard calendar by inserting leap days into the time axis. With version 0.19.0 of xarray, I'm getting this error when calling assign_coords():

ValueError: conflicting sizes for dimension 'time': length 34675 on 'time' and length 34698 on {'time': 'tmax', 'lat': 'tmax', 'lon': 'tmax'}

The code looks like this:

    # Create an equivalent date range on the Gregorian calendar
    start_date = ds.time.values[0]
    end_date = ds.time.values[-1]
    times = xr.DataArray(xr.cftime_range(start=start_date, end=end_date, freq='D', calendar='gregorian', normalize=True), dims='time')

    # Find the leap days in this date range.
    is_leap_day = (times.time.dt.month == 2) & (times.time.dt.day == 29)
    leap_days = times.where(is_leap_day, drop=True)

    print(f'Found {len(leap_days)} leap days.')

    # Create fill values for these days.
    one_time_step = ds.isel(time=slice(0, 1))
    fill_values = []
    for leap_day in leap_days:
        d = xr.full_like(one_time_step,fill_value=np.nan)
        d = d.assign_coords(time=[leap_day.data])
        fill_values.append(d)

    # Append the fill values to the dataset and then sort values by time.
    fill_values.append(ds)

    ds_fixed=xr.concat(fill_values, dim='time').sortby('time')

I'm considering deprecating xarray and cftime to the versions I was using before when the code worked, but I can't figure out which versions of xarray and cftime are compatible. conda search doesn't seem to give version dates, so I don't know how to resolve this problem. Any help is appreciated!

view this post on Zulip Brian Bonnlander (Jan 19 2022 at 23:33):

I'll try to answer my own question: it looks like installing a deprecated version of xarray will automatically install the appropriate version of cftime. I didn't realize that the latter package is bundled with the former.


Last updated: May 16 2025 at 17:14 UTC