Skip to content

Commit 0039f16

Browse files
jbrockmendelclaude
andauthored
PERF: Optimize month_position_check in infer_freq (#64463)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 729a626 commit 0039f16

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

doc/source/whatsnew/v3.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Performance improvements
105105
- Performance improvement in :meth:`DataFrame.__getitem__` when selecting a
106106
single column by label on a :class:`DataFrame` with duplicate column names.
107107
(:issue:`64126`).
108+
- Performance improvement in :func:`infer_freq` (:issue:`64463`)
108109
- Performance improvement in :func:`merge` with ``how="left"`` (:issue:`64370`)
109110
- Performance improvement in :meth:`GroupBy.quantile` (:issue:`64330`)
110111

pandas/_libs/tslibs/fields.pyx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ def build_field_sarray(const int64_t[:] dtindex, NPY_DATETIMEUNIT reso):
9797
return out
9898

9999

100+
@cython.wraparound(False)
101+
@cython.boundscheck(False)
100102
def month_position_check(fields, weekdays) -> str | None:
101103
cdef:
102-
int32_t daysinmonth, y, m, d
104+
Py_ssize_t i, count
105+
int32_t daysinmonth, y, m, d, wd
103106
bint calendar_end = True
104107
bint business_end = True
105108
bint calendar_start = True
@@ -108,8 +111,16 @@ def month_position_check(fields, weekdays) -> str | None:
108111
int32_t[:] years = fields["Y"]
109112
int32_t[:] months = fields["M"]
110113
int32_t[:] days = fields["D"]
114+
int32_t[:] wdays = np.asarray(weekdays, dtype=np.int32)
115+
116+
count = len(years)
117+
118+
for i in range(count):
119+
y = years[i]
120+
m = months[i]
121+
d = days[i]
122+
wd = wdays[i]
111123

112-
for y, m, d, wd in zip(years, months, days, weekdays, strict=True):
113124
if calendar_start:
114125
calendar_start &= d == 1
115126
if business_start:

0 commit comments

Comments
 (0)