Skip to content

BUG: bdate_range(end, periods, freq=nB) anchors stride at buffer start#65604

Merged
mroeschke merged 3 commits into
pandas-dev:mainfrom
jbrockmendel:perf-bdate_range-2
May 29, 2026
Merged

BUG: bdate_range(end, periods, freq=nB) anchors stride at buffer start#65604
mroeschke merged 3 commits into
pandas-dev:mainfrom
jbrockmendel:perf-bdate_range-2

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

Summary

Follow-up to GH-64648, addressing a regression flagged by @jorisvandenbossche in post-merge review.

When bdate_range / date_range is called with end + periods and freq="nB" (or "nC") where n >= 2, the new fast path in generate_daily_offset_range anchored the [::n] stride at the start of the internal buffer instead of at end. The result was offset by buffer_days % n:

>>> pd.bdate_range(end="2026-03-22", periods=3, freq="2B")
# main (before fix):  ['2026-03-13', '2026-03-17', '2026-03-19']
# expected / 3.0.x:   ['2026-03-16', '2026-03-18', '2026-03-20']

n=1 was unaffected. Only n>=2 with end+periods is broken.

The fix reverses the array before striding so the anchor is the last on-offset date <= end, then reverses back. Symmetric with the existing trim_from_end branch a few lines later.

This regression only exists on main — 3.0.x doesn't have GH-64648 and uses the old _generate_range path, which handles nB correctly. No 3.1.0 has been released yet, so no whatsnew entry.

Test plan

  • New parametrized regression test covering end on Fri/Sat/Sun with freq="2B" and "3B"
  • Full pandas/tests/indexes/datetimes/test_date_range.py (421 tests) passes
  • Full pandas/tests/tseries/ (6941 tests) passes
  • mypy and pre-commit clean

🤖 Generated with Claude Code

jbrockmendel and others added 2 commits May 11, 2026 13:08
…buffer

Follow-up to pandas-devGH-64648: when end + periods are given and freq is n*B
(or n*C) with n >= 2, the [::abs_n] stride was anchored at the start
of the internal buffer instead of at end, producing dates offset by
(buffer_days % n). Reverse before striding so the anchor is the last
on-offset date <= end.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jorisvandenbossche jorisvandenbossche added this to the 3.1 milestone May 12, 2026
@jorisvandenbossche
Copy link
Copy Markdown
Member

Thanks, looks good to me

@jbrockmendel jbrockmendel marked this pull request as ready for review May 27, 2026 21:09
@jbrockmendel jbrockmendel marked this pull request as draft May 27, 2026 21:09
@jbrockmendel jbrockmendel added the Frequency DateOffsets label May 28, 2026
The stride-anchoring comment cited GH#64834, which is a separate
(already-fixed) n=1 weekend regression. Point it at GH#64648, the PR
that introduced this fast path, matching the commit and test references.
Also drop the weekend-specific framing since the bug occurs even when
end is itself a business day.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jbrockmendel jbrockmendel marked this pull request as ready for review May 29, 2026 01:27
@mroeschke mroeschke merged commit f5dedc4 into pandas-dev:main May 29, 2026
46 checks passed
@mroeschke
Copy link
Copy Markdown
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the perf-bdate_range-2 branch May 29, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Frequency DateOffsets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants