10
10
import numpy as np
11
11
import pandas as pd
12
12
13
- from pandas ._libs import (lib , index as libindex ,
14
- algos as libalgos , ops as libops )
13
+ from pandas ._libs import algos as libalgos , ops as libops
15
14
16
15
from pandas import compat
17
16
from pandas .util ._decorators import Appender
@@ -1127,24 +1126,20 @@ def na_op(x, y):
1127
1126
# integer comparisons
1128
1127
1129
1128
# we have a datetime/timedelta and may need to convert
1129
+ assert not needs_i8_conversion (x )
1130
1130
mask = None
1131
- if (needs_i8_conversion (x ) or
1132
- (not is_scalar (y ) and needs_i8_conversion (y ))):
1133
-
1134
- if is_scalar (y ):
1135
- mask = isna (x )
1136
- y = libindex .convert_scalar (x , com ._values_from_object (y ))
1137
- else :
1138
- mask = isna (x ) | isna (y )
1139
- y = y .view ('i8' )
1131
+ if not is_scalar (y ) and needs_i8_conversion (y ):
1132
+ mask = isna (x ) | isna (y )
1133
+ y = y .view ('i8' )
1140
1134
x = x .view ('i8' )
1141
1135
1142
- try :
1136
+ method = getattr (x , name , None )
1137
+ if method is not None :
1143
1138
with np .errstate (all = 'ignore' ):
1144
- result = getattr ( x , name ) (y )
1139
+ result = method (y )
1145
1140
if result is NotImplemented :
1146
1141
raise TypeError ("invalid type comparison" )
1147
- except AttributeError :
1142
+ else :
1148
1143
result = op (x , y )
1149
1144
1150
1145
if mask is not None and mask .any ():
@@ -1174,6 +1169,14 @@ def wrapper(self, other, axis=None):
1174
1169
return self ._constructor (res_values , index = self .index ,
1175
1170
name = res_name )
1176
1171
1172
+ if is_datetime64_dtype (self ) or is_datetime64tz_dtype (self ):
1173
+ # Dispatch to DatetimeIndex to ensure identical
1174
+ # Series/Index behavior
1175
+ res_values = dispatch_to_index_op (op , self , other ,
1176
+ pd .DatetimeIndex )
1177
+ return self ._constructor (res_values , index = self .index ,
1178
+ name = res_name )
1179
+
1177
1180
elif is_timedelta64_dtype (self ):
1178
1181
res_values = dispatch_to_index_op (op , self , other ,
1179
1182
pd .TimedeltaIndex )
@@ -1191,8 +1194,7 @@ def wrapper(self, other, axis=None):
1191
1194
elif isinstance (other , (np .ndarray , pd .Index )):
1192
1195
# do not check length of zerodim array
1193
1196
# as it will broadcast
1194
- if (not is_scalar (lib .item_from_zerodim (other )) and
1195
- len (self ) != len (other )):
1197
+ if other .ndim != 0 and len (self ) != len (other ):
1196
1198
raise ValueError ('Lengths must match to compare' )
1197
1199
1198
1200
res_values = na_op (self .values , np .asarray (other ))
0 commit comments