Skip to content

Commit 491b5f8

Browse files
committed
Add warning for bugged xarray version
1 parent 1ae0b48 commit 491b5f8

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

climada/hazard/forecast.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import numpy as np
2727
import xarray as xr
28+
from packaging.version import Version
2829
from scipy import sparse
2930

3031
from climada.hazard.xarray import HazardXarrayReader
@@ -35,6 +36,19 @@
3536

3637
LOGGER = 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

3953
class 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)):

climada/hazard/test/test_forecast.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,15 @@
2626
import pandas as pd
2727
import pytest
2828
import xarray as xr
29-
from packaging.version import Version
3029
from scipy.sparse import csr_matrix
3130

3231
from climada.hazard.base import Hazard
33-
from climada.hazard.forecast import HazardForecast
32+
from climada.hazard.forecast import HazardForecast, xarray_has_timedelta_bug
3433
from climada.hazard.test.test_base import hazard_kwargs
3534

3635
# See https://docs.xarray.dev/en/stable/whats-new.html#id80
3736
xarray_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

0 commit comments

Comments
 (0)