Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ Timedelta

Timezones
^^^^^^^^^
- Bug in :func:`pandas.date_range` where using ``normalize=True`` could raise ``NonExistentTimeError`` or ``AmbiguousTimeError`` when crossing DST transitions, even if ``nonexistent`` or ``ambiguous`` parameters were specified. (:issue:`62602`)
- Bug in :meth:`DatetimeIndex.union`, :meth:`DatetimeIndex.intersection`, and :meth:`DatetimeIndex.symmetric_difference` changing timezone to UTC when merging two DatetimeIndex objects with the same timezone but different units (:issue:`60080`)
- Bug in :meth:`Series.dt.tz_localize` with a timezone-aware :class:`ArrowDtype` incorrectly converting to UTC when ``tz=None`` (:issue:`61780`)
- Fixed bug in :func:`date_range` where tz-aware endpoints with calendar offsets (e.g. ``"MS"``) failed on DST fall-back. These now respect ``ambiguous``/ ``nonexistent``. (:issue:`52908`)
Expand Down
6 changes: 5 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ class Timestamp(datetime):
ambiguous: bool | Literal["raise", "NaT"] = ...,
nonexistent: TimestampNonexistent = ...,
) -> Self: ...
def normalize(self) -> Self: ...
def normalize(
self,
ambiguous: bool | Literal["raise", "NaT"] = ...,
nonexistent: TimestampNonexistent = ...,
) -> Self: ...
# TODO: round/floor/ceil could return NaT?
def round(
self,
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ cdef class _Timestamp(ABCTimestamp):
# -----------------------------------------------------------------
# Transformation Methods

def normalize(self) -> "Timestamp":
def normalize(self, ambiguous="raise", nonexistent="raise") -> "Timestamp":
"""
Normalize Timestamp to midnight, preserving tz information.

Expand Down Expand Up @@ -1346,7 +1346,7 @@ cdef class _Timestamp(ABCTimestamp):
"Cannot normalize Timestamp without integer overflow"
) from err
ts = type(self)._from_value_and_reso(normalized, reso=self._creso, tz=None)
return ts.tz_localize(self.tzinfo)
return ts.tz_localize(self.tzinfo, ambiguous=ambiguous, nonexistent=nonexistent)

# -----------------------------------------------------------------
# Pickle Methods
Expand Down
18 changes: 14 additions & 4 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ def _generate_range(
end = end.as_unit(unit, round_ok=False)

left_inclusive, right_inclusive = validate_inclusive(inclusive)
start, end = _maybe_normalize_endpoints(start, end, normalize)
start, end = _maybe_normalize_endpoints(
start, end, normalize, ambiguous, nonexistent
)
tz = _infer_tz_from_endpoints(start, end, tz)

if tz is not None:
Expand All @@ -466,6 +468,10 @@ def _generate_range(
if end is not None and end.tz is not None:
end = end.tz_localize(None)

start, end = _maybe_normalize_endpoints(
start, end, normalize, ambiguous, nonexistent
)

if isinstance(freq, (Tick, Day)):
i8values = generate_regular_range(start, end, periods, freq, unit=unit)
else:
Expand Down Expand Up @@ -2878,14 +2884,18 @@ def _infer_tz_from_endpoints(


def _maybe_normalize_endpoints(
start: _TimestampNoneT1, end: _TimestampNoneT2, normalize: bool
start: _TimestampNoneT1,
end: _TimestampNoneT2,
normalize: bool,
ambiguous: TimeAmbiguous = "raise",
nonexistent: TimeNonexistent = "raise",
) -> tuple[_TimestampNoneT1, _TimestampNoneT2]:
if normalize:
if start is not None:
start = start.normalize()
start = start.normalize(ambiguous, nonexistent)

if end is not None:
end = end.normalize()
end = end.normalize(ambiguous, nonexistent)

return start, end

Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,28 @@ def test_date_range_uppercase_frequency_raises(self, freq):
with pytest.raises(ValueError, match=msg):
pd.date_range("1/1/2000", periods=4, freq=freq)

def test_date_range_normalize_nonexistent_ambiguous_dst(self):
# GH#62602: Ensure normalize works with nonexistent/ambiguous times (DST)
tz = "Africa/Cairo"
start = pd.Timestamp("2024-04-26 01:00:00", tz=tz)
end = pd.Timestamp("2024-04-27 00:00:00", tz=tz)

result = pd.date_range(
start=start,
end=end,
freq="D",
tz=tz,
nonexistent="shift_forward",
ambiguous=True,
normalize=True,
)

expected = pd.to_datetime(
["2024-04-26 01:00:00", "2024-04-27 00:00:00"], unit="ns"
).tz_localize(tz)

tm.assert_index_equal(result, expected)


def test_factorize_sort_without_freq():
dta = DatetimeArray._from_sequence([0, 2, 1], dtype="M8[ns]")
Expand Down
Loading