Skip to content

Commit c8efd6b

Browse files
author
Tom Augspurger
committed
Merge pull request #8246 from TomAugspurger/datetime-asof
BUG: DatetimeIndex.asof matches partial dates
2 parents 42ed2a2 + 7ba2393 commit c8efd6b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/v0.15.0.txt

+5
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,8 @@ Bug Fixes
788788
needed interpolating (:issue:`7173`).
789789
- Bug where ``col_space`` was ignored in ``DataFrame.to_string()`` when ``header=False``
790790
(:issue:`8230`).
791+
- Bug with ``DatetimeIndex.asof`` incorrectly matching partial strings and
792+
returning the wrong date (:issue:`8245`).
793+
794+
795+

pandas/core/index.py

+3-2
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

+6
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)