Ouranosinc / xclim

Library of derived climate variables, ie climate indicators, based on xarray.
https://xclim.readthedocs.io/en/stable/
Apache License 2.0
331 stars 59 forks source link

Bug related to new version of xarray #1959

Closed knlowman closed 2 weeks ago

knlowman commented 2 weeks ago

Discussed in https://github.com/Ouranosinc/xclim/discussions/1957

Originally posted by **knlowman** October 15, 2024 ### Setup Information * Xclim version: 0.52.1 ### Context I just upgraded my xarray to version 2024.9.0 and I am now getting an error when I try to import ensembles from xclim. I reverted back to xarray version 2024.7.0 and am no longer getting the error. I am wondering if this is a universal bug for users of the newest xarray version. ### Steps To Reproduce My code: ``` import xarray as xr from xclim import ensembles ``` The error: ``` --------------------------------------------------------------------------- ImportError Traceback (most recent call last) Cell In[9], line 30 27 import matplotlib.ticker as mticker 28 from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER ---> 30 from xclim import ensembles 31 from xoverturning import calcmoc File [~/.local/lib/python3.10/site-packages/xclim/__init__.py:7](http://an101.princeton.rdhpcs.noaa.gov:11270/lab/tree/Jupyter_Notebooks/~/.local/lib/python3.10/site-packages/xclim/__init__.py#line=6) 3 from __future__ import annotations 5 import importlib.resources as _resources ----> 7 from xclim import indices 8 from xclim.core import units # noqa 9 from xclim.core.indicator import build_indicator_module_from_yaml File [~/.local/lib/python3.10/site-packages/xclim/indices/__init__.py:5](http://an101.princeton.rdhpcs.noaa.gov:11270/lab/tree/Jupyter_Notebooks/~/.local/lib/python3.10/site-packages/xclim/indices/__init__.py#line=4) 1 """Indices module.""" 3 from __future__ import annotations ----> 5 from ._agro import * 6 from ._anuclim import * 7 from ._conversion import * File [~/.local/lib/python3.10/site-packages/xclim/indices/_agro.py:20](http://an101.princeton.rdhpcs.noaa.gov:11270/lab/tree/Jupyter_Notebooks/~/.local/lib/python3.10/site-packages/xclim/indices/_agro.py#line=19) 12 from xclim.core.units import ( 13 amount2lwethickness, 14 convert_units_to, (...) 17 to_agg_units, 18 ) 19 from xclim.core.utils import DateStr, DayOfYearStr, Quantified ---> 20 from xclim.indices._conversion import potential_evapotranspiration 21 from xclim.indices._simple import tn_min 22 from xclim.indices._threshold import ( 23 first_day_temperature_above, 24 first_day_temperature_below, 25 ) File [~/.local/lib/python3.10/site-packages/xclim/indices/_conversion.py:19](http://an101.princeton.rdhpcs.noaa.gov:11270/lab/tree/Jupyter_Notebooks/~/.local/lib/python3.10/site-packages/xclim/indices/_conversion.py#line=18) 10 from xclim.core.units import ( 11 amount2rate, 12 convert_units_to, (...) 16 units2pint, 17 ) 18 from xclim.core.utils import Quantified ---> 19 from xclim.indices.helpers import ( 20 _gather_lat, 21 _gather_lon, 22 cosine_of_solar_zenith_angle, 23 day_lengths, 24 distance_from_sun, 25 extraterrestrial_solar_radiation, 26 solar_declination, 27 time_correction_for_solar_angle, 28 wind_speed_height_conversion, 29 ) 31 __all__ = [ 32 "clausius_clapeyron_scaled_precipitation", 33 "heat_index", (...) 55 "wind_profile", 56 ] 59 @declare_units(tas="[temperature]", tdps="[temperature]", hurs="[]") 60 def humidex( 61 tas: xr.DataArray, 62 tdps: xr.DataArray | None = None, 63 hurs: xr.DataArray | None = None, 64 ) -> xr.DataArray: File [~/.local/lib/python3.10/site-packages/xclim/indices/helpers.py:19](http://an101.princeton.rdhpcs.noaa.gov:11270/lab/tree/Jupyter_Notebooks/~/.local/lib/python3.10/site-packages/xclim/indices/helpers.py#line=18) 17 import numpy as np 18 import xarray as xr ---> 19 from xarray.coding.calendar_ops import ( 20 _datetime_to_decimal_year as datetime_to_decimal_year, 21 ) 23 from xclim.core.calendar import ensure_cftime_array, get_calendar 24 from xclim.core.units import convert_units_to ImportError: cannot import name '_datetime_to_decimal_year' from 'xarray.coding.calendar_ops' ([/home/Kiera.Lowman/.local/lib/python3.10/site-packages/xarray/coding/calendar_ops.py](http://an101.princeton.rdhpcs.noaa.gov:11270/home/Kiera.Lowman/.local/lib/python3.10/site-packages/xarray/coding/calendar_ops.py)) ```
aulemahal commented 2 weeks ago

Sadly, this was me shooting ourselves in the foot, sorry about that.

The issue is fixed in xclim 0.52.2. You should be able to smoothly update xclim, as this fix was the only significant modification from 0.52.1 .

Zeitsperre commented 2 weeks ago

Hi there @knlowman

This error is known and is being raised due to the fact that changes that were ported from xclim were accepted into xarray not long after release our release here of v0.52.1.

The latest patch release of xclim (https://github.com/Ouranosinc/xclim/releases/tag/v0.52.2) addresses this issue. You should be able to install the most recent xarray (2024.9.0) alongside the latest release.

knlowman commented 2 weeks ago

Hi @Zeitsperre, sorry I missed that! Upgrading xclim fixed it.