Skip to content

Commit b610a3c

Browse files
Adapt exception handling in CFTimeIndex.__sub__ and __rsub__ (#5006)
The latest version of pandas raises an OutOfBoundsTimedelta error instead of an Overflow error.
1 parent 67903ff commit b610a3c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

xarray/coding/cftimeindex.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
REPR_ELLIPSIS_SHOW_ITEMS_FRONT_END = 10
6060

6161

62+
if LooseVersion(pd.__version__) > LooseVersion("1.2.3"):
63+
OUT_OF_BOUNDS_TIMEDELTA_ERROR = pd.errors.OutOfBoundsTimedelta
64+
else:
65+
OUT_OF_BOUNDS_TIMEDELTA_ERROR = OverflowError
66+
67+
6268
def named(name, pattern):
6369
return "(?P<" + name + ">" + pattern + ")"
6470

@@ -562,7 +568,7 @@ def __sub__(self, other):
562568
elif _contains_cftime_datetimes(np.array(other)):
563569
try:
564570
return pd.TimedeltaIndex(np.array(self) - np.array(other))
565-
except OverflowError:
571+
except OUT_OF_BOUNDS_TIMEDELTA_ERROR:
566572
raise ValueError(
567573
"The time difference exceeds the range of values "
568574
"that can be expressed at the nanosecond resolution."
@@ -573,7 +579,7 @@ def __sub__(self, other):
573579
def __rsub__(self, other):
574580
try:
575581
return pd.TimedeltaIndex(other - np.array(self))
576-
except OverflowError:
582+
except OUT_OF_BOUNDS_TIMEDELTA_ERROR:
577583
raise ValueError(
578584
"The time difference exceeds the range of values "
579585
"that can be expressed at the nanosecond resolution."

0 commit comments

Comments
 (0)