From 6d8932378121ee2e1e14fb7888faf334d275a92b Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 21 Aug 2020 16:15:12 -0700 Subject: [PATCH 01/10] Ensure dates can be passed into get_slice bounds --- pandas/core/indexes/base.py | 12 ++++++++---- pandas/core/indexes/datetimes.py | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 623ce68201492..a748d0f86811c 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5028,7 +5028,7 @@ def _searchsorted_monotonic(self, label, side="left"): raise ValueError("index must be monotonic increasing or decreasing") - def get_slice_bound(self, label, side: str_t, kind) -> int: + def get_slice_bound(self, label, side: str_t, kind: Optional[str_t]) -> int: """ Calculate slice bound that corresponds to given label. @@ -5046,20 +5046,24 @@ def get_slice_bound(self, label, side: str_t, kind) -> int: int Index of label. """ - assert kind in ["loc", "getitem", None] - if side not in ("left", "right"): raise ValueError( "Invalid value for side kwarg, must be either " f"'left' or 'right': {side}" ) + if kind not in ("loc", "getitem", None): + raise ValueError( + "Invalid value for kind kwarg, must be either " + f"'loc' or 'getitem': {kind}" + ) + original_label = label # For datetime indices label may be a string that has to be converted # to datetime boundary according to its resolution. label = self._maybe_cast_slice_bound(label, side, kind) - + breakpoint() # we need to look up the label try: slc = self.get_loc(label) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index f71fd0d406c54..6d81d2f28511b 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -672,6 +672,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind): if self._is_strictly_monotonic_decreasing and len(self) > 1: return upper if side == "left" else lower return lower if side == "left" else upper + elif isinstance(label, date) and not isinstance(label, datetime): + return datetime.combine(label, time(0, 0)) else: return label From bf6ea7e165f203fdbcf76f1455faa8b80abc82c1 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 21 Aug 2020 22:14:54 -0700 Subject: [PATCH 02/10] Cast the slice bounds and conform the tz --- pandas/core/indexes/base.py | 2 +- pandas/core/indexes/datetimes.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a748d0f86811c..59e1949570e3b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5063,7 +5063,7 @@ def get_slice_bound(self, label, side: str_t, kind: Optional[str_t]) -> int: # For datetime indices label may be a string that has to be converted # to datetime boundary according to its resolution. label = self._maybe_cast_slice_bound(label, side, kind) - breakpoint() + # we need to look up the label try: slc = self.get_loc(label) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 6d81d2f28511b..50804f3b4f454 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -672,8 +672,11 @@ def _maybe_cast_slice_bound(self, label, side: str, kind): if self._is_strictly_monotonic_decreasing and len(self) > 1: return upper if side == "left" else lower return lower if side == "left" else upper - elif isinstance(label, date) and not isinstance(label, datetime): - return datetime.combine(label, time(0, 0)) + # GH 35690: Ensure the label matches the time timezone as the index's tz + elif isinstance(label, date): + if not isinstance(label, datetime): + label = datetime.combine(label, time(0, 0)) + return self._maybe_cast_for_get_loc(label) else: return label From 0a9e38aaf5cd79a958314bcc721c84d03d9a5d42 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 21 Aug 2020 23:20:44 -0700 Subject: [PATCH 03/10] Create new test file for get_slice_bound --- .../tests/indexes/datetimes/test_slice_ops.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 pandas/tests/indexes/datetimes/test_slice_ops.py diff --git a/pandas/tests/indexes/datetimes/test_slice_ops.py b/pandas/tests/indexes/datetimes/test_slice_ops.py new file mode 100644 index 0000000000000..f5eecabcbf1ce --- /dev/null +++ b/pandas/tests/indexes/datetimes/test_slice_ops.py @@ -0,0 +1,48 @@ +from datetime import date, datetime + +import pytest + +from pandas import DatetimeIndex, Timestamp, bdate_range + + +def test_get_slice_bounds_invalid_kind(): + with pytest.raises(ValueError, match="Invalid value for kind kwarg"): + DatetimeIndex([]).get_slice_bound(datetime(2020, 1, 1), kind="foo", side="left") + + +def test_get_slice_bounds_invalid_side(): + with pytest.raises(ValueError, match="Invalid value for side kwarg"): + DatetimeIndex([]).get_slice_bound( + datetime(2020, 1, 1), kind=None, side="middle" + ) + + +@pytest.mark.parametrize("box", [date, datetime, Timestamp]) +@pytest.mark.parametrize("kind", ["getitem", "loc", None]) +@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) +def test_get_slice_bounds_within(box, kind, side, expected, tz_aware_fixture): + # GH 35690 + index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) + result = index.get_slice_bound(box(year=2000, month=1, day=7), kind=kind, side=side) + assert result == expected + + +@pytest.mark.parametrize("box", [date, datetime, Timestamp]) +@pytest.mark.parametrize("kind", ["getitem", "loc", None]) +@pytest.mark.parametrize("side", ["left", "right"]) +@pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)]) +def test_get_slice_bounds_outside(box, kind, side, year, expected, tz_aware_fixture): + # GH 35690 + index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) + result = index.get_slice_bound(box(year=year, month=1, day=7), kind=kind, side=side) + assert result == expected + + +@pytest.mark.parametrize("box", [date, datetime, Timestamp]) +@pytest.mark.parametrize("kind", ["getitem", "loc", None]) +def test_slice_locs(box, kind, tz_aware_fixture): + # GH 34077 + index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(tz_aware_fixture) + result = index.slice_locs(box(2010, 1, 1), box(2010, 1, 2)) + expected = (0, 1) + assert result == expected From 460284d0aa1c4a93113d05dda3db366b121c1588 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 21 Aug 2020 23:41:21 -0700 Subject: [PATCH 04/10] Move file and add tests for numeric and object types --- .../indexes/{datetimes => }/test_slice_ops.py | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) rename pandas/tests/indexes/{datetimes => }/test_slice_ops.py (58%) diff --git a/pandas/tests/indexes/datetimes/test_slice_ops.py b/pandas/tests/indexes/test_slice_ops.py similarity index 58% rename from pandas/tests/indexes/datetimes/test_slice_ops.py rename to pandas/tests/indexes/test_slice_ops.py index f5eecabcbf1ce..36bf0ba112912 100644 --- a/pandas/tests/indexes/datetimes/test_slice_ops.py +++ b/pandas/tests/indexes/test_slice_ops.py @@ -2,7 +2,7 @@ import pytest -from pandas import DatetimeIndex, Timestamp, bdate_range +from pandas import DatetimeIndex, Index, Timestamp, bdate_range def test_get_slice_bounds_invalid_kind(): @@ -17,10 +17,36 @@ def test_get_slice_bounds_invalid_side(): ) +@pytest.mark.parametrize("kind", ["getitem", "loc", None]) +@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) +@pytest.mark.parametrize("data, bound", [(range(6), 4), (list("abcdef"), "e")]) +def test_get_slice_bounds_within(kind, side, expected, data, bound): + index = Index(data) + result = index.get_slice_bound(bound, kind=kind, side=side) + assert result == expected + + +@pytest.mark.parametrize("kind", ["getitem", "loc", None]) +@pytest.mark.parametrize("side", ["left", "right"]) +@pytest.mark.parametrize( + "data, bound, expected", + [ + (range(6), -1, 0), + (range(6), 10, 6), + (list("abcdef"), "x", 6), + (list("bcdefg"), "a", 0), + ], +) +def test_get_slice_bounds_outside(kind, side, expected, data, bound): + index = Index(data) + result = index.get_slice_bound(bound, kind=kind, side=side) + assert result == expected + + @pytest.mark.parametrize("box", [date, datetime, Timestamp]) @pytest.mark.parametrize("kind", ["getitem", "loc", None]) @pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) -def test_get_slice_bounds_within(box, kind, side, expected, tz_aware_fixture): +def test_get_slice_bounds_datetime_within(box, kind, side, expected, tz_aware_fixture): # GH 35690 index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) result = index.get_slice_bound(box(year=2000, month=1, day=7), kind=kind, side=side) @@ -31,7 +57,9 @@ def test_get_slice_bounds_within(box, kind, side, expected, tz_aware_fixture): @pytest.mark.parametrize("kind", ["getitem", "loc", None]) @pytest.mark.parametrize("side", ["left", "right"]) @pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)]) -def test_get_slice_bounds_outside(box, kind, side, year, expected, tz_aware_fixture): +def test_get_slice_bounds_datetime_outside( + box, kind, side, year, expected, tz_aware_fixture +): # GH 35690 index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) result = index.get_slice_bound(box(year=year, month=1, day=7), kind=kind, side=side) @@ -40,7 +68,7 @@ def test_get_slice_bounds_outside(box, kind, side, year, expected, tz_aware_fixt @pytest.mark.parametrize("box", [date, datetime, Timestamp]) @pytest.mark.parametrize("kind", ["getitem", "loc", None]) -def test_slice_locs(box, kind, tz_aware_fixture): +def test_slice_datetime_locs(box, kind, tz_aware_fixture): # GH 34077 index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(tz_aware_fixture) result = index.slice_locs(box(2010, 1, 1), box(2010, 1, 2)) From 2b06ce257eb0d3132334e6875c6e5355f9d531a2 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 21 Aug 2020 23:46:12 -0700 Subject: [PATCH 05/10] Add whatsnew --- doc/source/whatsnew/v1.2.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 09a5bcb0917c2..88bb7462b02e6 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -176,7 +176,8 @@ Datetimelike - Bug in :attr:`DatetimeArray.date` where a ``ValueError`` would be raised with a read-only backing array (:issue:`33530`) - Bug in ``NaT`` comparisons failing to raise ``TypeError`` on invalid inequality comparisons (:issue:`35046`) - Bug in :class:`DateOffset` where attributes reconstructed from pickle files differ from original objects when input values exceed normal ranges (e.g months=12) (:issue:`34511`) -- +- Bug in :meth:`DatetimeIndex.get_slice_bound` where ``datetime.date`` objects were not accepted or naive :class:`Timestamp` with a tz-aware :class:`DatetimeIndex` (:issue:`35690`) +- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`) Timedelta ^^^^^^^^^ From 5fface89709af3164e459e8003e53e3c2235e553 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 21 Aug 2020 23:48:34 -0700 Subject: [PATCH 06/10] English --- pandas/core/indexes/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 50804f3b4f454..7636571e92b82 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -672,7 +672,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind): if self._is_strictly_monotonic_decreasing and len(self) > 1: return upper if side == "left" else lower return lower if side == "left" else upper - # GH 35690: Ensure the label matches the time timezone as the index's tz + # GH 35690: Ensure the label matches the timezone of the index's tz elif isinstance(label, date): if not isinstance(label, datetime): label = datetime.combine(label, time(0, 0)) From 93f7162c0c281b8b3700f3f64b794ecc9b406226 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 21 Aug 2020 23:54:06 -0700 Subject: [PATCH 07/10] Skip typing for now --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 59e1949570e3b..6393d12f21b6b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5028,7 +5028,7 @@ def _searchsorted_monotonic(self, label, side="left"): raise ValueError("index must be monotonic increasing or decreasing") - def get_slice_bound(self, label, side: str_t, kind: Optional[str_t]) -> int: + def get_slice_bound(self, label, side: str_t, kind) -> int: """ Calculate slice bound that corresponds to given label. From 54cc06b61c5cf49b357c5be864ffdeb1cf6e0fe3 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 22 Aug 2020 21:46:03 -0700 Subject: [PATCH 08/10] Move conversion logic to _maybe_cast_for_get_loc --- pandas/core/indexes/datetimes.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 7636571e92b82..aa21805290d54 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -627,7 +627,9 @@ def get_loc(self, key, method=None, tolerance=None): raise KeyError(orig_key) from err def _maybe_cast_for_get_loc(self, key) -> Timestamp: - # needed to localize naive datetimes + # needed to localize naive datetimes or dates (GH 35690) + if isinstance(key, date) and not isinstance(key, datetime): + key = datetime.combine(key, time(0, 0)) key = Timestamp(key) if key.tzinfo is None: key = key.tz_localize(self.tz) @@ -672,13 +674,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind): if self._is_strictly_monotonic_decreasing and len(self) > 1: return upper if side == "left" else lower return lower if side == "left" else upper - # GH 35690: Ensure the label matches the timezone of the index's tz - elif isinstance(label, date): - if not isinstance(label, datetime): - label = datetime.combine(label, time(0, 0)) - return self._maybe_cast_for_get_loc(label) - else: - return label + return self._maybe_cast_for_get_loc(label) def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True): freq = getattr(self, "freqstr", getattr(self, "inferred_freq", None)) From 830df027f90901fe29a2be7a59f7f90588cdfde8 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Mon, 24 Aug 2020 22:08:16 -0700 Subject: [PATCH 09/10] Split tests into relevant files, put back assertion, dont need to check for date --- pandas/core/indexes/base.py | 8 +- pandas/core/indexes/datetimes.py | 2 - .../tests/indexes/base_class/test_indexing.py | 26 +++++++ .../tests/indexes/datetimes/test_indexing.py | 42 +++++++++- pandas/tests/indexes/test_numeric.py | 19 +++++ pandas/tests/indexes/test_slice_ops.py | 76 ------------------- 6 files changed, 88 insertions(+), 85 deletions(-) create mode 100644 pandas/tests/indexes/base_class/test_indexing.py delete mode 100644 pandas/tests/indexes/test_slice_ops.py diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 212685429735b..ceb109fdf6d7a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5055,18 +5055,14 @@ def get_slice_bound(self, label, side: str_t, kind) -> int: int Index of label. """ + assert kind in ["loc", "getitem", None] + if side not in ("left", "right"): raise ValueError( "Invalid value for side kwarg, must be either " f"'left' or 'right': {side}" ) - if kind not in ("loc", "getitem", None): - raise ValueError( - "Invalid value for kind kwarg, must be either " - f"'loc' or 'getitem': {kind}" - ) - original_label = label # For datetime indices label may be a string that has to be converted diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index aa21805290d54..366f12739f3cb 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -628,8 +628,6 @@ def get_loc(self, key, method=None, tolerance=None): def _maybe_cast_for_get_loc(self, key) -> Timestamp: # needed to localize naive datetimes or dates (GH 35690) - if isinstance(key, date) and not isinstance(key, datetime): - key = datetime.combine(key, time(0, 0)) key = Timestamp(key) if key.tzinfo is None: key = key.tz_localize(self.tz) diff --git a/pandas/tests/indexes/base_class/test_indexing.py b/pandas/tests/indexes/base_class/test_indexing.py new file mode 100644 index 0000000000000..23906edec2ea7 --- /dev/null +++ b/pandas/tests/indexes/base_class/test_indexing.py @@ -0,0 +1,26 @@ +import pytest + +from pandas import Index + + +class TestGetSliceBounds: + @pytest.mark.parametrize("kind", ["getitem", "loc", None]) + @pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) + def test_get_slice_bounds_within(self, kind, side, expected): + index = Index(list("abcdef")) + result = index.get_slice_bound("e", kind=kind, side=side) + assert result == expected + + @pytest.mark.parametrize("kind", ["getitem", "loc", None]) + @pytest.mark.parametrize("side", ["left", "right"]) + @pytest.mark.parametrize( + "data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0),], + ) + def test_get_slice_bounds_outside(self, kind, side, expected, data, bound): + index = Index(data) + result = index.get_slice_bound(bound, kind=kind, side=side) + assert result == expected + + def test_get_slice_bounds_invalid_side(self): + with pytest.raises(ValueError, match="Invalid value for side kwarg"): + Index([]).get_slice_bound("a", kind=None, side="middle") diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index 5d2c6daba3f57..539d9cb8f06a7 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -6,7 +6,7 @@ from pandas.errors import InvalidIndexError import pandas as pd -from pandas import DatetimeIndex, Index, Timestamp, date_range, notna +from pandas import DatetimeIndex, Index, Timestamp, bdate_range, date_range, notna import pandas._testing as tm from pandas.tseries.offsets import BDay, CDay @@ -665,3 +665,43 @@ def test_get_value(self): with tm.assert_produces_warning(FutureWarning): result = dti.get_value(ser, key.to_datetime64()) assert result == 7 + + +class TestGetSliceBounds: + @pytest.mark.parametrize("box", [date, datetime, Timestamp]) + @pytest.mark.parametrize("kind", ["getitem", "loc", None]) + @pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) + def test_get_slice_bounds_datetime_within( + self, box, kind, side, expected, tz_aware_fixture + ): + # GH 35690 + index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) + result = index.get_slice_bound( + box(year=2000, month=1, day=7), kind=kind, side=side + ) + assert result == expected + + @pytest.mark.parametrize("box", [date, datetime, Timestamp]) + @pytest.mark.parametrize("kind", ["getitem", "loc", None]) + @pytest.mark.parametrize("side", ["left", "right"]) + @pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)]) + def test_get_slice_bounds_datetime_outside( + self, box, kind, side, year, expected, tz_aware_fixture + ): + # GH 35690 + index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) + result = index.get_slice_bound( + box(year=year, month=1, day=7), kind=kind, side=side + ) + assert result == expected + + @pytest.mark.parametrize("box", [date, datetime, Timestamp]) + @pytest.mark.parametrize("kind", ["getitem", "loc", None]) + def test_slice_datetime_locs(self, box, kind, tz_aware_fixture): + # GH 34077 + index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize( + tz_aware_fixture + ) + result = index.slice_locs(box(2010, 1, 1), box(2010, 1, 2)) + expected = (0, 1) + assert result == expected diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index e6f455e60eee3..0f1d8972ffbdf 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -679,3 +679,22 @@ def test_float64_index_difference(): result = string_index.difference(float_index) tm.assert_index_equal(result, string_index) + + +class TestGetSliceBounds: + @pytest.mark.parametrize("kind", ["getitem", "loc", None]) + @pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) + def test_get_slice_bounds_within(self, kind, side, expected): + index = Index(range(6)) + result = index.get_slice_bound(4, kind=kind, side=side) + assert result == expected + + @pytest.mark.parametrize("kind", ["getitem", "loc", None]) + @pytest.mark.parametrize("side", ["left", "right"]) + @pytest.mark.parametrize( + "bound, expected", [(-1, 0), (10, 6),], + ) + def test_get_slice_bounds_outside(self, kind, side, expected, bound): + index = Index(range(6)) + result = index.get_slice_bound(bound, kind=kind, side=side) + assert result == expected diff --git a/pandas/tests/indexes/test_slice_ops.py b/pandas/tests/indexes/test_slice_ops.py deleted file mode 100644 index 36bf0ba112912..0000000000000 --- a/pandas/tests/indexes/test_slice_ops.py +++ /dev/null @@ -1,76 +0,0 @@ -from datetime import date, datetime - -import pytest - -from pandas import DatetimeIndex, Index, Timestamp, bdate_range - - -def test_get_slice_bounds_invalid_kind(): - with pytest.raises(ValueError, match="Invalid value for kind kwarg"): - DatetimeIndex([]).get_slice_bound(datetime(2020, 1, 1), kind="foo", side="left") - - -def test_get_slice_bounds_invalid_side(): - with pytest.raises(ValueError, match="Invalid value for side kwarg"): - DatetimeIndex([]).get_slice_bound( - datetime(2020, 1, 1), kind=None, side="middle" - ) - - -@pytest.mark.parametrize("kind", ["getitem", "loc", None]) -@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) -@pytest.mark.parametrize("data, bound", [(range(6), 4), (list("abcdef"), "e")]) -def test_get_slice_bounds_within(kind, side, expected, data, bound): - index = Index(data) - result = index.get_slice_bound(bound, kind=kind, side=side) - assert result == expected - - -@pytest.mark.parametrize("kind", ["getitem", "loc", None]) -@pytest.mark.parametrize("side", ["left", "right"]) -@pytest.mark.parametrize( - "data, bound, expected", - [ - (range(6), -1, 0), - (range(6), 10, 6), - (list("abcdef"), "x", 6), - (list("bcdefg"), "a", 0), - ], -) -def test_get_slice_bounds_outside(kind, side, expected, data, bound): - index = Index(data) - result = index.get_slice_bound(bound, kind=kind, side=side) - assert result == expected - - -@pytest.mark.parametrize("box", [date, datetime, Timestamp]) -@pytest.mark.parametrize("kind", ["getitem", "loc", None]) -@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) -def test_get_slice_bounds_datetime_within(box, kind, side, expected, tz_aware_fixture): - # GH 35690 - index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) - result = index.get_slice_bound(box(year=2000, month=1, day=7), kind=kind, side=side) - assert result == expected - - -@pytest.mark.parametrize("box", [date, datetime, Timestamp]) -@pytest.mark.parametrize("kind", ["getitem", "loc", None]) -@pytest.mark.parametrize("side", ["left", "right"]) -@pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)]) -def test_get_slice_bounds_datetime_outside( - box, kind, side, year, expected, tz_aware_fixture -): - # GH 35690 - index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture) - result = index.get_slice_bound(box(year=year, month=1, day=7), kind=kind, side=side) - assert result == expected - - -@pytest.mark.parametrize("box", [date, datetime, Timestamp]) -@pytest.mark.parametrize("kind", ["getitem", "loc", None]) -def test_slice_datetime_locs(box, kind, tz_aware_fixture): - # GH 34077 - index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(tz_aware_fixture) - result = index.slice_locs(box(2010, 1, 1), box(2010, 1, 2)) - expected = (0, 1) - assert result == expected From 75a9afbd1741340278a9fa3719a33cd6ad7edd6d Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Mon, 24 Aug 2020 22:13:55 -0700 Subject: [PATCH 10/10] lint --- pandas/tests/indexes/base_class/test_indexing.py | 2 +- pandas/tests/indexes/test_numeric.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_indexing.py b/pandas/tests/indexes/base_class/test_indexing.py index 23906edec2ea7..196c0401a72be 100644 --- a/pandas/tests/indexes/base_class/test_indexing.py +++ b/pandas/tests/indexes/base_class/test_indexing.py @@ -14,7 +14,7 @@ def test_get_slice_bounds_within(self, kind, side, expected): @pytest.mark.parametrize("kind", ["getitem", "loc", None]) @pytest.mark.parametrize("side", ["left", "right"]) @pytest.mark.parametrize( - "data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0),], + "data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0)], ) def test_get_slice_bounds_outside(self, kind, side, expected, data, bound): index = Index(data) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 0f1d8972ffbdf..1ffdbbc9afd3f 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -692,7 +692,7 @@ def test_get_slice_bounds_within(self, kind, side, expected): @pytest.mark.parametrize("kind", ["getitem", "loc", None]) @pytest.mark.parametrize("side", ["left", "right"]) @pytest.mark.parametrize( - "bound, expected", [(-1, 0), (10, 6),], + "bound, expected", [(-1, 0), (10, 6)], ) def test_get_slice_bounds_outside(self, kind, side, expected, bound): index = Index(range(6))