21
21
_ensure_index , _handle_legacy_indexes )
22
22
from pandas .core .indexing import _SeriesIndexer
23
23
from pandas .tseries .index import DatetimeIndex
24
- from pandas .tseries .period import PeriodIndex
24
+ from pandas .tseries .period import PeriodIndex , Period
25
25
from pandas .util import py3compat
26
26
from pandas .util .terminal import get_terminal_size
27
27
import pandas .core .common as com
@@ -2537,11 +2537,18 @@ def asof(self, where):
2537
2537
where = datetools .to_datetime (where )
2538
2538
2539
2539
values = self .values
2540
+ time_index = self .index
2541
+ is_periodindex = isinstance (self .index , PeriodIndex )
2542
+ if is_periodindex :
2543
+ time_index = Index (self .index .values )
2540
2544
2541
2545
if not hasattr (where , '__iter__' ):
2542
- if where < self .index [0 ]:
2546
+ if is_periodindex :
2547
+ where = Period (where , freq = self .index .freq ).ordinal
2548
+
2549
+ if where < time_index [0 ]:
2543
2550
return np .nan
2544
- loc = self . index .searchsorted (where , side = 'right' )
2551
+ loc = time_index .searchsorted (where , side = 'right' )
2545
2552
if loc > 0 :
2546
2553
loc -= 1
2547
2554
while isnull (values [loc ]) and loc > 0 :
@@ -2551,7 +2558,16 @@ def asof(self, where):
2551
2558
if not isinstance (where , Index ):
2552
2559
where = Index (where )
2553
2560
2554
- locs = self .index .asof_locs (where , notnull (values ))
2561
+ where_index = where
2562
+ if is_periodindex :
2563
+ if isinstance (where_index , DatetimeIndex ):
2564
+ where_index = PeriodIndex (where_index .values ,
2565
+ freq = self .index .freq )
2566
+
2567
+ if isinstance (where_index , PeriodIndex ):
2568
+ where_index = Index (where_index .values )
2569
+
2570
+ locs = time_index .asof_locs (where_index , notnull (values ))
2555
2571
new_values = com .take_1d (values , locs )
2556
2572
return Series (new_values , index = where , name = self .name )
2557
2573
0 commit comments