Skip to content

WIP: speed up Period creation #23500

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 2 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
18 changes: 9 additions & 9 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cdef extern from "src/datetime/np_datetime.h":
npy_datetimestruct *d) nogil

cimport util
from util cimport is_period_object, is_string_object
from util cimport is_period_object, is_string_object, is_offset_object

from timestamps import Timestamp, maybe_integer_op_deprecated
from timezones cimport is_utc, is_tzlocal, get_dst_info
Expand Down Expand Up @@ -1442,7 +1442,7 @@ def extract_ordinals(object[:] values, freq):
int64_t[:] ordinals = np.empty(n, dtype=np.int64)
object p

freqstr = Period._maybe_convert_freq(freq).freqstr
freq = Period._maybe_convert_freq(freq)

for i in range(n):
p = values[i]
Expand All @@ -1453,8 +1453,8 @@ def extract_ordinals(object[:] values, freq):
try:
ordinals[i] = p.ordinal

if p.freqstr != freqstr:
msg = DIFFERENT_FREQ_INDEX.format(freqstr, p.freqstr)
if p.freqstr != freq.freqstr:
msg = DIFFERENT_FREQ_INDEX.format(freq.freqstr, p.freqstr)
Copy link
Member

Choose a reason for hiding this comment

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

everything here is orthogonal to the rest of the PR, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is insignificant from a performance perspective - just related cleanup.

raise IncompatibleFrequency(msg)

except AttributeError:
Expand Down Expand Up @@ -1567,12 +1567,12 @@ cdef class _Period(object):

@classmethod
def _maybe_convert_freq(cls, object freq):
if not is_offset_object(freq):
Copy link
Member

Choose a reason for hiding this comment

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

This looks fine, and the timings you posted look nice. Can you try adding the not is_offset_object check at the top of tslibs.offsets.to_offset? Should get a similar performance improvement in a handful of other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will give that a shot. Will be out for a bit so may be several hours before I can post the results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not seeing any significant changes from implementing this - did you have a particular benchmark in mind?

Copy link
Member

Choose a reason for hiding this comment

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

I didn’t have any one in mind, but had a hunch. Thanks for taking a look

if isinstance(freq, (int, tuple)):
code, stride = get_freq_code(freq)
freq = get_freq_str(code, stride)

if isinstance(freq, (int, tuple)):
code, stride = get_freq_code(freq)
freq = get_freq_str(code, stride)

freq = to_offset(freq)
freq = to_offset(freq)

if freq.n <= 0:
raise ValueError('Frequency must be positive, because it'
Expand Down