Skip to content

Commit 628583d

Browse files
committed
Fix: Indexing with dates of higher resolution should return empty slice instead raising KeyError
1 parent db31f6a commit 628583d

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ Datetimelike
944944
^^^^^^^^^^^^
945945
- Bug in :attr:`is_year_start` where a DateTimeIndex constructed via a date_range with frequency 'MS' wouldn't have the correct year or quarter start attributes (:issue:`57377`)
946946
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
947+
- Bug in :class:`DatetimeIndex` returning KeyError for out of range, failing to return empty slice when the resolution is greater than the current datetime objects. (:issue:`25803`)
947948
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
948949
- Bug in :class:`Timestamp` constructor failing to raise when given a ``np.datetime64`` object with non-standard unit (:issue:`25611`)
949950
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)

pandas/core/indexes/datetimelike.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,8 @@ def _partial_date_slice(
317317
unbox = self._data._unbox
318318

319319
if self.is_monotonic_increasing:
320-
if len(self) and (
321-
(t1 < self[0] and t2 < self[0]) or (t1 > self[-1] and t2 > self[-1])
322-
):
323-
# we are out of range
324-
raise KeyError
325-
326-
# TODO: does this depend on being monotonic _increasing_?
327-
328-
# a monotonic (sorted) series can be sliced
320+
# a monotonic (sorted) series can be searched in ologn
321+
# if date is not found, the empty slice will be returned
329322
left = vals.searchsorted(unbox(t1), side="left")
330323
right = vals.searchsorted(unbox(t2), side="right")
331324
return slice(left, right)

pandas/tests/indexes/datetimes/test_partial_slicing.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,16 @@ def test_partial_slice_second_precision(self):
247247
tm.assert_series_equal(s["2005-1-1 00:01:00"], s.iloc[10:])
248248

249249
assert s[Timestamp("2005-1-1 00:00:59.999990")] == s.iloc[0]
250-
with pytest.raises(KeyError, match="2005-1-1 00:00:00"):
251-
s["2005-1-1 00:00:00"]
250+
tm.assert_series_equal(s["2005-1-1 00:00:00"], s.iloc[0:0])
251+
252+
def test_partial_slice_higher_precision_empty(self):
253+
# GH25803
254+
s = Series(
255+
[1, 2, 3],
256+
DatetimeIndex(["2018-01-01T01:01", "2018-02-02T01:01", "2018-02-02T02:02"]),
257+
)
258+
tm.assert_series_equal(s.loc["2018-03-03"], s.iloc[0:0])
259+
assert s.loc["2018-03-03"].size == 0
252260

253261
def test_partial_slicing_dataframe(self):
254262
# GH14856

pandas/tests/indexes/period/test_indexing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ def test_getitem_index(self):
111111
def test_getitem_partial(self):
112112
rng = period_range("2007-01", periods=50, freq="M")
113113
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), rng)
114-
115-
with pytest.raises(KeyError, match=r"^'2006'$"):
116-
ts["2006"]
114+
115+
# GH 25803
116+
assert(ts['2006'] == slice(0, 0))
117117

118118
result = ts["2008"]
119119
assert (result.index.year == 2008).all()

0 commit comments

Comments
 (0)