@@ -3196,7 +3196,6 @@ def remove_na(arr):
3196
3196
"""
3197
3197
return arr [notnull (arr )]
3198
3198
3199
-
3200
3199
def _sanitize_array (data , index , dtype = None , copy = False ,
3201
3200
raise_cast_failure = False ):
3202
3201
@@ -3208,7 +3207,13 @@ def _sanitize_array(data, index, dtype=None, copy=False,
3208
3207
else :
3209
3208
data = data .copy ()
3210
3209
3211
- def _try_cast (arr ):
3210
+ def _try_cast (arr , take_fast_path ):
3211
+
3212
+ # perf shortcut as this is the most common case
3213
+ if take_fast_path :
3214
+ if com ._possibly_castable (arr ) and not copy and dtype is None :
3215
+ return arr
3216
+
3212
3217
try :
3213
3218
arr = com ._possibly_cast_to_datetime (arr , dtype )
3214
3219
subarr = pa .array (arr , dtype = dtype , copy = copy )
@@ -3227,7 +3232,7 @@ def _try_cast(arr):
3227
3232
# possibility of nan -> garbage
3228
3233
if com .is_float_dtype (data .dtype ) and com .is_integer_dtype (dtype ):
3229
3234
if not isnull (data ).any ():
3230
- subarr = _try_cast (data )
3235
+ subarr = _try_cast (data , True )
3231
3236
elif copy :
3232
3237
subarr = data .copy ()
3233
3238
else :
@@ -3239,17 +3244,17 @@ def _try_cast(arr):
3239
3244
elif raise_cast_failure :
3240
3245
raise TypeError ('Cannot cast datetime64 to %s' % dtype )
3241
3246
else :
3242
- subarr = _try_cast (data )
3247
+ subarr = _try_cast (data , True )
3243
3248
else :
3244
- subarr = _try_cast (data )
3249
+ subarr = _try_cast (data , True )
3245
3250
3246
3251
if copy :
3247
3252
subarr = data .copy ()
3248
3253
3249
3254
elif isinstance (data , list ) and len (data ) > 0 :
3250
3255
if dtype is not None :
3251
3256
try :
3252
- subarr = _try_cast (data )
3257
+ subarr = _try_cast (data , False )
3253
3258
except Exception :
3254
3259
if raise_cast_failure : # pragma: no cover
3255
3260
raise
@@ -3262,7 +3267,7 @@ def _try_cast(arr):
3262
3267
subarr = com ._possibly_cast_to_datetime (subarr , dtype )
3263
3268
3264
3269
else :
3265
- subarr = _try_cast (data )
3270
+ subarr = _try_cast (data , False )
3266
3271
3267
3272
# scalar like
3268
3273
if subarr .ndim == 0 :
0 commit comments