Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ Datetimelike
^^^^^^^^^^^^
- 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`)
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
- 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`)
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
- Bug in :class:`Timestamp` constructor failing to raise when given a ``np.datetime64`` object with non-standard unit (:issue:`25611`)
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
Expand Down
11 changes: 2 additions & 9 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,8 @@ def _partial_date_slice(
unbox = self._data._unbox

if self.is_monotonic_increasing:
if len(self) and (
(t1 < self[0] and t2 < self[0]) or (t1 > self[-1] and t2 > self[-1])
):
# we are out of range
raise KeyError

# TODO: does this depend on being monotonic _increasing_?

# a monotonic (sorted) series can be sliced
# a monotonic (sorted) series can be searched in ologn
# if date is not found, the empty slice will be returned
left = vals.searchsorted(unbox(t1), side="left")
right = vals.searchsorted(unbox(t2), side="right")
return slice(left, right)
Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/indexes/datetimes/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,16 @@ def test_partial_slice_second_precision(self):
tm.assert_series_equal(s["2005-1-1 00:01:00"], s.iloc[10:])

assert s[Timestamp("2005-1-1 00:00:59.999990")] == s.iloc[0]
with pytest.raises(KeyError, match="2005-1-1 00:00:00"):
s["2005-1-1 00:00:00"]
tm.assert_series_equal(s["2005-1-1 00:00:00"], s.iloc[0:0])

def test_partial_slice_higher_precision_empty(self):
# GH25803
s = Series(
[1, 2, 3],
DatetimeIndex(["2018-01-01T01:01", "2018-02-02T01:01", "2018-02-02T02:02"]),
)
tm.assert_series_equal(s.loc["2018-03-03"], s.iloc[0:0])
assert s.loc["2018-03-03"].size == 0

def test_partial_slicing_dataframe(self):
# GH14856
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def test_getitem_partial(self):
rng = period_range("2007-01", periods=50, freq="M")
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), rng)

with pytest.raises(KeyError, match=r"^'2006'$"):
ts["2006"]
# GH 25803
assert len(ts["2006"]) == 0

result = ts["2008"]
assert (result.index.year == 2008).all()
Expand Down
Loading