Skip to content

REF: de-duplicate vectorized normalize functions #35250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 42 additions & 51 deletions pandas/_libs/tslibs/vectorized.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -280,45 +280,40 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
ndarray[int64_t] trans
int64_t[:] deltas
str typ
Py_ssize_t[:] pos
intp_t[:] pos
int64_t delta, local_val
bint use_utc = False, use_tzlocal = False, use_fixed = False
bint use_pytz = False

if tz is None or is_utc(tz):
with nogil:
for i in range(n):
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
local_val = stamps[i]
result[i] = normalize_i8_stamp(local_val)

if is_utc(tz) or tz is None:
use_utc = True
elif is_tzlocal(tz):
for i in range(n):
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
result[i] = normalize_i8_stamp(local_val)
use_tzlocal = True
else:
# Adjust datetime64 timestamp, recompute datetimestruct
trans, deltas, typ = get_dst_info(tz)

if typ not in ["pytz", "dateutil"]:
# static/fixed; in this case we know that len(delta) == 1
use_fixed = True
delta = deltas[0]
for i in range(n):
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
local_val = stamps[i] + delta
result[i] = normalize_i8_stamp(local_val)
else:
pos = trans.searchsorted(stamps, side="right") - 1
for i in range(n):
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
local_val = stamps[i] + deltas[pos[i]]
result[i] = normalize_i8_stamp(local_val)

for i in range(n):
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue

if use_utc:
local_val = stamps[i]
elif use_tzlocal:
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
elif use_fixed:
local_val = stamps[i] + delta
else:
local_val = stamps[i] + deltas[pos[i]]

result[i] = normalize_i8_stamp(local_val)

return result.base # `.base` to access underlying ndarray

Expand Down Expand Up @@ -348,37 +343,33 @@ def is_date_array_normalized(const int64_t[:] stamps, tzinfo tz=None):
int64_t local_val, delta
str typ
int64_t day_nanos = 24 * 3600 * 1_000_000_000
bint use_utc = False, use_tzlocal = False, use_fixed = False

if tz is None or is_utc(tz):
for i in range(n):
local_val = stamps[i]
if local_val % day_nanos != 0:
return False

if is_utc(tz) or tz is None:
use_utc = True
elif is_tzlocal(tz):
for i in range(n):
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
if local_val % day_nanos != 0:
return False
use_tzlocal = True
else:
trans, deltas, typ = get_dst_info(tz)

if typ not in ["pytz", "dateutil"]:
# static/fixed; in this case we know that len(delta) == 1
use_fixed = True
delta = deltas[0]
for i in range(n):
# Adjust datetime64 timestamp, recompute datetimestruct
local_val = stamps[i] + delta
if local_val % day_nanos != 0:
return False
else:
pos = trans.searchsorted(stamps, side="right") - 1

for i in range(n):
if use_utc:
local_val = stamps[i]
elif use_tzlocal:
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
elif use_fixed:
local_val = stamps[i] + delta
else:
pos = trans.searchsorted(stamps) - 1
for i in range(n):
# Adjust datetime64 timestamp, recompute datetimestruct
local_val = stamps[i] + deltas[pos[i]]
if local_val % day_nanos != 0:
return False
local_val = stamps[i] + deltas[pos[i]]

if local_val % day_nanos != 0:
return False

return True

Expand Down