@@ -296,17 +296,34 @@ 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 ,
299
+ def array_with_unit_to_datetime (ndarray values , ndarray mask , object unit ,
300
300
str errors = ' coerce' ):
301
301
"""
302
- convert the ndarray according to the unit
302
+ Convert the ndarray to datetime according to the time unit.
303
+
304
+ This function converts an array of objects into a numpy array of
305
+ datetime64[ns]. It returns the converted array
306
+ and also returns the timezone offset
307
+
303
308
if errors:
304
309
- raise: return converted values or raise OutOfBoundsDatetime
305
310
if out of range on the conversion or
306
311
ValueError for other conversions (e.g. a string)
307
312
- ignore: return non-convertible values as the same unit
308
313
- coerce: NaT for non-convertibles
309
314
315
+ Parameters
316
+ ----------
317
+ values : ndarray of object
318
+ Date-like objects to convert
319
+ mask : ndarray of bool
320
+ Not-a-time mask for non-nullable integer types conversion,
321
+ can be None
322
+ unit : object
323
+ Time unit to use during conversion
324
+ errors : str, default 'raise'
325
+ Error behavior when parsing
326
+
310
327
Returns
311
328
-------
312
329
result : ndarray of m8 values
@@ -316,7 +333,6 @@ def array_with_unit_to_datetime(ndarray values, object unit,
316
333
Py_ssize_t i, j, n= len (values)
317
334
int64_t m
318
335
ndarray[float64_t] fvalues
319
- ndarray mask
320
336
bint is_ignore = errors== ' ignore'
321
337
bint is_coerce = errors== ' coerce'
322
338
bint is_raise = errors== ' raise'
@@ -329,9 +345,13 @@ def array_with_unit_to_datetime(ndarray values, object unit,
329
345
330
346
if unit == ' ns' :
331
347
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)
348
+ result = values.astype(' M8[ns]' )
349
+ else :
350
+ result, tz = array_to_datetime(values.astype(object ), errors = errors)
351
+ if mask is not None :
352
+ iresult = result.view(' i8' )
353
+ iresult[mask] = NPY_NAT
354
+ return result, tz
335
355
336
356
m = cast_from_unit(None , unit)
337
357
@@ -343,7 +363,9 @@ def array_with_unit_to_datetime(ndarray values, object unit,
343
363
if values.dtype.kind == " i" :
344
364
# Note: this condition makes the casting="same_kind" redundant
345
365
iresult = values.astype(' i8' , casting = ' same_kind' , copy = False )
346
- mask = iresult == NPY_NAT
366
+ # If no mask, fill mask by comparing to NPY_NAT constant
367
+ if mask is None :
368
+ mask = iresult == NPY_NAT
347
369
iresult[mask] = 0
348
370
fvalues = iresult.astype(' f8' ) * m
349
371
need_to_iterate = False
0 commit comments