Skip to content

Commit

Permalink
Adapt exception handling logic in CFTimeIndex.__sub__ and __rsub__
Browse files Browse the repository at this point in the history
The latest version of pandas raises an OutOfBoundsTimedelta error instead
of an Overflow error.
  • Loading branch information
spencerkclark committed Mar 7, 2021
1 parent dfe50b8 commit ba60d9b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
REPR_ELLIPSIS_SHOW_ITEMS_FRONT_END = 10


if LooseVersion(pd.__version__) > LooseVersion("1.2.3"):
OUT_OF_BOUNDS_TIMEDELTA_ERROR = pd.errors.OutOfBoundsTimedelta
else:
OUT_OF_BOUNDS_TIMEDELTA_ERROR = OverflowError


def named(name, pattern):
return "(?P<" + name + ">" + pattern + ")"

Expand Down Expand Up @@ -562,7 +568,7 @@ def __sub__(self, other):
elif _contains_cftime_datetimes(np.array(other)):
try:
return pd.TimedeltaIndex(np.array(self) - np.array(other))
except OverflowError:
except OUT_OF_BOUNDS_TIMEDELTA_ERROR:
raise ValueError(
"The time difference exceeds the range of values "
"that can be expressed at the nanosecond resolution."
Expand All @@ -573,7 +579,7 @@ def __sub__(self, other):
def __rsub__(self, other):
try:
return pd.TimedeltaIndex(other - np.array(self))
except OverflowError:
except OUT_OF_BOUNDS_TIMEDELTA_ERROR:
raise ValueError(
"The time difference exceeds the range of values "
"that can be expressed at the nanosecond resolution."
Expand Down

0 comments on commit ba60d9b

Please sign in to comment.