Skip to content

ENH: get_resolution support non-nano #47322

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 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/vectorized.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def normalize_i8_timestamps(
def get_resolution(
stamps: npt.NDArray[np.int64],
tz: tzinfo | None = ...,
reso: int = ..., # NPY_DATETIMEUNIT
) -> Resolution: ...
def ints_to_pydatetime(
arr: npt.NDArray[np.int64],
Expand Down
17 changes: 10 additions & 7 deletions pandas/_libs/tslibs/vectorized.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ from .np_datetime cimport (
NPY_FR_ns,
dt64_to_dtstruct,
npy_datetimestruct,
pandas_datetime_to_datetimestruct,
)
from .offsets cimport BaseOffset
from .period cimport get_period_ordinal
Expand Down Expand Up @@ -226,17 +227,19 @@ cdef inline c_Resolution _reso_stamp(npy_datetimestruct *dts):

@cython.wraparound(False)
@cython.boundscheck(False)
def get_resolution(ndarray stamps, tzinfo tz=None) -> Resolution:
def get_resolution(
ndarray stamps, tzinfo tz=None, NPY_DATETIMEUNIT reso=NPY_FR_ns
) -> Resolution:
# stamps is int64_t, any ndim
cdef:
Localizer info = Localizer(tz, reso=NPY_FR_ns)
Localizer info = Localizer(tz, reso=reso)
int64_t utc_val, local_val
Py_ssize_t i, n = stamps.size
Py_ssize_t pos = -1 # unused, avoid not-initialized warning
cnp.flatiter it = cnp.PyArray_IterNew(stamps)

npy_datetimestruct dts
c_Resolution reso = c_Resolution.RESO_DAY, curr_reso
c_Resolution pd_reso = c_Resolution.RESO_DAY, curr_reso

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

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

cnp.PyArray_ITER_NEXT(it)

return Resolution(reso)
return Resolution(pd_reso)


# -------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def is_normalized(self) -> bool:

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

# ----------------------------------------------------------------
# Array-Like / EA-Interface Methods
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/tslibs/test_resolution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import numpy as np
import pytz

from pandas._libs.tslibs import (
Resolution,
get_resolution,
)
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit


def test_get_resolution_nano():
# don't return the fallback RESO_DAY
arr = np.array([1], dtype=np.int64)
res = get_resolution(arr)
assert res == Resolution.RESO_NS


def test_get_resolution_non_nano_data():
arr = np.array([1], dtype=np.int64)
res = get_resolution(arr, None, NpyDatetimeUnit.NPY_FR_us.value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test with UTC and None?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, updated

assert res == Resolution.RESO_US

res = get_resolution(arr, pytz.UTC, NpyDatetimeUnit.NPY_FR_us.value)
assert res == Resolution.RESO_US
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,11 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
"depends": tseries_depends,
"sources": ["pandas/_libs/tslibs/src/datetime/np_datetime.c"],
},
"_libs.tslibs.vectorized": {"pyxfile": "_libs/tslibs/vectorized"},
"_libs.tslibs.vectorized": {
"pyxfile": "_libs/tslibs/vectorized",
"depends": tseries_depends,
"sources": ["pandas/_libs/tslibs/src/datetime/np_datetime.c"],
},
"_libs.testing": {"pyxfile": "_libs/testing"},
"_libs.window.aggregations": {
"pyxfile": "_libs/window/aggregations",
Expand Down