Skip to content

Conversation

msobhyy10
Copy link

…existent="shift_forward"` when crossing DST boundaries

This fixes GH#62602 where `date_range` failed for nonexistent times during
DST transitions due to `Timestamp.normalize` not handling `ambiguous` and
`nonexistent` parameters.

Changes:
- Updated `Timestamp.normalize` (in `_libs/tslibs/timestamps.pyx`):
  * Added `ambiguous` and `nonexistent` keyword arguments (defaulting to "raise").
  * Passed these parameters to `tz_localize` to properly handle DST-related
    ambiguous and nonexistent timestamps.
- Updated type stubs in `timestamps.pyi` to reflect the new method signature.
- Extended `_maybe_normalize_endpoints` (in `core/arrays/datetimes.py`):
  * Added support for `ambiguous` and `nonexistent` parameters.
  * Updated internal normalization calls to propagate these parameters.
- Updated `DatetimeArray._generate_range` logic to ensure consistent propagation
  of `ambiguous` and `nonexistent` through `_maybe_normalize_endpoints` both
  before and after timezone handling.

With these changes, `date_range` now correctly handles cases where normalization
intersects DST transitions, preserving expected behavior and avoiding
`NonExistentTimeError` or `AmbiguousTimeError` exceptions.

Co-authored-by: Mohamed Sobhy <[email protected]>
…iguous times (GH#62602)

Add a test in `pandas/tests/arrays/test_datetimes.py` to ensure that
`pd.date_range` with `normalize=True` correctly handles DST transitions
when both `nonexistent` and `ambiguous` parameters are specified.
@msobhyy10 msobhyy10 changed the title BUG: pandas.date_range() raises NonExistentTimeError despite nonexistent="shift_forward" when crossing DST boundaries BUG: handle nonexistent="shift_forward" with normalize=True in date_range (GH#62602) Oct 9, 2025
@msobhyy10 msobhyy10 changed the title BUG: handle nonexistent="shift_forward" with normalize=True in date_range (GH#62602) BUG: pandas.date_range() raises NonExistentTimeError despite nonexistent="shift_forward" when crossing DST boundaries Oct 9, 2025
@msobhyy10 msobhyy10 changed the title BUG: pandas.date_range() raises NonExistentTimeError despite nonexistent="shift_forward" when crossing DST boundaries BUG: handle nonexistent="shift_forward" with normalize=True in date_range (GH#62602) Oct 9, 2025
@msobhyy10 msobhyy10 closed this Oct 9, 2025
@msobhyy10 msobhyy10 reopened this Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: pandas.date_range() raises NonExistentTimeError despite nonexistent="shift_forward" when crossing DST boundaries

1 participant