@@ -25,6 +25,8 @@ from pandas._libs.hashtable cimport HashTable
25
25
from pandas._libs import algos, hashtable as _hash
26
26
from pandas._libs.tslibs import Timestamp, Timedelta, period as periodlib
27
27
from pandas._libs.missing import checknull
28
+ from pandas._libs.tslibs.util cimport is_period_object
29
+
28
30
29
31
cdef int64_t NPY_NAT = util.get_nat()
30
32
@@ -498,7 +500,7 @@ cdef class TimedeltaEngine(DatetimeEngine):
498
500
cdef class PeriodEngine(Int64Engine):
499
501
500
502
cdef _get_index_values(self ):
501
- return super (PeriodEngine, self ).vgetter()
503
+ return super (PeriodEngine, self ).vgetter().view( ' i8 ' )
502
504
503
505
cpdef _call_map_locations(self , values):
504
506
super (PeriodEngine, self )._call_map_locations(values.view(' i8' ))
@@ -537,6 +539,43 @@ cdef class PeriodEngine(Int64Engine):
537
539
538
540
return super (PeriodEngine, self ).get_indexer_non_unique(ordinal_array)
539
541
542
+ cpdef get_loc(self , object key):
543
+ cdef:
544
+ int64_t loc
545
+ if is_definitely_invalid_key(key):
546
+ raise TypeError (" '{val}' is an invalid key" .format(val = key))
547
+
548
+ ordinal = key.ordinal if is_period_object(key) else key
549
+ self ._ordinal_check_type(key, ordinal)
550
+
551
+ if self .over_size_threshold and self .is_monotonic_increasing:
552
+ if not self .is_unique:
553
+ return self ._get_loc_duplicates(key)
554
+
555
+ values = self ._get_index_values()
556
+
557
+ try :
558
+ loc = values.searchsorted(ordinal, side = ' left' )
559
+ except TypeError :
560
+ raise KeyError (key)
561
+
562
+ if loc == len (values) or values[loc] != ordinal:
563
+ raise KeyError (key)
564
+ return loc
565
+
566
+ self ._ensure_mapping_populated()
567
+ if not self .unique:
568
+ return self ._get_loc_duplicates(ordinal)
569
+
570
+ try :
571
+ return self .mapping.get_item(ordinal)
572
+ except KeyError :
573
+ raise KeyError (key)
574
+
575
+ cdef inline _ordinal_check_type(self , object key, ordinal):
576
+ if not util.is_integer_object(ordinal):
577
+ raise KeyError (key)
578
+
540
579
541
580
cpdef convert_scalar(ndarray arr, object value):
542
581
# we don't turn integers
0 commit comments