Skip to content

REF: de-duplicate get_resolution #35245

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

Merged
merged 10 commits into from
Aug 3, 2020
57 changes: 24 additions & 33 deletions pandas/_libs/tslibs/vectorized.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -211,49 +211,40 @@ def get_resolution(const int64_t[:] stamps, tzinfo tz=None):
int reso = RESO_DAY, curr_reso
ndarray[int64_t] trans
int64_t[:] deltas
Py_ssize_t[:] pos
int64_t local_val, delta
intp_t[:] pos
int64_t local_val, delta = NPY_NAT
bint use_utc = False, use_tzlocal = False, use_fixed = False

if is_utc(tz) or tz is None:
for i in range(n):
if stamps[i] == NPY_NAT:
continue
dt64_to_dtstruct(stamps[i], &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
use_utc = True
elif is_tzlocal(tz):
for i in range(n):
if stamps[i] == NPY_NAT:
continue
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
dt64_to_dtstruct(local_val, &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
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:
continue
dt64_to_dtstruct(stamps[i] + delta, &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
else:
pos = trans.searchsorted(stamps, side="right") - 1
for i in range(n):
if stamps[i] == NPY_NAT:
continue
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso

for i in range(n):
if stamps[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]]

dt64_to_dtstruct(local_val, &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso

return Resolution(reso)

Expand Down