Skip to content

Commit 29f67d3

Browse files
authored
Implement get_supported_reso (pandas-dev#48956)
1 parent 855ed4e commit 29f67d3

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

pandas/_libs/tslibs/dtypes.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev)
88
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil
99
cpdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=*) except? -1
1010
cpdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1
11+
cpdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso)
1112

1213
cdef dict attrname_to_abbrevs
1314

pandas/_libs/tslibs/dtypes.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def periods_per_day(reso: int) -> int: ...
99
def periods_per_second(reso: int) -> int: ...
1010
def is_supported_unit(reso: int) -> bool: ...
1111
def npy_unit_to_abbrev(reso: int) -> str: ...
12+
def get_supported_reso(reso: int) -> int: ...
1213

1314
class PeriodDtypeBase:
1415
_dtype_code: int # PeriodDtypeCode

pandas/_libs/tslibs/dtypes.pyx

+13
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,19 @@ class NpyDatetimeUnit(Enum):
278278
NPY_FR_GENERIC = NPY_DATETIMEUNIT.NPY_FR_GENERIC
279279

280280

281+
cpdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso):
282+
# If we have an unsupported reso, return the nearest supported reso.
283+
if reso == NPY_DATETIMEUNIT.NPY_FR_GENERIC:
284+
# TODO: or raise ValueError? trying this gives unraisable errors, but
285+
# "except? -1" breaks at compile-time for unknown reasons
286+
return NPY_DATETIMEUNIT.NPY_FR_ns
287+
if reso < NPY_DATETIMEUNIT.NPY_FR_s:
288+
return NPY_DATETIMEUNIT.NPY_FR_s
289+
elif reso > NPY_DATETIMEUNIT.NPY_FR_ns:
290+
return NPY_DATETIMEUNIT.NPY_FR_ns
291+
return reso
292+
293+
281294
def is_supported_unit(NPY_DATETIMEUNIT reso):
282295
return (
283296
reso == NPY_DATETIMEUNIT.NPY_FR_ns

0 commit comments

Comments
 (0)