Skip to content

Commit c5dad15

Browse files
authored
PERF: 30% faster is_period_object checks (#33961)
1 parent 3cebe30 commit c5dad15

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

pandas/_libs/lib.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ from pandas._libs.tslibs.nattype cimport (
7575
from pandas._libs.tslibs.conversion cimport convert_to_tsobject
7676
from pandas._libs.tslibs.timedeltas cimport convert_to_timedelta64
7777
from pandas._libs.tslibs.timezones cimport get_timezone, tz_compare
78+
from pandas._libs.tslibs.period cimport is_period_object
7879

7980
from pandas._libs.missing cimport (
8081
checknull,
@@ -185,7 +186,7 @@ def is_scalar(val: object) -> bool:
185186

186187
# Note: PyNumber_Check check includes Decimal, Fraction, numbers.Number
187188
return (PyNumber_Check(val)
188-
or util.is_period_object(val)
189+
or is_period_object(val)
189190
or is_interval(val)
190191
or util.is_offset_object(val))
191192

@@ -942,7 +943,7 @@ def is_period(val: object) -> bool:
942943
-------
943944
bool
944945
"""
945-
return util.is_period_object(val)
946+
return is_period_object(val)
946947

947948

948949
def is_list_like(obj: object, allow_sets: bool = True) -> bool:
@@ -1417,7 +1418,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
14171418
if is_bytes_array(values, skipna=skipna):
14181419
return "bytes"
14191420

1420-
elif util.is_period_object(val):
1421+
elif is_period_object(val):
14211422
if is_period_array(values):
14221423
return "period"
14231424

@@ -1849,7 +1850,7 @@ cpdef bint is_time_array(ndarray values, bint skipna=False):
18491850

18501851
cdef class PeriodValidator(TemporalValidator):
18511852
cdef inline bint is_value_typed(self, object value) except -1:
1852-
return util.is_period_object(value)
1853+
return is_period_object(value)
18531854

18541855
cdef inline bint is_valid_null(self, object value) except -1:
18551856
return checknull_with_nat(value)

pandas/_libs/tslibs/period.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cdef bint is_period_object(object obj)

pandas/_libs/tslibs/period.pyx

+9-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ cdef extern from "src/datetime/np_datetime.h":
3636
npy_datetimestruct *d) nogil
3737

3838
cimport pandas._libs.tslibs.util as util
39-
from pandas._libs.tslibs.util cimport is_period_object
4039

4140
from pandas._libs.tslibs.timestamps import Timestamp
4241
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
@@ -2467,6 +2466,15 @@ class Period(_Period):
24672466
return cls._from_ordinal(ordinal, freq)
24682467

24692468

2469+
cdef bint is_period_object(object obj):
2470+
"""
2471+
Cython-optimized equivalent of isinstance(obj, Period)
2472+
"""
2473+
# Note: this is significantly faster than the implementation in tslibs.util,
2474+
# only use the util version when necessary to prevent circular imports.
2475+
return isinstance(obj, _Period)
2476+
2477+
24702478
cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
24712479
int hour, int minute, int second, freq):
24722480
base, mult = get_freq_code(freq)

0 commit comments

Comments
 (0)