@@ -67,12 +67,11 @@ def _dt_index_cmp(opname):
6767 def wrapper (self , other ):
6868 func = getattr (super (DatetimeIndex , self ), opname )
6969 if isinstance (other , datetime ):
70- func = getattr (self , opname )
71- other = _to_m8 (other )
70+ other = _to_m8 (other , tz = self .tz )
7271 elif isinstance (other , list ):
7372 other = DatetimeIndex (other )
7473 elif isinstance (other , basestring ):
75- other = _to_m8 (Timestamp ( other , tz = self .tz ) )
74+ other = _to_m8 (other , tz = self .tz )
7675 elif not isinstance (other , np .ndarray ):
7776 other = _ensure_datetime64 (other )
7877 result = func (other )
@@ -1092,6 +1091,11 @@ def get_value(self, series, key):
10921091 Fast lookup of value from 1-dimensional ndarray. Only use this if you
10931092 know what you're doing
10941093 """
1094+ if isinstance (key , datetime ):
1095+ # needed to localize naive datetimes
1096+ stamp = Timestamp (key , tz = self .tz )
1097+ return self ._engine .get_value (series , stamp )
1098+
10951099 try :
10961100 return Index .get_value (self , series , key )
10971101 except KeyError :
@@ -1106,13 +1110,10 @@ def get_value(self, series, key):
11061110 return series .take (locs )
11071111
11081112 try :
1109- if isinstance (key , basestring ):
1110- stamp = Timestamp (key , tz = self .tz )
1111- else :
1112- stamp = Timestamp (key )
1113+ stamp = Timestamp (key , tz = self .tz )
11131114 return self ._engine .get_value (series , stamp )
1114- except KeyError :
1115- raise KeyError (stamp )
1115+ except ( KeyError , ValueError ) :
1116+ raise KeyError (key )
11161117
11171118 def get_loc (self , key ):
11181119 """
@@ -1122,22 +1123,24 @@ def get_loc(self, key):
11221123 -------
11231124 loc : int
11241125 """
1126+ if isinstance (key , datetime ):
1127+ # needed to localize naive datetimes
1128+ stamp = Timestamp (key , tz = self .tz )
1129+ return self ._engine .get_loc (stamp )
1130+
11251131 try :
1126- return self . _engine . get_loc (key )
1127- except KeyError :
1132+ return Index . get_loc (self , key )
1133+ except ( KeyError , ValueError ) :
11281134 try :
11291135 return self ._get_string_slice (key )
11301136 except (TypeError , KeyError , ValueError ):
11311137 pass
11321138
11331139 if isinstance (key , time ):
11341140 return self .indexer_at_time (key )
1135-
1141+
11361142 try :
1137- if isinstance (key , basestring ):
1138- stamp = Timestamp (key , tz = self .tz )
1139- else :
1140- stamp = Timestamp (key )
1143+ stamp = Timestamp (key , tz = self .tz )
11411144 return self ._engine .get_loc (stamp )
11421145 except (KeyError , ValueError ):
11431146 raise KeyError (key )
@@ -1256,7 +1259,7 @@ def searchsorted(self, key, side='left'):
12561259 if isinstance (key , np .ndarray ):
12571260 key = np .array (key , dtype = _NS_DTYPE , copy = False )
12581261 else :
1259- key = _to_m8 (key )
1262+ key = _to_m8 (key , tz = self . tz )
12601263
12611264 return self .values .searchsorted (key , side = side )
12621265
@@ -1345,7 +1348,7 @@ def insert(self, loc, item):
13451348 new_index : Index
13461349 """
13471350 if isinstance (item , datetime ):
1348- item = _to_m8 (item )
1351+ item = _to_m8 (item , tz = self . tz )
13491352
13501353 new_index = np .concatenate ((self [:loc ].asi8 ,
13511354 [item .view (np .int64 )],
@@ -1619,13 +1622,13 @@ def bdate_range(start=None, end=None, periods=None, freq='B', tz=None,
16191622 freq = freq , tz = tz , normalize = normalize , name = name )
16201623
16211624
1622- def _to_m8 (key ):
1625+ def _to_m8 (key , tz = None ):
16231626 '''
16241627 Timestamp-like => dt64
16251628 '''
1626- if not isinstance (key , ( Timestamp , datetime ) ):
1629+ if not isinstance (key , Timestamp ):
16271630 # this also converts strings
1628- key = Timestamp (key )
1631+ key = Timestamp (key , tz = tz )
16291632
16301633 return np .int64 (tslib .pydt_to_i8 (key )).view (_NS_DTYPE )
16311634
0 commit comments