From e2401450921043adae35fe0dfc5e4a388c9cd07a Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Sat, 21 Aug 2021 06:35:38 -0400 Subject: [PATCH 1/3] Remove use of deprecated kind argument in CFTimeIndex tests --- doc/whats-new.rst | 3 +++ xarray/tests/test_cftimeindex.py | 37 +++++++++++++------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 4f79a37eb4b..35a2323988a 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -59,6 +59,9 @@ Internal Changes By `Jimmy Westling `_. - Use isort's `float_to_top` config. (:pull:`5695`). By `Maximilian Roos `_. +- Remove use of the deprecated ``kind`` argument in + :py:meth:`pandas.Index.get_slice_bound` inside :py:class:`xarray.CFTimeIndex` + tests (:pull:`5722`). By `Spencer Clark `_. .. _whats-new.0.19.0: diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 725b5efee75..3ece195cdea 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -345,65 +345,58 @@ def test_get_loc(date_type, index): @requires_cftime -@pytest.mark.parametrize("kind", ["loc", "getitem"]) -def test_get_slice_bound(date_type, index, kind): - result = index.get_slice_bound("0001", "left", kind) +def test_get_slice_bound(date_type, index): + result = index.get_slice_bound("0001", "left") expected = 0 assert result == expected - result = index.get_slice_bound("0001", "right", kind) + result = index.get_slice_bound("0001", "right") expected = 2 assert result == expected - result = index.get_slice_bound(date_type(1, 3, 1), "left", kind) + result = index.get_slice_bound(date_type(1, 3, 1), "left") expected = 2 assert result == expected - result = index.get_slice_bound(date_type(1, 3, 1), "right", kind) + result = index.get_slice_bound(date_type(1, 3, 1), "right") expected = 2 assert result == expected @requires_cftime -@pytest.mark.parametrize("kind", ["loc", "getitem"]) -def test_get_slice_bound_decreasing_index(date_type, monotonic_decreasing_index, kind): - result = monotonic_decreasing_index.get_slice_bound("0001", "left", kind) +def test_get_slice_bound_decreasing_index(date_type, monotonic_decreasing_index): + result = monotonic_decreasing_index.get_slice_bound("0001", "left") expected = 2 assert result == expected - result = monotonic_decreasing_index.get_slice_bound("0001", "right", kind) + result = monotonic_decreasing_index.get_slice_bound("0001", "right") expected = 4 assert result == expected - result = monotonic_decreasing_index.get_slice_bound( - date_type(1, 3, 1), "left", kind - ) + result = monotonic_decreasing_index.get_slice_bound(date_type(1, 3, 1), "left") expected = 2 assert result == expected - result = monotonic_decreasing_index.get_slice_bound( - date_type(1, 3, 1), "right", kind - ) + result = monotonic_decreasing_index.get_slice_bound(date_type(1, 3, 1), "right") expected = 2 assert result == expected @requires_cftime -@pytest.mark.parametrize("kind", ["loc", "getitem"]) -def test_get_slice_bound_length_one_index(date_type, length_one_index, kind): - result = length_one_index.get_slice_bound("0001", "left", kind) +def test_get_slice_bound_length_one_index(date_type, length_one_index): + result = length_one_index.get_slice_bound("0001", "left") expected = 0 assert result == expected - result = length_one_index.get_slice_bound("0001", "right", kind) + result = length_one_index.get_slice_bound("0001", "right") expected = 1 assert result == expected - result = length_one_index.get_slice_bound(date_type(1, 3, 1), "left", kind) + result = length_one_index.get_slice_bound(date_type(1, 3, 1), "left") expected = 1 assert result == expected - result = length_one_index.get_slice_bound(date_type(1, 3, 1), "right", kind) + result = length_one_index.get_slice_bound(date_type(1, 3, 1), "right") expected = 1 assert result == expected From b7c6fe65ba8c0e4e640bc6a635489c4c229731e9 Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Sat, 21 Aug 2021 06:54:42 -0400 Subject: [PATCH 2/3] Update PR number --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 35a2323988a..5b654582f44 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -61,7 +61,7 @@ Internal Changes By `Maximilian Roos `_. - Remove use of the deprecated ``kind`` argument in :py:meth:`pandas.Index.get_slice_bound` inside :py:class:`xarray.CFTimeIndex` - tests (:pull:`5722`). By `Spencer Clark `_. + tests (:pull:`5723`). By `Spencer Clark `_. .. _whats-new.0.19.0: From f8607aa1e73c277524a422ee6040b1cc5865e378 Mon Sep 17 00:00:00 2001 From: spencerkclark Date: Sat, 23 Oct 2021 20:15:53 -0400 Subject: [PATCH 3/3] Add version-dependent logic to add kind argument where needed in tests --- xarray/tests/test_cftimeindex.py | 53 ++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 3ece195cdea..619fb0acdc4 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -1,4 +1,5 @@ from datetime import timedelta +from distutils.version import LooseVersion from textwrap import dedent import numpy as np @@ -346,57 +347,85 @@ def test_get_loc(date_type, index): @requires_cftime def test_get_slice_bound(date_type, index): - result = index.get_slice_bound("0001", "left") + # The kind argument is required in earlier versions of pandas even though it + # is not used by CFTimeIndex. This logic can be removed once our minimum + # version of pandas is at least 1.3. + if LooseVersion(pd.__version__) < LooseVersion("1.3"): + kind_args = ("getitem",) + else: + kind_args = () + + result = index.get_slice_bound("0001", "left", *kind_args) expected = 0 assert result == expected - result = index.get_slice_bound("0001", "right") + result = index.get_slice_bound("0001", "right", *kind_args) expected = 2 assert result == expected - result = index.get_slice_bound(date_type(1, 3, 1), "left") + result = index.get_slice_bound(date_type(1, 3, 1), "left", *kind_args) expected = 2 assert result == expected - result = index.get_slice_bound(date_type(1, 3, 1), "right") + result = index.get_slice_bound(date_type(1, 3, 1), "right", *kind_args) expected = 2 assert result == expected @requires_cftime def test_get_slice_bound_decreasing_index(date_type, monotonic_decreasing_index): - result = monotonic_decreasing_index.get_slice_bound("0001", "left") + # The kind argument is required in earlier versions of pandas even though it + # is not used by CFTimeIndex. This logic can be removed once our minimum + # version of pandas is at least 1.3. + if LooseVersion(pd.__version__) < LooseVersion("1.3"): + kind_args = ("getitem",) + else: + kind_args = () + + result = monotonic_decreasing_index.get_slice_bound("0001", "left", *kind_args) expected = 2 assert result == expected - result = monotonic_decreasing_index.get_slice_bound("0001", "right") + result = monotonic_decreasing_index.get_slice_bound("0001", "right", *kind_args) expected = 4 assert result == expected - result = monotonic_decreasing_index.get_slice_bound(date_type(1, 3, 1), "left") + result = monotonic_decreasing_index.get_slice_bound( + date_type(1, 3, 1), "left", *kind_args + ) expected = 2 assert result == expected - result = monotonic_decreasing_index.get_slice_bound(date_type(1, 3, 1), "right") + result = monotonic_decreasing_index.get_slice_bound( + date_type(1, 3, 1), "right", *kind_args + ) expected = 2 assert result == expected @requires_cftime def test_get_slice_bound_length_one_index(date_type, length_one_index): - result = length_one_index.get_slice_bound("0001", "left") + # The kind argument is required in earlier versions of pandas even though it + # is not used by CFTimeIndex. This logic can be removed once our minimum + # version of pandas is at least 1.3. + if LooseVersion(pd.__version__) <= LooseVersion("1.3"): + kind_args = ("getitem",) + else: + kind_args = () + + result = length_one_index.get_slice_bound("0001", "left", *kind_args) expected = 0 assert result == expected - result = length_one_index.get_slice_bound("0001", "right") + result = length_one_index.get_slice_bound("0001", "right", *kind_args) expected = 1 assert result == expected - result = length_one_index.get_slice_bound(date_type(1, 3, 1), "left") + result = length_one_index.get_slice_bound(date_type(1, 3, 1), "left", *kind_args) expected = 1 assert result == expected - result = length_one_index.get_slice_bound(date_type(1, 3, 1), "right") + result = length_one_index.get_slice_bound(date_type(1, 3, 1), "right", *kind_args) expected = 1 assert result == expected