Skip to content

Commit a8d8ae7

Browse files
authored
ENH: get_resolution support non-nano (#47322)
* ENH: get_resolution support non-nano * tzaware case
1 parent f40203c commit a8d8ae7

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

pandas/_libs/tslibs/vectorized.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def normalize_i8_timestamps(
2929
def get_resolution(
3030
stamps: npt.NDArray[np.int64],
3131
tz: tzinfo | None = ...,
32+
reso: int = ..., # NPY_DATETIMEUNIT
3233
) -> Resolution: ...
3334
def ints_to_pydatetime(
3435
arr: npt.NDArray[np.int64],

pandas/_libs/tslibs/vectorized.pyx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,19 @@ cdef inline c_Resolution _reso_stamp(npy_datetimestruct *dts):
227227

228228
@cython.wraparound(False)
229229
@cython.boundscheck(False)
230-
def get_resolution(ndarray stamps, tzinfo tz=None) -> Resolution:
230+
def get_resolution(
231+
ndarray stamps, tzinfo tz=None, NPY_DATETIMEUNIT reso=NPY_FR_ns
232+
) -> Resolution:
231233
# stamps is int64_t, any ndim
232234
cdef:
233-
Localizer info = Localizer(tz, reso=NPY_FR_ns)
235+
Localizer info = Localizer(tz, reso=reso)
234236
int64_t utc_val, local_val
235237
Py_ssize_t i, n = stamps.size
236238
Py_ssize_t pos = -1 # unused, avoid not-initialized warning
237239
cnp.flatiter it = cnp.PyArray_IterNew(stamps)
238240

239241
npy_datetimestruct dts
240-
c_Resolution reso = c_Resolution.RESO_DAY, curr_reso
242+
c_Resolution pd_reso = c_Resolution.RESO_DAY, curr_reso
241243

242244
for i in range(n):
243245
# Analogous to: utc_val = stamps[i]
@@ -248,14 +250,14 @@ def get_resolution(ndarray stamps, tzinfo tz=None) -> Resolution:
248250
else:
249251
local_val = info.utc_val_to_local_val(utc_val, &pos)
250252

251-
dt64_to_dtstruct(local_val, &dts)
253+
pandas_datetime_to_datetimestruct(local_val, reso, &dts)
252254
curr_reso = _reso_stamp(&dts)
253-
if curr_reso < reso:
254-
reso = curr_reso
255+
if curr_reso < pd_reso:
256+
pd_reso = curr_reso
255257

256258
cnp.PyArray_ITER_NEXT(it)
257259

258-
return Resolution(reso)
260+
return Resolution(pd_reso)
259261

260262

261263
# -------------------------------------------------------------------------

pandas/core/arrays/datetimes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def is_normalized(self) -> bool:
619619

620620
@property # NB: override with cache_readonly in immutable subclasses
621621
def _resolution_obj(self) -> Resolution:
622-
return get_resolution(self.asi8, self.tz)
622+
return get_resolution(self.asi8, self.tz, reso=self._reso)
623623

624624
# ----------------------------------------------------------------
625625
# Array-Like / EA-Interface Methods
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import numpy as np
2+
import pytz
23

34
from pandas._libs.tslibs import (
45
Resolution,
56
get_resolution,
67
)
8+
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
79

810

911
def test_get_resolution_nano():
1012
# don't return the fallback RESO_DAY
1113
arr = np.array([1], dtype=np.int64)
1214
res = get_resolution(arr)
1315
assert res == Resolution.RESO_NS
16+
17+
18+
def test_get_resolution_non_nano_data():
19+
arr = np.array([1], dtype=np.int64)
20+
res = get_resolution(arr, None, NpyDatetimeUnit.NPY_FR_us.value)
21+
assert res == Resolution.RESO_US
22+
23+
res = get_resolution(arr, pytz.UTC, NpyDatetimeUnit.NPY_FR_us.value)
24+
assert res == Resolution.RESO_US

0 commit comments

Comments
 (0)