Skip to content

Commit

Permalink
Catch either OutOfBoundsTimedelta or OverflowError in CFTimeIndex.__s…
Browse files Browse the repository at this point in the history
…ub__ and CFTimeIndex.__rsub__ (#5154)
  • Loading branch information
spencerkclark authored Apr 14, 2021
1 parent f94de6b commit 9b60f01
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import warnings
from datetime import timedelta
from distutils.version import LooseVersion
from typing import Tuple, Type

import numpy as np
import pandas as pd
Expand All @@ -59,10 +60,11 @@
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
OUT_OF_BOUNDS_TIMEDELTA_ERRORS: Tuple[Type[Exception], ...]
try:
OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (pd.errors.OutOfBoundsTimedelta, OverflowError)
except AttributeError:
OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (OverflowError,)


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

0 comments on commit 9b60f01

Please sign in to comment.