Skip to content

PERF: Optimize month_position_check in infer_freq#64463

Merged
mroeschke merged 2 commits intopandas-dev:mainfrom
jbrockmendel:perf-infer_freq
Mar 8, 2026
Merged

PERF: Optimize month_position_check in infer_freq#64463
mroeschke merged 2 commits intopandas-dev:mainfrom
jbrockmendel:perf-infer_freq

Conversation

@jbrockmendel
Copy link
Member

@jbrockmendel jbrockmendel commented Mar 7, 2026

Claude wrote this, I reviewed it manually

dti = pd.date_range("2000-01-31", periods=10000, freq="ME")._with_freq(None)

%timeit pd.infer_freq(dti)
5.15 ms ± 692 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)  # <- main
686 μs ± 91.4 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)  # <- PR

Summary

  • Replace Python-level zip() iteration with C-level indexed range() loop in month_position_check
  • Type the weekdays parameter as an int32_t[:] memoryview instead of untyped Python object
  • Add @cython.boundscheck(False) and @cython.wraparound(False) decorators

jbrockmendel and others added 2 commits March 7, 2026 15:40
Replace Python-level zip() iteration with C-level indexed loop,
type the weekdays parameter as an int32 memoryview, and add
boundscheck/wraparound decorators. ~10x speedup for infer_freq.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mroeschke mroeschke added this to the 3.1 milestone Mar 8, 2026
@mroeschke mroeschke added Performance Memory or execution speed performance Frequency DateOffsets labels Mar 8, 2026
@mroeschke mroeschke merged commit 0039f16 into pandas-dev:main Mar 8, 2026
49 checks passed
@mroeschke
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the perf-infer_freq branch March 8, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Frequency DateOffsets Performance Memory or execution speed performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants