@@ -67,12 +67,11 @@ def _dt_index_cmp(opname):
67
67
def wrapper (self , other ):
68
68
func = getattr (super (DatetimeIndex , self ), opname )
69
69
if isinstance (other , datetime ):
70
- func = getattr (self , opname )
71
- other = _to_m8 (other )
70
+ other = _to_m8 (other , tz = self .tz )
72
71
elif isinstance (other , list ):
73
72
other = DatetimeIndex (other )
74
73
elif isinstance (other , basestring ):
75
- other = _to_m8 (Timestamp ( other , tz = self .tz ) )
74
+ other = _to_m8 (other , tz = self .tz )
76
75
elif not isinstance (other , np .ndarray ):
77
76
other = _ensure_datetime64 (other )
78
77
result = func (other )
@@ -1092,6 +1091,11 @@ def get_value(self, series, key):
1092
1091
Fast lookup of value from 1-dimensional ndarray. Only use this if you
1093
1092
know what you're doing
1094
1093
"""
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
+
1095
1099
try :
1096
1100
return Index .get_value (self , series , key )
1097
1101
except KeyError :
@@ -1106,13 +1110,10 @@ def get_value(self, series, key):
1106
1110
return series .take (locs )
1107
1111
1108
1112
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 )
1113
1114
return self ._engine .get_value (series , stamp )
1114
- except KeyError :
1115
- raise KeyError (stamp )
1115
+ except ( KeyError , ValueError ) :
1116
+ raise KeyError (key )
1116
1117
1117
1118
def get_loc (self , key ):
1118
1119
"""
@@ -1122,22 +1123,24 @@ def get_loc(self, key):
1122
1123
-------
1123
1124
loc : int
1124
1125
"""
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
+
1125
1131
try :
1126
- return self . _engine . get_loc (key )
1127
- except KeyError :
1132
+ return Index . get_loc (self , key )
1133
+ except ( KeyError , ValueError ) :
1128
1134
try :
1129
1135
return self ._get_string_slice (key )
1130
1136
except (TypeError , KeyError , ValueError ):
1131
1137
pass
1132
1138
1133
1139
if isinstance (key , time ):
1134
1140
return self .indexer_at_time (key )
1135
-
1141
+
1136
1142
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 )
1141
1144
return self ._engine .get_loc (stamp )
1142
1145
except (KeyError , ValueError ):
1143
1146
raise KeyError (key )
@@ -1256,7 +1259,7 @@ def searchsorted(self, key, side='left'):
1256
1259
if isinstance (key , np .ndarray ):
1257
1260
key = np .array (key , dtype = _NS_DTYPE , copy = False )
1258
1261
else :
1259
- key = _to_m8 (key )
1262
+ key = _to_m8 (key , tz = self . tz )
1260
1263
1261
1264
return self .values .searchsorted (key , side = side )
1262
1265
@@ -1345,7 +1348,7 @@ def insert(self, loc, item):
1345
1348
new_index : Index
1346
1349
"""
1347
1350
if isinstance (item , datetime ):
1348
- item = _to_m8 (item )
1351
+ item = _to_m8 (item , tz = self . tz )
1349
1352
1350
1353
new_index = np .concatenate ((self [:loc ].asi8 ,
1351
1354
[item .view (np .int64 )],
@@ -1619,13 +1622,13 @@ def bdate_range(start=None, end=None, periods=None, freq='B', tz=None,
1619
1622
freq = freq , tz = tz , normalize = normalize , name = name )
1620
1623
1621
1624
1622
- def _to_m8 (key ):
1625
+ def _to_m8 (key , tz = None ):
1623
1626
'''
1624
1627
Timestamp-like => dt64
1625
1628
'''
1626
- if not isinstance (key , ( Timestamp , datetime ) ):
1629
+ if not isinstance (key , Timestamp ):
1627
1630
# this also converts strings
1628
- key = Timestamp (key )
1631
+ key = Timestamp (key , tz = tz )
1629
1632
1630
1633
return np .int64 (tslib .pydt_to_i8 (key )).view (_NS_DTYPE )
1631
1634
0 commit comments