Skip to content

PERF: vectorize PeriodIndex.from_fields#65921

Open
jbrockmendel wants to merge 2 commits into
pandas-dev:mainfrom
jbrockmendel:perf-vectorize
Open

PERF: vectorize PeriodIndex.from_fields#65921
jbrockmendel wants to merge 2 commits into
pandas-dev:mainfrom
jbrockmendel:perf-vectorize

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Split out of GH-65195 (which also vectorized to_datetime(DataFrame)); this PR is just the PeriodIndex.from_fields half, with the more involved to_datetime assembly path to follow in a separate PR.

Adds a period_ordinals_from_fields Cython function that converts arrays of date/time fields to period ordinals in a single C-level loop, replacing the Python-space list-append loop in _range_from_fields, and vectorizes the quarter-to-calendar-month conversion with numpy ops. Fields that would overflow the int32 npy_datetimestruct slots raise OverflowError, matching period_ordinal on main.

Benchmark Before After Speedup
PeriodIndex.from_fields (2k monthly) 0.47 ms 0.03 ms 16x
PeriodIndex.from_fields (100k monthly) 22.7 ms 0.82 ms ~25x

Test plan

  • pandas/tests/indexes/period/ and pandas/tests/arrays/period/ pass
  • pandas/tests/scalar/period/ and pandas/tests/tslibs/ pass
  • New tests: non-DEC quarter inferring Q-DEC, all-6-field hourly periods, empty arrays, and field values outside int32 range raising

🤖 Generated with Claude Code

@jbrockmendel jbrockmendel added Performance Memory or execution speed performance Period Period data type labels Jun 21, 2026
Add period_ordinals_from_fields Cython function that converts arrays of
year/month/day/hour/minute/second fields to period ordinals in a single
C-level loop, replacing the Python-space list-append loop in
_range_from_fields, and vectorize the quarter-to-calendar-month
conversion with numpy ops.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Performance Memory or execution speed performance Period Period data type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant