@@ -18,7 +18,6 @@ import_datetime()
18
18
19
19
cimport numpy as cnp
20
20
from numpy cimport (
21
- float64_t,
22
21
int64_t,
23
22
ndarray,
24
23
)
@@ -231,7 +230,7 @@ def format_array_from_datetime(
231
230
232
231
233
232
def array_with_unit_to_datetime (
234
- ndarray values ,
233
+ ndarray[object] values ,
235
234
str unit ,
236
235
str errors = " coerce"
237
236
):
@@ -266,70 +265,24 @@ def array_with_unit_to_datetime(
266
265
cdef:
267
266
Py_ssize_t i, n= len (values)
268
267
int64_t mult
269
- int prec = 0
270
- ndarray[float64_t] fvalues
271
268
bint is_ignore = errors== " ignore"
272
269
bint is_coerce = errors== " coerce"
273
270
bint is_raise = errors== " raise"
274
- bint need_to_iterate = True
275
271
ndarray[int64_t] iresult
276
272
ndarray[object ] oresult
277
- ndarray mask
278
273
object tz = None
279
274
280
275
assert is_ignore or is_coerce or is_raise
281
276
282
277
if unit == " ns" :
283
- if issubclass (values.dtype.type, (np.integer, np.float_)):
284
- result = values.astype(" M8[ns]" , copy = False )
285
- else :
286
- result, tz = array_to_datetime(
287
- values.astype(object , copy = False ),
288
- errors = errors,
289
- )
278
+ result, tz = array_to_datetime(
279
+ values.astype(object , copy = False ),
280
+ errors = errors,
281
+ )
290
282
return result, tz
291
283
292
284
mult, _ = precision_from_unit(unit)
293
285
294
- if is_raise:
295
- # try a quick conversion to i8/f8
296
- # if we have nulls that are not type-compat
297
- # then need to iterate
298
-
299
- if values.dtype.kind in [" i" , " f" , " u" ]:
300
- iresult = values.astype(" i8" , copy = False )
301
- # fill missing values by comparing to NPY_NAT
302
- mask = iresult == NPY_NAT
303
- # Trying to Convert NaN to integer results in undefined
304
- # behaviour, so handle it explicitly (see GH #48705)
305
- if values.dtype.kind == " f" :
306
- mask |= values != values
307
- iresult[mask] = 0
308
- fvalues = iresult.astype(" f8" ) * mult
309
- need_to_iterate = False
310
-
311
- if not need_to_iterate:
312
- # check the bounds
313
- if (fvalues < Timestamp.min.value).any() or (
314
- (fvalues > Timestamp.max.value).any()
315
- ):
316
- raise OutOfBoundsDatetime(f" cannot convert input with unit '{unit}'" )
317
-
318
- if values.dtype.kind in [" i" , " u" ]:
319
- result = (iresult * mult).astype(" M8[ns]" )
320
-
321
- elif values.dtype.kind == " f" :
322
- fresult = (values * mult).astype(" f8" )
323
- fresult[mask] = 0
324
- if prec:
325
- fresult = round (fresult, prec)
326
- result = fresult.astype(" M8[ns]" , copy = False )
327
-
328
- iresult = result.view(" i8" )
329
- iresult[mask] = NPY_NAT
330
-
331
- return result, tz
332
-
333
286
result = np.empty(n, dtype = " M8[ns]" )
334
287
iresult = result.view(" i8" )
335
288
0 commit comments