Skip to content

REF: reso->creso #49123

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 3 commits into from
Oct 16, 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
12 changes: 6 additions & 6 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -490,24 +490,24 @@ cdef class ObjectEngine(IndexEngine):
cdef class DatetimeEngine(Int64Engine):

cdef:
NPY_DATETIMEUNIT reso
NPY_DATETIMEUNIT _creso

def __init__(self, ndarray values):
super().__init__(values.view("i8"))
self.reso = get_unit_from_dtype(values.dtype)
self._creso = get_unit_from_dtype(values.dtype)

cdef int64_t _unbox_scalar(self, scalar) except? -1:
# NB: caller is responsible for ensuring tzawareness compat
# before we get here
if scalar is NaT:
return NaT.value
elif isinstance(scalar, _Timestamp):
if scalar._creso == self.reso:
if scalar._creso == self._creso:
return scalar.value
else:
# Note: caller is responsible for catching potential ValueError
# from _as_creso
return (<_Timestamp>scalar)._as_creso(self.reso, round_ok=False).value
return (<_Timestamp>scalar)._as_creso(self._creso, round_ok=False).value
raise TypeError(scalar)

def __contains__(self, val: object) -> bool:
Expand Down Expand Up @@ -570,12 +570,12 @@ cdef class TimedeltaEngine(DatetimeEngine):
if scalar is NaT:
return NaT.value
elif isinstance(scalar, _Timedelta):
if scalar._creso == self.reso:
if scalar._creso == self._creso:
return scalar.value
else:
# Note: caller is responsible for catching potential ValueError
# from _as_creso
return (<_Timedelta>scalar)._as_creso(self.reso, round_ok=False).value
return (<_Timedelta>scalar)._as_creso(self._creso, round_ok=False).value
raise TypeError(scalar)


Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2099,10 +2099,10 @@ default 'raise'
value = tz_localize_to_utc_single(self.value, tz,
ambiguous=ambiguous,
nonexistent=nonexistent,
reso=self._creso)
creso=self._creso)
elif tz is None:
# reset tz
value = tz_convert_from_utc_single(self.value, self.tz, reso=self._creso)
value = tz_convert_from_utc_single(self.value, self.tz, creso=self._creso)

else:
raise TypeError(
Expand Down Expand Up @@ -2245,7 +2245,7 @@ default 'raise'
fold = self.fold

if tzobj is not None:
value = tz_convert_from_utc_single(value, tzobj, reso=self._creso)
value = tz_convert_from_utc_single(value, tzobj, creso=self._creso)

# setup components
pandas_datetime_to_datetimestruct(value, self._creso, &dts)
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/tzconversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT


cpdef int64_t tz_convert_from_utc_single(
int64_t utc_val, tzinfo tz, NPY_DATETIMEUNIT reso=*
int64_t utc_val, tzinfo tz, NPY_DATETIMEUNIT creso=*
) except? -1
cdef int64_t tz_localize_to_utc_single(
int64_t val,
tzinfo tz,
object ambiguous=*,
object nonexistent=*,
NPY_DATETIMEUNIT reso=*,
NPY_DATETIMEUNIT creso=*,
) except? -1


Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/tzconversion.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ from pandas._typing import npt

# tz_convert_from_utc_single exposed for testing
def tz_convert_from_utc_single(
val: np.int64, tz: tzinfo, reso: int = ...
val: np.int64, tz: tzinfo, creso: int = ...
) -> np.int64: ...
def tz_localize_to_utc(
vals: npt.NDArray[np.int64],
tz: tzinfo | None,
ambiguous: str | bool | Iterable[bool] | None = ...,
nonexistent: str | timedelta | np.timedelta64 | None = ...,
reso: int = ..., # NPY_DATETIMEUNIT
creso: int = ..., # NPY_DATETIMEUNIT
) -> npt.NDArray[np.int64]: ...
82 changes: 41 additions & 41 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ cdef const int64_t[::1] _deltas_placeholder = np.array([], dtype=np.int64)
cdef class Localizer:
# cdef:
# tzinfo tz
# NPY_DATETIMEUNIT _reso
# NPY_DATETIMEUNIT _creso
# bint use_utc, use_fixed, use_tzlocal, use_dst, use_pytz
# ndarray trans
# Py_ssize_t ntrans
Expand All @@ -64,9 +64,9 @@ cdef class Localizer:

@cython.initializedcheck(False)
@cython.boundscheck(False)
def __cinit__(self, tzinfo tz, NPY_DATETIMEUNIT reso):
def __cinit__(self, tzinfo tz, NPY_DATETIMEUNIT creso):
self.tz = tz
self._creso = reso
self._creso = creso
self.use_utc = self.use_tzlocal = self.use_fixed = False
self.use_dst = self.use_pytz = False
self.ntrans = -1 # placeholder
Expand All @@ -82,22 +82,22 @@ cdef class Localizer:

else:
trans, deltas, typ = get_dst_info(tz)
if reso != NPY_DATETIMEUNIT.NPY_FR_ns:
if creso != NPY_DATETIMEUNIT.NPY_FR_ns:
# NB: using floordiv here is implicitly assuming we will
# never see trans or deltas that are not an integer number
# of seconds.
# TODO: avoid these np.array calls
if reso == NPY_DATETIMEUNIT.NPY_FR_us:
if creso == NPY_DATETIMEUNIT.NPY_FR_us:
trans = np.array(trans) // 1_000
deltas = np.array(deltas) // 1_000
elif reso == NPY_DATETIMEUNIT.NPY_FR_ms:
elif creso == NPY_DATETIMEUNIT.NPY_FR_ms:
trans = np.array(trans) // 1_000_000
deltas = np.array(deltas) // 1_000_000
elif reso == NPY_DATETIMEUNIT.NPY_FR_s:
elif creso == NPY_DATETIMEUNIT.NPY_FR_s:
trans = np.array(trans) // 1_000_000_000
deltas = np.array(deltas) // 1_000_000_000
else:
raise NotImplementedError(reso)
raise NotImplementedError(creso)

self.trans = trans
self.ntrans = self.trans.shape[0]
Expand All @@ -121,7 +121,7 @@ cdef class Localizer:
return utc_val
elif self.use_tzlocal:
return utc_val + _tz_localize_using_tzinfo_api(
utc_val, self.tz, to_utc=False, reso=self._creso, fold=fold
utc_val, self.tz, to_utc=False, creso=self._creso, fold=fold
)
elif self.use_fixed:
return utc_val + self.delta
Expand All @@ -140,7 +140,7 @@ cdef int64_t tz_localize_to_utc_single(
tzinfo tz,
object ambiguous=None,
object nonexistent=None,
NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns,
NPY_DATETIMEUNIT creso=NPY_DATETIMEUNIT.NPY_FR_ns,
) except? -1:
"""See tz_localize_to_utc.__doc__"""
cdef:
Expand All @@ -155,18 +155,18 @@ cdef int64_t tz_localize_to_utc_single(
return val

elif is_tzlocal(tz) or is_zoneinfo(tz):
return val - _tz_localize_using_tzinfo_api(val, tz, to_utc=True, reso=reso)
return val - _tz_localize_using_tzinfo_api(val, tz, to_utc=True, creso=creso)

elif is_fixed_offset(tz):
_, deltas, _ = get_dst_info(tz)
delta = deltas[0]
# TODO: de-duplicate with Localizer.__init__
if reso != NPY_DATETIMEUNIT.NPY_FR_ns:
if reso == NPY_DATETIMEUNIT.NPY_FR_us:
if creso != NPY_DATETIMEUNIT.NPY_FR_ns:
if creso == NPY_DATETIMEUNIT.NPY_FR_us:
delta = delta // 1000
elif reso == NPY_DATETIMEUNIT.NPY_FR_ms:
elif creso == NPY_DATETIMEUNIT.NPY_FR_ms:
delta = delta // 1_000_000
elif reso == NPY_DATETIMEUNIT.NPY_FR_s:
elif creso == NPY_DATETIMEUNIT.NPY_FR_s:
delta = delta // 1_000_000_000

return val - delta
Expand All @@ -177,7 +177,7 @@ cdef int64_t tz_localize_to_utc_single(
tz,
ambiguous=ambiguous,
nonexistent=nonexistent,
reso=reso,
creso=creso,
)[0]


Expand All @@ -188,7 +188,7 @@ def tz_localize_to_utc(
tzinfo tz,
object ambiguous=None,
object nonexistent=None,
NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns,
NPY_DATETIMEUNIT creso=NPY_DATETIMEUNIT.NPY_FR_ns,
):
"""
Localize tzinfo-naive i8 to given time zone (using pytz). If
Expand Down Expand Up @@ -216,7 +216,7 @@ def tz_localize_to_utc(
nonexistent : {None, "NaT", "shift_forward", "shift_backward", "raise", \
timedelta-like}
How to handle non-existent times when converting wall times to UTC
reso : NPY_DATETIMEUNIT, default NPY_FR_ns
creso : NPY_DATETIMEUNIT, default NPY_FR_ns

Returns
-------
Expand All @@ -236,8 +236,8 @@ timedelta-like}
bint shift_forward = False, shift_backward = False
bint fill_nonexist = False
str stamp
Localizer info = Localizer(tz, reso=reso)
int64_t pph = periods_per_day(reso) // 24
Localizer info = Localizer(tz, creso=creso)
int64_t pph = periods_per_day(creso) // 24

# Vectorized version of DstTzInfo.localize
if info.use_utc:
Expand All @@ -252,7 +252,7 @@ timedelta-like}
result[i] = NPY_NAT
else:
result[i] = v - _tz_localize_using_tzinfo_api(
v, tz, to_utc=True, reso=reso
v, tz, to_utc=True, creso=creso
)
return result.base # to return underlying ndarray

Expand Down Expand Up @@ -294,7 +294,7 @@ timedelta-like}
shift_backward = True
elif PyDelta_Check(nonexistent):
from .timedeltas import delta_to_nanoseconds
shift_delta = delta_to_nanoseconds(nonexistent, reso=reso)
shift_delta = delta_to_nanoseconds(nonexistent, reso=creso)
elif nonexistent not in ('raise', None):
msg = ("nonexistent must be one of {'NaT', 'raise', 'shift_forward', "
"shift_backwards} or a timedelta object")
Expand All @@ -303,13 +303,13 @@ timedelta-like}
# Determine whether each date lies left of the DST transition (store in
# result_a) or right of the DST transition (store in result_b)
result_a, result_b =_get_utc_bounds(
vals, info.tdata, info.ntrans, info.deltas, reso=reso
vals, info.tdata, info.ntrans, info.deltas, creso=creso
)

# silence false-positive compiler warning
dst_hours = np.empty(0, dtype=np.int64)
if infer_dst:
dst_hours = _get_dst_hours(vals, result_a, result_b, reso=reso)
dst_hours = _get_dst_hours(vals, result_a, result_b, creso=creso)

# Pre-compute delta_idx_offset that will be used if we go down non-existent
# paths.
Expand Down Expand Up @@ -348,7 +348,7 @@ timedelta-like}
# TODO: test with non-nano; parametrize test_dt_round_tz_ambiguous
result[i] = NPY_NAT
else:
stamp = _render_tstamp(val, reso=reso)
stamp = _render_tstamp(val, creso=creso)
raise pytz.AmbiguousTimeError(
f"Cannot infer dst time from {stamp}, try using the "
"'ambiguous' argument"
Expand Down Expand Up @@ -386,7 +386,7 @@ timedelta-like}
elif fill_nonexist:
result[i] = NPY_NAT
else:
stamp = _render_tstamp(val, reso=reso)
stamp = _render_tstamp(val, creso=creso)
raise pytz.NonExistentTimeError(stamp)

return result.base # .base to get underlying ndarray
Expand Down Expand Up @@ -422,10 +422,10 @@ cdef inline Py_ssize_t bisect_right_i8(int64_t *data,
return left


cdef inline str _render_tstamp(int64_t val, NPY_DATETIMEUNIT reso):
cdef inline str _render_tstamp(int64_t val, NPY_DATETIMEUNIT creso):
""" Helper function to render exception messages"""
from pandas._libs.tslibs.timestamps import Timestamp
ts = Timestamp._from_value_and_reso(val, reso, None)
ts = Timestamp._from_value_and_reso(val, creso, None)
return str(ts)


Expand All @@ -434,7 +434,7 @@ cdef _get_utc_bounds(
int64_t* tdata,
Py_ssize_t ntrans,
const int64_t[::1] deltas,
NPY_DATETIMEUNIT reso,
NPY_DATETIMEUNIT creso,
):
# Determine whether each date lies left of the DST transition (store in
# result_a) or right of the DST transition (store in result_b)
Expand All @@ -444,7 +444,7 @@ cdef _get_utc_bounds(
Py_ssize_t i, n = vals.size
int64_t val, v_left, v_right
Py_ssize_t isl, isr, pos_left, pos_right
int64_t ppd = periods_per_day(reso)
int64_t ppd = periods_per_day(creso)

result_a = cnp.PyArray_EMPTY(vals.ndim, vals.shape, cnp.NPY_INT64, 0)
result_b = cnp.PyArray_EMPTY(vals.ndim, vals.shape, cnp.NPY_INT64, 0)
Expand Down Expand Up @@ -486,11 +486,11 @@ cdef _get_utc_bounds(

@cython.boundscheck(False)
cdef ndarray[int64_t] _get_dst_hours(
# vals, reso only needed here to potential render an exception message
# vals, creso only needed here to potential render an exception message
const int64_t[:] vals,
ndarray[int64_t] result_a,
ndarray[int64_t] result_b,
NPY_DATETIMEUNIT reso,
NPY_DATETIMEUNIT creso,
):
cdef:
Py_ssize_t i, n = vals.shape[0]
Expand Down Expand Up @@ -519,7 +519,7 @@ cdef ndarray[int64_t] _get_dst_hours(

if trans_idx.size == 1:
# see test_tz_localize_to_utc_ambiguous_infer
stamp = _render_tstamp(vals[trans_idx[0]], reso=reso)
stamp = _render_tstamp(vals[trans_idx[0]], creso=creso)
raise pytz.AmbiguousTimeError(
f"Cannot infer dst time from {stamp} as there "
"are no repeated times"
Expand All @@ -541,7 +541,7 @@ cdef ndarray[int64_t] _get_dst_hours(
delta = np.diff(result_a[grp])
if grp.size == 1 or np.all(delta > 0):
# see test_tz_localize_to_utc_ambiguous_infer
stamp = _render_tstamp(vals[grp[0]], reso=reso)
stamp = _render_tstamp(vals[grp[0]], creso=creso)
raise pytz.AmbiguousTimeError(stamp)

# Find the index for the switch and pull from a for dst and b
Expand All @@ -567,7 +567,7 @@ cdef ndarray[int64_t] _get_dst_hours(
# Timezone Conversion

cpdef int64_t tz_convert_from_utc_single(
int64_t utc_val, tzinfo tz, NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns
int64_t utc_val, tzinfo tz, NPY_DATETIMEUNIT creso=NPY_DATETIMEUNIT.NPY_FR_ns
) except? -1:
"""
Convert the val (in i8) from UTC to tz
Expand All @@ -578,14 +578,14 @@ cpdef int64_t tz_convert_from_utc_single(
----------
utc_val : int64
tz : tzinfo
reso : NPY_DATETIMEUNIT, default NPY_FR_ns
creso : NPY_DATETIMEUNIT, default NPY_FR_ns

Returns
-------
converted: int64
"""
cdef:
Localizer info = Localizer(tz, reso=reso)
Localizer info = Localizer(tz, creso=creso)
Py_ssize_t pos

# Note: caller is responsible for ensuring utc_val != NPY_NAT
Expand All @@ -598,7 +598,7 @@ cdef int64_t _tz_localize_using_tzinfo_api(
int64_t val,
tzinfo tz,
bint to_utc=True,
NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns,
NPY_DATETIMEUNIT creso=NPY_DATETIMEUNIT.NPY_FR_ns,
bint* fold=NULL,
) except? -1:
"""
Expand All @@ -613,7 +613,7 @@ cdef int64_t _tz_localize_using_tzinfo_api(
tz : tzinfo
to_utc : bint
True if converting _to_ UTC, False if going the other direction.
reso : NPY_DATETIMEUNIT
creso : NPY_DATETIMEUNIT
fold : bint*, default NULL
pointer to fold: whether datetime ends up in a fold or not
after adjustment.
Expand All @@ -633,9 +633,9 @@ cdef int64_t _tz_localize_using_tzinfo_api(
datetime dt
int64_t delta
timedelta td
int64_t pps = periods_per_second(reso)
int64_t pps = periods_per_second(creso)

pandas_datetime_to_datetimestruct(val, reso, &dts)
pandas_datetime_to_datetimestruct(val, creso, &dts)

# datetime_new is cython-optimized constructor
if not to_utc:
Expand Down
Loading