Skip to content

Fastpaths for Timestamp properties #18539

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
Nov 29, 2017
Merged
Changes from 2 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
24 changes: 22 additions & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,12 @@ cdef class _Timestamp(datetime):
out = get_date_field(np.array([val], dtype=np.int64), field)
return int(out[0])

cpdef _get_start_end_field(self, field):
cpdef bint _get_start_end_field(self, str field):
Copy link
Contributor

Choose a reason for hiding this comment

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

so why is this in _Timestamp again (as opposed to Timestamp); this is why its cpdef, why not just cdef?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure why its in _Timestamp instead of Timestamp (though my understanding is putting it in _Timestamp is slightly more performant and smaller memory footprint); it was that way before I got here.

It is cpdef and not cdef because if it were cdef then calling it from Timestamp would be an AttributeError. That's why #18446 moved a bunch of properties up to _Timestamp after making it cdef.

cdef:
int64_t val
dict kwds
ndarray out
int month_kw

freq = self.freq
if freq:
Expand Down Expand Up @@ -713,7 +715,7 @@ class Timestamp(_Timestamp):

@property
def quarter(self):
return self._get_field('q')
return ((self.month - 1) // 3) + 1

@property
def days_in_month(self):
Expand All @@ -727,26 +729,44 @@ class Timestamp(_Timestamp):

@property
def is_month_start(self):
if self.freq is None:
# fast-path for non-business frequencies
return self.day == 1
return self._get_start_end_field('is_month_start')

@property
def is_month_end(self):
if self.freq is None:
# fast-path for non-business frequencies
return self.day == self.days_in_month
return self._get_start_end_field('is_month_end')

@property
def is_quarter_start(self):
if self.freq is None:
# fast-path for non-business frequencies
return self.day == 1 and self.month % 3 == 1
return self._get_start_end_field('is_quarter_start')

@property
def is_quarter_end(self):
if self.freq is None:
# fast-path for non-business frequencies
return (self.month % 3) == 0 and self.day == self.days_in_month
return self._get_start_end_field('is_quarter_end')

@property
def is_year_start(self):
if self.freq is None:
# fast-path for non-business frequencies
return self.day == self.month == 1
return self._get_start_end_field('is_year_start')

@property
def is_year_end(self):
if self.freq is None:
# fast-path for non-business frequencies
return self.month == 12 and self.day == 31
return self._get_start_end_field('is_year_end')

@property
Expand Down