Skip to content
Merged
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 @@ -983,6 +983,7 @@ Datetimelike
- Bug in :meth:`DataFrame.min` and :meth:`DataFrame.max` casting ``datetime64`` and ``timedelta64`` columns to ``float64`` and losing precision (:issue:`60850`)
- Bug in :meth:`Dataframe.agg` with df with missing values resulting in IndexError (:issue:`58810`)
- Bug in :meth:`DateOffset.rollback` (and subclass methods) with ``normalize=True`` rolling back one offset too long (:issue:`32616`)
- Bug in :meth:`DatetimeIndex.asof` with a string key giving incorrect results (:issue:`50946`)
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` does not raise on Custom business days frequencies bigger then "1C" (:issue:`58664`)
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` returning ``False`` on double-digit frequencies (:issue:`58523`)
- Bug in :meth:`DatetimeIndex.union` and :meth:`DatetimeIndex.intersection` when ``unit`` was non-nanosecond (:issue:`59036`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5675,7 +5675,7 @@ def asof(self, label):
return self._na_value
else:
if isinstance(loc, slice):
loc = loc.indices(len(self))[-1]
return self[loc][-1]

return self[loc]

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexes/datetimes/methods/test_asof.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import timedelta

from pandas import (
DatetimeIndex,
Index,
Timestamp,
date_range,
Expand Down Expand Up @@ -28,3 +29,18 @@ def test_asof(self):

dt = index[0].to_pydatetime()
assert isinstance(index.asof(dt), Timestamp)

def test_asof_datetime_string(self):
# GH#50946

dti = date_range("2021-08-05", "2021-08-10", freq="1D")

key = "2021-08-09"
res = dti.asof(key)
exp = dti[4]
assert res == exp

# add a non-midnight time caused a bug
dti2 = DatetimeIndex(list(dti) + ["2021-08-11 00:00:01"])
res = dti2.asof(key)
assert res == exp
Loading