From 48dc89cb9a1976ba2c4546bf10f4e9f14b44b09a Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Tue, 13 Apr 2021 20:20:40 -0400 Subject: [PATCH 1/5] Catch either OutOfBoundsTimedelta or OverflowError --- xarray/coding/cftimeindex.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index aafd620c7bf..77fe586442f 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -57,12 +57,7 @@ CFTIME_REPR_LENGTH = 19 ITEMS_IN_REPR_MAX_ELSE_ELLIPSIS = 100 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 = (pd.errors.OutOfBoundsTimedelta, OverflowError) def named(name, pattern): @@ -568,7 +563,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 +574,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." From 957882ea1bae3e13ac086c95239a24b66d0eab1a Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Tue, 13 Apr 2021 20:31:02 -0400 Subject: [PATCH 2/5] OutOfBoundsTimedelta was only added in pandas 1.1.0 --- xarray/coding/cftimeindex.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 77fe586442f..8cc5e7ebf1a 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -57,7 +57,12 @@ CFTIME_REPR_LENGTH = 19 ITEMS_IN_REPR_MAX_ELSE_ELLIPSIS = 100 REPR_ELLIPSIS_SHOW_ITEMS_FRONT_END = 10 -OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (pd.errors.OutOfBoundsTimedelta, OverflowError) + + +if LooseVersion(pd.__version__) > LooseVersion("1.1.0"): + OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (pd.errors.OutOfBoundsTimedelta, OverflowError) +else: + OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (OverflowError, ) def named(name, pattern): From bdc54ee7cb6174d8983b204a27466b5839faa2d5 Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Tue, 13 Apr 2021 20:34:01 -0400 Subject: [PATCH 3/5] lint --- xarray/coding/cftimeindex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 8cc5e7ebf1a..c0fe75482a6 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -62,7 +62,7 @@ if LooseVersion(pd.__version__) > LooseVersion("1.1.0"): OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (pd.errors.OutOfBoundsTimedelta, OverflowError) else: - OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (OverflowError, ) + OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (OverflowError,) def named(name, pattern): From 08fc7ab94215e31ff4a393686e5909895746897c Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Wed, 14 Apr 2021 06:57:56 -0400 Subject: [PATCH 4/5] Fix mypy error --- xarray/coding/cftimeindex.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index c0fe75482a6..687239c8d53 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,6 +60,7 @@ REPR_ELLIPSIS_SHOW_ITEMS_FRONT_END = 10 +OUT_OF_BOUNDS_TIMEDELTA_ERRORS: Tuple[Type[Exception], ...] if LooseVersion(pd.__version__) > LooseVersion("1.1.0"): OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (pd.errors.OutOfBoundsTimedelta, OverflowError) else: From af898e1243e656414e065530913d3ac785047397 Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Wed, 14 Apr 2021 08:57:41 -0400 Subject: [PATCH 5/5] Implement suggestion from @keewis --- xarray/coding/cftimeindex.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 687239c8d53..15f75955e00 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -61,9 +61,9 @@ OUT_OF_BOUNDS_TIMEDELTA_ERRORS: Tuple[Type[Exception], ...] -if LooseVersion(pd.__version__) > LooseVersion("1.1.0"): +try: OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (pd.errors.OutOfBoundsTimedelta, OverflowError) -else: +except AttributeError: OUT_OF_BOUNDS_TIMEDELTA_ERRORS = (OverflowError,)