-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: move more of Tick into liboffsets._Tick #34148
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
Changes from 4 commits
4b93740
f26f894
eb10383
634e147
88db438
3d4e54e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ from pandas._libs.tslibs.np_datetime cimport ( | |
from pandas._libs.tslibs.timezones cimport utc_pytz as UTC | ||
from pandas._libs.tslibs.tzconversion cimport tz_convert_single | ||
|
||
from pandas._libs.tslibs.timedeltas import Timedelta | ||
from pandas._libs.tslibs.timestamps import Timestamp | ||
|
||
# --------------------------------------------------------------------- | ||
|
@@ -643,7 +644,10 @@ class _BaseOffset: | |
|
||
# ------------------------------------------------------------------ | ||
|
||
def _validate_n(self, n): | ||
# Staticmethod so we can call from _Tick.__init__, will be unnecessary | ||
# once BaseOffset is a cdef class and is inherited by _Tick | ||
@staticmethod | ||
def _validate_n(n): | ||
""" | ||
Require that `n` be an integer. | ||
|
||
|
@@ -760,13 +764,78 @@ cdef class _Tick(ABCTick): | |
# ensure that reversed-ops with numpy scalars return NotImplemented | ||
__array_priority__ = 1000 | ||
_adjust_dst = False | ||
_inc = Timedelta(microseconds=1000) | ||
_prefix = "undefined" | ||
_attributes = frozenset(["n", "normalize"]) | ||
|
||
cdef readonly: | ||
int64_t n | ||
bint normalize | ||
dict _cache | ||
|
||
def __init__(self, n=1, normalize=False): | ||
n = _BaseOffset._validate_n(n) | ||
self.n = n | ||
self.normalize = False | ||
self._cache = {} | ||
if normalize: | ||
# GH#21427 | ||
raise ValueError( | ||
"Tick offset with `normalize=True` are not allowed." | ||
) | ||
|
||
@property | ||
def delta(self) -> Timedelta: | ||
return self.n * self._inc | ||
|
||
@property | ||
def nanos(self) -> int64_t: | ||
return self.delta.value | ||
|
||
def is_on_offset(self, dt) -> bool: | ||
return True | ||
|
||
def is_anchored(self) -> bool: | ||
return False | ||
|
||
# -------------------------------------------------------------------- | ||
# Comparison and Arithmetic Methods | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, str): | ||
try: | ||
# GH#23524 if to_offset fails, we are dealing with an | ||
# incomparable type so == is False and != is True | ||
other = to_offset(other) | ||
except ValueError: | ||
# e.g. "infer" | ||
return False | ||
return self.delta == other | ||
|
||
def __ne__(self, other): | ||
if isinstance(other, str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this just not self.eq...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure, thats how it is in the status quo. ill give it a shot and see if it breaks anything There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesnt break anything, just pushed with this update |
||
try: | ||
# GH#23524 if to_offset fails, we are dealing with an | ||
# incomparable type so == is False and != is True | ||
other = to_offset(other) | ||
except ValueError: | ||
# e.g. "infer" | ||
return True | ||
|
||
return self.delta != other | ||
|
||
def __le__(self, other): | ||
return self.delta.__le__(other) | ||
|
||
def __lt__(self, other): | ||
return self.delta.__lt__(other) | ||
|
||
def __ge__(self, other): | ||
return self.delta.__ge__(other) | ||
|
||
def __gt__(self, other): | ||
return self.delta.__gt__(other) | ||
|
||
def __truediv__(self, other): | ||
if not isinstance(self, _Tick): | ||
# cython semantics mean the args are sometimes swapped | ||
|
@@ -775,11 +844,15 @@ cdef class _Tick(ABCTick): | |
result = self.delta.__truediv__(other) | ||
return _wrap_timedelta_result(result) | ||
|
||
# -------------------------------------------------------------------- | ||
# Pickle Methods | ||
|
||
def __reduce__(self): | ||
return (type(self), (self.n,)) | ||
|
||
def __setstate__(self, state): | ||
object.__setattr__(self, "n", state["n"]) | ||
self.n = state["n"] | ||
self.normalize = False | ||
|
||
|
||
class BusinessMixin: | ||
|
Uh oh!
There was an error while loading. Please reload this page.