File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed
Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change 2525
2626import numpy as np
2727import xarray as xr
28+ from packaging .version import Version
2829from scipy import sparse
2930
3031from climada .hazard .xarray import HazardXarrayReader
3536
3637LOGGER = logging .getLogger (__name__ )
3738
39+ XARRAY_TIMEDELTA_BUG_BEGIN = Version ("2025.04.0" )
40+ XARRAY_TIMEDELTA_BUG_END = Version ("2025.07.0" )
41+
42+
43+ def xarray_has_timedelta_bug () -> bool :
44+ """Return True if xarray contains the timedelta bug
45+
46+ See https://docs.xarray.dev/en/stable/whats-new.html#id80
47+ """
48+ return (Version (xr .__version__ ) >= XARRAY_TIMEDELTA_BUG_BEGIN ) and (
49+ Version (xr .__version__ ) < XARRAY_TIMEDELTA_BUG_END
50+ )
51+
3852
3953class HazardForecast (Forecast , Hazard ):
4054 """A hazard object with forecast information"""
@@ -417,6 +431,15 @@ def from_xarray_raster(
417431 :py:meth:`climada.hazard.base.Hazard.from_xarray_raster`
418432 Parent method documentation for standard hazard loading
419433 """
434+ if xarray_has_timedelta_bug ():
435+ LOGGER .warning (
436+ "xarray version %s contains a bug that prevents proper timedelta "
437+ "parsing. Consider updating to version %s or later, or downgrading to "
438+ "before version %s" ,
439+ xr .__version__ ,
440+ XARRAY_TIMEDELTA_BUG_END ,
441+ XARRAY_TIMEDELTA_BUG_BEGIN ,
442+ )
420443
421444 # Open dataset if needed
422445 if isinstance (data , (pathlib .Path , str )):
Original file line number Diff line number Diff line change 2626import pandas as pd
2727import pytest
2828import xarray as xr
29- from packaging .version import Version
3029from scipy .sparse import csr_matrix
3130
3231from climada .hazard .base import Hazard
33- from climada .hazard .forecast import HazardForecast
32+ from climada .hazard .forecast import HazardForecast , xarray_has_timedelta_bug
3433from climada .hazard .test .test_base import hazard_kwargs
3534
3635# See https://docs.xarray.dev/en/stable/whats-new.html#id80
3736xarray_leadtime = pytest .mark .skipif (
38- (Version (xr .__version__ ) < Version ("2025.07.0" ))
39- and (Version (xr .__version__ ) >= Version ("2025.04.0" )),
40- reason = "xarray timedelta bug" ,
37+ xarray_has_timedelta_bug (), reason = "xarray timedelta bug"
4138)
4239
4340
You can’t perform that action at this time.
0 commit comments