From ba60d9b58c60c55a417144c9d97e2be0558e15c9 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sat, 6 Mar 2021 18:54:19 -0500 Subject: [PATCH] Adapt exception handling logic in CFTimeIndex.__sub__ and __rsub__ The latest version of pandas raises an OutOfBoundsTimedelta error instead of an Overflow error. --- xarray/coding/cftimeindex.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 948bff1056a..aafd620c7bf 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -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 + ")" @@ -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." @@ -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."