Skip to content

REF: _reso->_creso #49107

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 1 commit into from
Oct 15, 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
4 changes: 2 additions & 2 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ cdef class DatetimeEngine(Int64Engine):
if scalar is NaT:
return NaT.value
elif isinstance(scalar, _Timestamp):
if scalar._reso == self.reso:
if scalar._creso == self.reso:
return scalar.value
else:
# Note: caller is responsible for catching potential ValueError
Expand Down Expand Up @@ -570,7 +570,7 @@ cdef class TimedeltaEngine(DatetimeEngine):
if scalar is NaT:
return NaT.value
elif isinstance(scalar, _Timedelta):
if scalar._reso == self.reso:
if scalar._creso == self.reso:
return scalar.value
else:
# Note: caller is responsible for catching potential ValueError
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def to_offset(freq: _BaseOffsetT) -> _BaseOffsetT: ...
def to_offset(freq: timedelta | str) -> BaseOffset: ...

class Tick(SingleConstructorOffset):
_reso: int
_creso: int
_prefix: str
_td64_unit: str
def __init__(self, n: int = ..., normalize: bool = ...) -> None: ...
Expand Down
16 changes: 8 additions & 8 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1146,55 +1146,55 @@ cdef class Day(Tick):
_prefix = "D"
_td64_unit = "D"
_period_dtype_code = PeriodDtypeCode.D
_reso = NPY_DATETIMEUNIT.NPY_FR_D
_creso = NPY_DATETIMEUNIT.NPY_FR_D


cdef class Hour(Tick):
_nanos_inc = 3600 * 1_000_000_000
_prefix = "H"
_td64_unit = "h"
_period_dtype_code = PeriodDtypeCode.H
_reso = NPY_DATETIMEUNIT.NPY_FR_h
_creso = NPY_DATETIMEUNIT.NPY_FR_h


cdef class Minute(Tick):
_nanos_inc = 60 * 1_000_000_000
_prefix = "T"
_td64_unit = "m"
_period_dtype_code = PeriodDtypeCode.T
_reso = NPY_DATETIMEUNIT.NPY_FR_m
_creso = NPY_DATETIMEUNIT.NPY_FR_m


cdef class Second(Tick):
_nanos_inc = 1_000_000_000
_prefix = "S"
_td64_unit = "s"
_period_dtype_code = PeriodDtypeCode.S
_reso = NPY_DATETIMEUNIT.NPY_FR_s
_creso = NPY_DATETIMEUNIT.NPY_FR_s


cdef class Milli(Tick):
_nanos_inc = 1_000_000
_prefix = "L"
_td64_unit = "ms"
_period_dtype_code = PeriodDtypeCode.L
_reso = NPY_DATETIMEUNIT.NPY_FR_ms
_creso = NPY_DATETIMEUNIT.NPY_FR_ms


cdef class Micro(Tick):
_nanos_inc = 1000
_prefix = "U"
_td64_unit = "us"
_period_dtype_code = PeriodDtypeCode.U
_reso = NPY_DATETIMEUNIT.NPY_FR_us
_creso = NPY_DATETIMEUNIT.NPY_FR_us


cdef class Nano(Tick):
_nanos_inc = 1
_prefix = "N"
_td64_unit = "ns"
_period_dtype_code = PeriodDtypeCode.N
_reso = NPY_DATETIMEUNIT.NPY_FR_ns
_creso = NPY_DATETIMEUNIT.NPY_FR_ns


def delta_to_tick(delta: timedelta) -> Tick:
Expand Down Expand Up @@ -3394,7 +3394,7 @@ cdef class FY5253Quarter(FY5253Mixin):
for qlen in qtr_lens:
if qlen * 7 <= tdelta.days:
num_qtrs += 1
tdelta -= (<_Timedelta>Timedelta(days=qlen * 7))._as_creso(norm._reso)
tdelta -= (<_Timedelta>Timedelta(days=qlen * 7))._as_creso(norm._creso)
else:
break
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ cdef class _Period(PeriodMixin):
return NaT

try:
inc = delta_to_nanoseconds(other, reso=self.freq._reso, round_ok=False)
inc = delta_to_nanoseconds(other, reso=self.freq._creso, round_ok=False)
except ValueError as err:
raise IncompatibleFrequency("Input cannot be converted to "
f"Period(freq={self.freqstr})") from err
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timedeltas.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cdef class _Timedelta(timedelta):
int64_t value # nanoseconds
bint _is_populated # are my components populated
int64_t _d, _h, _m, _s, _ms, _us, _ns
NPY_DATETIMEUNIT _reso
NPY_DATETIMEUNIT _creso

cpdef timedelta to_pytimedelta(_Timedelta self)
cdef bint _has_ns(self)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def delta_to_nanoseconds(
) -> int: ...

class Timedelta(timedelta):
_reso: int
_creso: int
min: ClassVar[Timedelta]
max: ClassVar[Timedelta]
resolution: ClassVar[Timedelta]
Expand Down
80 changes: 40 additions & 40 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ cpdef int64_t delta_to_nanoseconds(

if is_tick_object(delta):
n = delta.n
in_reso = delta._reso
in_reso = delta._creso

elif isinstance(delta, _Timedelta):
n = delta.value
in_reso = delta._reso
in_reso = delta._creso

elif is_timedelta64_object(delta):
in_reso = get_datetime64_unit(delta)
Expand Down Expand Up @@ -339,7 +339,7 @@ cdef convert_to_timedelta64(object ts, str unit):
return np.timedelta64(NPY_NAT, "ns")
elif isinstance(ts, _Timedelta):
# already in the proper format
if ts._reso != NPY_FR_ns:
if ts._creso != NPY_FR_ns:
ts = ts._as_unit("ns").asm8
else:
ts = np.timedelta64(ts.value, "ns")
Expand Down Expand Up @@ -740,7 +740,7 @@ cdef bint _validate_ops_compat(other):
def _op_unary_method(func, name):
def f(self):
new_value = func(self.value)
return _timedelta_from_value_and_reso(new_value, self._reso)
return _timedelta_from_value_and_reso(new_value, self._creso)
f.__name__ = name
return f

Expand Down Expand Up @@ -792,18 +792,18 @@ def _binary_op_method_timedeltalike(op, name):

# Matching numpy, we cast to the higher resolution. Unlike numpy,
# we raise instead of silently overflowing during this casting.
if self._reso < other._reso:
self = (<_Timedelta>self)._as_creso(other._reso, round_ok=True)
elif self._reso > other._reso:
other = (<_Timedelta>other)._as_creso(self._reso, round_ok=True)
if self._creso < other._creso:
self = (<_Timedelta>self)._as_creso(other._creso, round_ok=True)
elif self._creso > other._creso:
other = (<_Timedelta>other)._as_creso(self._creso, round_ok=True)

res = op(self.value, other.value)
if res == NPY_NAT:
# e.g. test_implementation_limits
# TODO: more generally could do an overflowcheck in op?
return NaT

return _timedelta_from_value_and_reso(res, reso=self._reso)
return _timedelta_from_value_and_reso(res, reso=self._creso)

f.__name__ = name
return f
Expand Down Expand Up @@ -970,7 +970,7 @@ cdef _timedelta_from_value_and_reso(int64_t value, NPY_DATETIMEUNIT reso):

td_base.value = value
td_base._is_populated = 0
td_base._reso = reso
td_base._creso = reso
return td_base


Expand All @@ -996,7 +996,7 @@ class MinMaxReso:
# i.e. this is on the class, default to nanos
return Timedelta(val)
else:
return Timedelta._from_value_and_reso(val, obj._reso)
return Timedelta._from_value_and_reso(val, obj._creso)

def __set__(self, obj, value):
raise AttributeError(f"{self._name} is not settable.")
Expand All @@ -1022,9 +1022,9 @@ cdef class _Timedelta(timedelta):
@property
def _unit(self) -> str:
"""
The abbreviation associated with self._reso.
The abbreviation associated with self._creso.
"""
return npy_unit_to_abbrev(self._reso)
return npy_unit_to_abbrev(self._creso)

@property
def days(self) -> int: # TODO(cython3): make cdef property
Expand Down Expand Up @@ -1127,7 +1127,7 @@ cdef class _Timedelta(timedelta):
else:
return NotImplemented

if self._reso == ots._reso:
if self._creso == ots._creso:
return cmp_scalar(self.value, ots.value, op)
return self._compare_mismatched_resos(ots, op)

Expand All @@ -1139,18 +1139,18 @@ cdef class _Timedelta(timedelta):
npy_datetimestruct dts_other

# dispatch to the datetimestruct utils instead of writing new ones!
pandas_datetime_to_datetimestruct(self.value, self._reso, &dts_self)
pandas_datetime_to_datetimestruct(other.value, other._reso, &dts_other)
pandas_datetime_to_datetimestruct(self.value, self._creso, &dts_self)
pandas_datetime_to_datetimestruct(other.value, other._creso, &dts_other)
return cmp_dtstructs(&dts_self, &dts_other, op)

cdef bint _has_ns(self):
if self._reso == NPY_FR_ns:
if self._creso == NPY_FR_ns:
return self.value % 1000 != 0
elif self._reso < NPY_FR_ns:
elif self._creso < NPY_FR_ns:
# i.e. seconds, millisecond, microsecond
return False
else:
raise NotImplementedError(self._reso)
raise NotImplementedError(self._creso)

cdef _ensure_components(_Timedelta self):
"""
Expand All @@ -1162,7 +1162,7 @@ cdef class _Timedelta(timedelta):
cdef:
pandas_timedeltastruct tds

pandas_timedelta_to_timedeltastruct(self.value, self._reso, &tds)
pandas_timedelta_to_timedeltastruct(self.value, self._creso, &tds)
self._d = tds.days
self._h = tds.hrs
self._m = tds.min
Expand Down Expand Up @@ -1194,7 +1194,7 @@ cdef class _Timedelta(timedelta):
-----
Any nanosecond resolution will be lost.
"""
if self._reso == NPY_FR_ns:
if self._creso == NPY_FR_ns:
return timedelta(microseconds=int(self.value) / 1000)

# TODO(@WillAyd): is this the right way to use components?
Expand All @@ -1208,7 +1208,7 @@ cdef class _Timedelta(timedelta):
Return a numpy.timedelta64 object with 'ns' precision.
"""
cdef:
str abbrev = npy_unit_to_abbrev(self._reso)
str abbrev = npy_unit_to_abbrev(self._creso)
# TODO: way to create a np.timedelta64 obj with the reso directly
# instead of having to get the abbrev?
return np.timedelta64(self.value, abbrev)
Expand Down Expand Up @@ -1548,11 +1548,11 @@ cdef class _Timedelta(timedelta):
cdef:
int64_t value

if reso == self._reso:
if reso == self._creso:
return self

try:
value = convert_reso(self.value, self._reso, reso, round_ok=round_ok)
value = convert_reso(self.value, self._creso, reso, round_ok=round_ok)
except OverflowError as err:
unit = npy_unit_to_abbrev(reso)
raise OutOfBoundsTimedelta(
Expand All @@ -1565,10 +1565,10 @@ cdef class _Timedelta(timedelta):
"""
If _resos do not match, cast to the higher resolution, raising on overflow.
"""
if self._reso > other._reso:
other = other._as_creso(self._reso)
elif self._reso < other._reso:
self = self._as_creso(other._reso)
if self._creso > other._creso:
other = other._as_creso(self._creso)
elif self._creso < other._creso:
self = self._as_creso(other._creso)
return self, other


Expand Down Expand Up @@ -1736,7 +1736,7 @@ class Timedelta(_Timedelta):
return cls._from_value_and_reso(new_value, reso=new_reso)

elif is_tick_object(value):
new_reso = get_supported_reso(value._reso)
new_reso = get_supported_reso(value._creso)
new_value = delta_to_nanoseconds(value, reso=new_reso)
return cls._from_value_and_reso(new_value, reso=new_reso)

Expand Down Expand Up @@ -1769,10 +1769,10 @@ class Timedelta(_Timedelta):
else:
value, reso = state
self.value = value
self._reso = reso
self._creso = reso

def __reduce__(self):
object_state = self.value, self._reso
object_state = self.value, self._creso
return (_timedelta_unpickle, object_state)

@cython.cdivision(True)
Expand All @@ -1784,11 +1784,11 @@ class Timedelta(_Timedelta):
from pandas._libs.tslibs.offsets import to_offset

to_offset(freq).nanos # raises on non-fixed freq
unit = delta_to_nanoseconds(to_offset(freq), self._reso)
unit = delta_to_nanoseconds(to_offset(freq), self._creso)

arr = np.array([self.value], dtype="i8")
result = round_nsint64(arr, mode, unit)[0]
return Timedelta._from_value_and_reso(result, self._reso)
return Timedelta._from_value_and_reso(result, self._creso)

def round(self, freq):
"""
Expand Down Expand Up @@ -1852,7 +1852,7 @@ class Timedelta(_Timedelta):

return _timedelta_from_value_and_reso(
<int64_t>(other * self.value),
reso=self._reso,
reso=self._creso,
)

elif is_array(other):
Expand All @@ -1875,7 +1875,7 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if other._reso != self._reso:
if other._creso != self._creso:
self, other = self._maybe_cast_to_matching_resos(other)
return self.value / float(other.value)

Expand All @@ -1884,7 +1884,7 @@ class Timedelta(_Timedelta):
if util.is_nan(other):
return NaT
return Timedelta._from_value_and_reso(
<int64_t>(self.value / other), self._reso
<int64_t>(self.value / other), self._creso
)

elif is_array(other):
Expand All @@ -1902,7 +1902,7 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if self._reso != other._reso:
if self._creso != other._creso:
self, other = self._maybe_cast_to_matching_resos(other)
return float(other.value) / self.value

Expand Down Expand Up @@ -1930,14 +1930,14 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if self._reso != other._reso:
if self._creso != other._creso:
self, other = self._maybe_cast_to_matching_resos(other)
return self.value // other.value

elif is_integer_object(other) or is_float_object(other):
if util.is_nan(other):
return NaT
return type(self)._from_value_and_reso(self.value // other, self._reso)
return type(self)._from_value_and_reso(self.value // other, self._creso)

elif is_array(other):
if other.ndim == 0:
Expand Down Expand Up @@ -1975,7 +1975,7 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if self._reso != other._reso:
if self._creso != other._creso:
self, other = self._maybe_cast_to_matching_resos(other)
return other.value // self.value

Expand Down
Loading