Skip to content

More tslibs TODOS: small optimizations, trim namespaces #18446

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

Closed
wants to merge 6 commits into from
Closed
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
105 changes: 57 additions & 48 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ from cpython cimport (PyObject_RichCompareBool, PyObject_RichCompare,

import numpy as np
cimport numpy as np
from numpy cimport int64_t, int32_t, ndarray
from numpy cimport int64_t, int32_t, int8_t, ndarray
np.import_array()

from datetime import time as datetime_time
Expand Down Expand Up @@ -296,18 +296,43 @@ cdef class _Timestamp(datetime):
val = tz_convert_single(self.value, 'UTC', self.tz)
return val

cpdef int _get_field(self, field):
cdef int _get_field(_Timestamp self, field):
# Note: because _get_field is `cdef`, it is not visible in the python
# namespace. Any methods that need to call `_get_field` must be
# defined in _Timestamp, not Timestamp.
cdef:
int64_t val
ndarray[int32_t] out
val = self._maybe_convert_value_to_local()
out = get_date_field(np.array([val], dtype=np.int64), field)
return int(out[0])

cpdef _get_start_end_field(self, field):
@property
def dayofyear(self):
return self._get_field('doy')

@property
def week(self):
return self._get_field('woy')

@property
def quarter(self):
return self._get_field('q')

@property
def days_in_month(self):
return self._get_field('dim')

cdef bint _get_start_end_field(_Timestamp self, field):
Copy link
Contributor

Choose a reason for hiding this comment

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

why does moving this to _Timestamp help

Copy link
Member Author

Choose a reason for hiding this comment

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

why does moving this to _Timestamp help

Among the todos was getting _get_start_end_field and _get_field out of the user-facing namespace. This accomplishes that.

It also adds type declarations, so should be marginally more performant.

Copy link
Contributor

Choose a reason for hiding this comment

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

why do we care about _get_start_end_field in the user namespace, its a private method?

type declarations are fine (pls run an asv to confirm)

# Note: because _get_start_end_field is `cdef`, it is not visible
# in the python namespace. Any methods that need to call
# `_get_start_end_field` must be defined in _Timestamp, not Timestamp.
cdef:
int64_t val
dict kwds
# Note: As of 2017-11-23, adding a type declaration for `out`
# results in a ValueError when any of these properties are accessed
# "Does not understand character buffer dtype format string ('?')"

freq = self.freq
if freq:
Expand All @@ -321,7 +346,31 @@ cdef class _Timestamp(datetime):
val = self._maybe_convert_value_to_local()
out = get_start_end_field(np.array([val], dtype=np.int64),
field, freqstr, month_kw)
return out[0]
return bool(out[0])

@property
def is_month_start(self):
return self._get_start_end_field('is_month_start')

@property
def is_month_end(self):
return self._get_start_end_field('is_month_end')

@property
def is_quarter_start(self):
return self._get_start_end_field('is_quarter_start')

@property
def is_quarter_end(self):
return self._get_start_end_field('is_quarter_end')

@property
def is_year_start(self):
return self._get_start_end_field('is_year_start')

@property
def is_year_end(self):
return self._get_start_end_field('is_year_end')

@property
def _repr_base(self):
Expand Down Expand Up @@ -701,54 +750,10 @@ class Timestamp(_Timestamp):
6: 'Sunday'}
return wdays[self.weekday()]

@property
def dayofyear(self):
return self._get_field('doy')

@property
def week(self):
return self._get_field('woy')

weekofyear = week

@property
def quarter(self):
return self._get_field('q')

@property
def days_in_month(self):
return self._get_field('dim')

daysinmonth = days_in_month

@property
def freqstr(self):
return getattr(self.freq, 'freqstr', self.freq)

@property
def is_month_start(self):
return self._get_start_end_field('is_month_start')

@property
def is_month_end(self):
return self._get_start_end_field('is_month_end')

@property
def is_quarter_start(self):
return self._get_start_end_field('is_quarter_start')

@property
def is_quarter_end(self):
return self._get_start_end_field('is_quarter_end')

@property
def is_year_start(self):
return self._get_start_end_field('is_year_start')

@property
def is_year_end(self):
return self._get_start_end_field('is_year_end')

@property
def is_leap_year(self):
return bool(is_leapyear(self.year))
Expand Down Expand Up @@ -982,6 +987,10 @@ class Timestamp(_Timestamp):
return self + other


# property aliases
Timestamp.daysinmonth = Timestamp.days_in_month
Timestamp.weekofyear = Timestamp.week

# Add the min and max fields at the class level
cdef int64_t _NS_UPPER_BOUND = INT64_MAX
# the smallest value we could actually represent is
Expand Down