Skip to content

Commit 7ba2393

Browse files
author
TomAugspurger
committed
BUG: DatetimeIndex.asof matches partial dates
1 parent d5c9a22 commit 7ba2393

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/v0.15.0.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,8 @@ Bug Fixes
770770
needed interpolating (:issue:`7173`).
771771
- Bug where ``col_space`` was ignored in ``DataFrame.to_string()`` when ``header=False``
772772
(:issue:`8230`).
773+
- Bug with ``DatetimeIndex.asof`` incorrectly matching partial strings and
774+
returning the wrong date (:issue:`8245`).
775+
776+
777+

pandas/core/index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,15 +1052,16 @@ def asof(self, label):
10521052
if isinstance(label, (Index, ABCSeries, np.ndarray)):
10531053
raise TypeError('%s' % type(label))
10541054

1055+
if not isinstance(label, Timestamp):
1056+
label = Timestamp(label)
1057+
10551058
if label not in self:
10561059
loc = self.searchsorted(label, side='left')
10571060
if loc > 0:
10581061
return self[loc - 1]
10591062
else:
10601063
return np.nan
10611064

1062-
if not isinstance(label, Timestamp):
1063-
label = Timestamp(label)
10641065
return label
10651066

10661067
def asof_locs(self, where, mask):

pandas/tests/test_index.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ def test_asof(self):
390390
d = self.dateIndex[0].to_datetime()
391391
tm.assert_isinstance(self.dateIndex.asof(d), Timestamp)
392392

393+
def test_asof_datetime_partial(self):
394+
idx = pd.date_range('2010-01-01', periods=2, freq='m')
395+
expected = Timestamp('2010-01-31')
396+
result = idx.asof('2010-02')
397+
self.assertEqual(result, expected)
398+
393399
def test_nanosecond_index_access(self):
394400
s = Series([Timestamp('20130101')]).values.view('i8')[0]
395401
r = DatetimeIndex([s + 50 + i for i in range(100)])

0 commit comments

Comments
 (0)