@@ -296,8 +296,8 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
296
296
return result
297
297
298
298
299
- def array_with_unit_to_datetime (ndarray values , object unit ,
300
- str errors = ' coerce' ):
299
+ def array_with_unit_to_datetime (ndarray values , object unit ,
300
+ str errors = ' coerce' , ndarray mask = None ):
301
301
"""
302
302
convert the ndarray according to the unit
303
303
if errors:
@@ -316,7 +316,6 @@ def array_with_unit_to_datetime(ndarray values, object unit,
316
316
Py_ssize_t i, j, n= len (values)
317
317
int64_t m
318
318
ndarray[float64_t] fvalues
319
- ndarray mask
320
319
bint is_ignore = errors== ' ignore'
321
320
bint is_coerce = errors== ' coerce'
322
321
bint is_raise = errors== ' raise'
@@ -329,9 +328,13 @@ def array_with_unit_to_datetime(ndarray values, object unit,
329
328
330
329
if unit == ' ns' :
331
330
if issubclass (values.dtype.type, np.integer):
332
- return values.astype(' M8[ns]' ), tz
333
- # This will return a tz
334
- return array_to_datetime(values.astype(object ), errors = errors)
331
+ result = values.astype(' M8[ns]' )
332
+ else :
333
+ result, tz = array_to_datetime(values.astype(object ), errors = errors)
334
+ if mask is not None :
335
+ iresult = result.view(' i8' )
336
+ iresult[mask] = NPY_NAT
337
+ return result, tz
335
338
336
339
m = cast_from_unit(None , unit)
337
340
@@ -343,7 +346,9 @@ def array_with_unit_to_datetime(ndarray values, object unit,
343
346
if values.dtype.kind == " i" :
344
347
# Note: this condition makes the casting="same_kind" redundant
345
348
iresult = values.astype(' i8' , casting = ' same_kind' , copy = False )
346
- mask = iresult == NPY_NAT
349
+ # If no mask, fill mask by comparing to NPY_NAT constant
350
+ if mask is None :
351
+ mask = iresult == NPY_NAT
347
352
iresult[mask] = 0
348
353
fvalues = iresult.astype(' f8' ) * m
349
354
need_to_iterate = False
0 commit comments