From 9b60f01066c1209b719ab3a3b111aa66b5fc3e26 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Wed, 14 Apr 2021 09:27:10 -0400 Subject: [PATCH] Catch either OutOfBoundsTimedelta or OverflowError in CFTimeIndex.__sub__ and CFTimeIndex.__rsub__ (#5154) --- xarray/coding/cftimeindex.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index aafd620c7bf..15f75955e00 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -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 @@ -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): @@ -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." @@ -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."