Skip to content

Commit 5413b6b

Browse files
authored
REF: de-duplicate get_resolution (#35245)
1 parent d367c8a commit 5413b6b

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

pandas/_libs/tslibs/vectorized.pyx

+24-33
Original file line numberDiff line numberDiff line change
@@ -211,49 +211,40 @@ def get_resolution(const int64_t[:] stamps, tzinfo tz=None):
211211
int reso = RESO_DAY, curr_reso
212212
ndarray[int64_t] trans
213213
int64_t[:] deltas
214-
Py_ssize_t[:] pos
215-
int64_t local_val, delta
214+
intp_t[:] pos
215+
int64_t local_val, delta = NPY_NAT
216+
bint use_utc = False, use_tzlocal = False, use_fixed = False
216217

217218
if is_utc(tz) or tz is None:
218-
for i in range(n):
219-
if stamps[i] == NPY_NAT:
220-
continue
221-
dt64_to_dtstruct(stamps[i], &dts)
222-
curr_reso = _reso_stamp(&dts)
223-
if curr_reso < reso:
224-
reso = curr_reso
219+
use_utc = True
225220
elif is_tzlocal(tz):
226-
for i in range(n):
227-
if stamps[i] == NPY_NAT:
228-
continue
229-
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
230-
dt64_to_dtstruct(local_val, &dts)
231-
curr_reso = _reso_stamp(&dts)
232-
if curr_reso < reso:
233-
reso = curr_reso
221+
use_tzlocal = True
234222
else:
235-
# Adjust datetime64 timestamp, recompute datetimestruct
236223
trans, deltas, typ = get_dst_info(tz)
237-
238224
if typ not in ["pytz", "dateutil"]:
239225
# static/fixed; in this case we know that len(delta) == 1
226+
use_fixed = True
240227
delta = deltas[0]
241-
for i in range(n):
242-
if stamps[i] == NPY_NAT:
243-
continue
244-
dt64_to_dtstruct(stamps[i] + delta, &dts)
245-
curr_reso = _reso_stamp(&dts)
246-
if curr_reso < reso:
247-
reso = curr_reso
248228
else:
249229
pos = trans.searchsorted(stamps, side="right") - 1
250-
for i in range(n):
251-
if stamps[i] == NPY_NAT:
252-
continue
253-
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
254-
curr_reso = _reso_stamp(&dts)
255-
if curr_reso < reso:
256-
reso = curr_reso
230+
231+
for i in range(n):
232+
if stamps[i] == NPY_NAT:
233+
continue
234+
235+
if use_utc:
236+
local_val = stamps[i]
237+
elif use_tzlocal:
238+
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
239+
elif use_fixed:
240+
local_val = stamps[i] + delta
241+
else:
242+
local_val = stamps[i] + deltas[pos[i]]
243+
244+
dt64_to_dtstruct(local_val, &dts)
245+
curr_reso = _reso_stamp(&dts)
246+
if curr_reso < reso:
247+
reso = curr_reso
257248

258249
return Resolution(reso)
259250

0 commit comments

Comments
 (0)