@@ -275,44 +275,38 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
275
275
int64_t[:] deltas
276
276
str typ
277
277
Py_ssize_t[:] pos
278
- int64_t delta, local_val
279
-
280
- if tz is None or is_utc(tz):
281
- with nogil:
282
- for i in range (n):
283
- if stamps[i] == NPY_NAT:
284
- result[i] = NPY_NAT
285
- continue
286
- local_val = stamps[i]
287
- result[i] = normalize_i8_stamp(local_val)
278
+ int64_t local_val, delta = NPY_NAT
279
+ bint use_utc = False , use_tzlocal = False , use_fixed = False
280
+
281
+ if is_utc(tz) or tz is None :
282
+ use_utc = True
288
283
elif is_tzlocal(tz):
289
- for i in range (n):
290
- if stamps[i] == NPY_NAT:
291
- result[i] = NPY_NAT
292
- continue
293
- local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
294
- result[i] = normalize_i8_stamp(local_val)
284
+ use_tzlocal = True
295
285
else :
296
- # Adjust datetime64 timestamp, recompute datetimestruct
297
286
trans, deltas, typ = get_dst_info(tz)
298
-
299
287
if typ not in [" pytz" , " dateutil" ]:
300
288
# static/fixed; in this case we know that len(delta) == 1
289
+ use_fixed = True
301
290
delta = deltas[0 ]
302
- for i in range (n):
303
- if stamps[i] == NPY_NAT:
304
- result[i] = NPY_NAT
305
- continue
306
- local_val = stamps[i] + delta
307
- result[i] = normalize_i8_stamp(local_val)
308
291
else :
309
292
pos = trans.searchsorted(stamps, side = " right" ) - 1
310
- for i in range (n):
311
- if stamps[i] == NPY_NAT:
312
- result[i] = NPY_NAT
313
- continue
314
- local_val = stamps[i] + deltas[pos[i]]
315
- result[i] = normalize_i8_stamp(local_val)
293
+
294
+ for i in range (n):
295
+ # TODO: reinstate nogil for use_utc case?
296
+ if stamps[i] == NPY_NAT:
297
+ result[i] = NPY_NAT
298
+ continue
299
+
300
+ if use_utc:
301
+ local_val = stamps[i]
302
+ elif use_tzlocal:
303
+ local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
304
+ elif use_fixed:
305
+ local_val = stamps[i] + delta
306
+ else :
307
+ local_val = stamps[i] + deltas[pos[i]]
308
+
309
+ result[i] = normalize_i8_stamp(local_val)
316
310
317
311
return result.base # `.base` to access underlying ndarray
318
312
@@ -339,40 +333,36 @@ def is_date_array_normalized(const int64_t[:] stamps, tzinfo tz=None):
339
333
ndarray[int64_t] trans
340
334
int64_t[:] deltas
341
335
intp_t[:] pos
342
- int64_t local_val, delta
336
+ int64_t local_val, delta = NPY_NAT
343
337
str typ
344
338
int64_t day_nanos = 24 * 3600 * 1 _000_000_000
339
+ bint use_utc = False , use_tzlocal = False , use_fixed = False
345
340
346
- if tz is None or is_utc(tz):
347
- for i in range (n):
348
- local_val = stamps[i]
349
- if local_val % day_nanos != 0 :
350
- return False
351
-
341
+ if is_utc(tz) or tz is None :
342
+ use_utc = True
352
343
elif is_tzlocal(tz):
353
- for i in range (n):
354
- local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
355
- if local_val % day_nanos != 0 :
356
- return False
344
+ use_tzlocal = True
357
345
else :
358
346
trans, deltas, typ = get_dst_info(tz)
359
-
360
347
if typ not in [" pytz" , " dateutil" ]:
361
348
# static/fixed; in this case we know that len(delta) == 1
349
+ use_fixed = True
362
350
delta = deltas[0 ]
363
- for i in range (n):
364
- # Adjust datetime64 timestamp, recompute datetimestruct
365
- local_val = stamps[i] + delta
366
- if local_val % day_nanos != 0 :
367
- return False
351
+ else :
352
+ pos = trans.searchsorted(stamps, side = " right" ) - 1
368
353
354
+ for i in range (n):
355
+ if use_utc:
356
+ local_val = stamps[i]
357
+ elif use_tzlocal:
358
+ local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
359
+ elif use_fixed:
360
+ local_val = stamps[i] + delta
369
361
else :
370
- pos = trans.searchsorted(stamps) - 1
371
- for i in range (n):
372
- # Adjust datetime64 timestamp, recompute datetimestruct
373
- local_val = stamps[i] + deltas[pos[i]]
374
- if local_val % day_nanos != 0 :
375
- return False
362
+ local_val = stamps[i] + deltas[pos[i]]
363
+
364
+ if local_val % day_nanos != 0 :
365
+ return False
376
366
377
367
return True
378
368
@@ -390,45 +380,38 @@ def dt64arr_to_periodarr(const int64_t[:] stamps, int freq, tzinfo tz):
390
380
int64_t[:] deltas
391
381
Py_ssize_t[:] pos
392
382
npy_datetimestruct dts
393
- int64_t local_val
383
+ int64_t local_val, delta = NPY_NAT
384
+ bint use_utc = False , use_tzlocal = False , use_fixed = False
394
385
395
386
if is_utc(tz) or tz is None :
396
- with nogil:
397
- for i in range (n):
398
- if stamps[i] == NPY_NAT:
399
- result[i] = NPY_NAT
400
- continue
401
- dt64_to_dtstruct(stamps[i], & dts)
402
- result[i] = get_period_ordinal(& dts, freq)
403
-
387
+ use_utc = True
404
388
elif is_tzlocal(tz):
405
- for i in range (n):
406
- if stamps[i] == NPY_NAT:
407
- result[i] = NPY_NAT
408
- continue
409
- local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
410
- dt64_to_dtstruct(local_val, & dts)
411
- result[i] = get_period_ordinal(& dts, freq)
389
+ use_tzlocal = True
412
390
else :
413
- # Adjust datetime64 timestamp, recompute datetimestruct
414
391
trans, deltas, typ = get_dst_info(tz)
415
-
416
392
if typ not in [" pytz" , " dateutil" ]:
417
393
# static/fixed; in this case we know that len(delta) == 1
418
- for i in range (n):
419
- if stamps[i] == NPY_NAT:
420
- result[i] = NPY_NAT
421
- continue
422
- dt64_to_dtstruct(stamps[i] + deltas[0 ], & dts)
423
- result[i] = get_period_ordinal(& dts, freq)
394
+ use_fixed = True
395
+ delta = deltas[0 ]
424
396
else :
425
397
pos = trans.searchsorted(stamps, side = " right" ) - 1
426
398
427
- for i in range (n):
428
- if stamps[i] == NPY_NAT:
429
- result[i] = NPY_NAT
430
- continue
431
- dt64_to_dtstruct(stamps[i] + deltas[pos[i]], & dts)
432
- result[i] = get_period_ordinal(& dts, freq)
399
+ for i in range (n):
400
+ # TODO: reinstate nogil for use_utc case?
401
+ if stamps[i] == NPY_NAT:
402
+ result[i] = NPY_NAT
403
+ continue
404
+
405
+ if use_utc:
406
+ local_val = stamps[i]
407
+ elif use_tzlocal:
408
+ local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
409
+ elif use_fixed:
410
+ local_val = stamps[i] + delta
411
+ else :
412
+ local_val = stamps[i] + deltas[pos[i]]
413
+
414
+ dt64_to_dtstruct(local_val, & dts)
415
+ result[i] = get_period_ordinal(& dts, freq)
433
416
434
417
return result.base # .base to get underlying ndarray
0 commit comments