-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
start = pd.Timestamp("2024-04-26 01:00:00", tz="Africa/Cairo")
end = pd.Timestamp("2024-04-27 00:00:00", tz="Africa/Cairo")
tz = "Africa/Cairo"
freq = "D"
full_range = pd.date_range(
start=start,
end=end,
freq=freq,
tz=tz,
nonexistent="shift_forward", # should handle DST spring forward
ambiguous=True, # should handle DST fall back
normalize=True,
)
Issue Description
pandas.date_range()
raises NonExistentTimeError
when the generated range crosses a daylight saving time (DST) transition,
even when the user explicitly provides nonexistent="shift_forward"
and ambiguous=True
.
This makes it impossible to safely generate timezone-aware ranges that include DST boundaries.
Example
import pandas as pd
start = pd.Timestamp("2024-04-26 01:00:00", tz="Africa/Cairo")
end = pd.Timestamp("2024-04-27 00:00:00", tz="Africa/Cairo")
tz = "Africa/Cairo"
freq = "D"
full_range = pd.date_range(
start=start,
end=end,
freq=freq,
tz=tz,
nonexistent="shift_forward", # should handle DST spring forward
ambiguous=True, # should handle DST fall back
normalize=True,
)
Actual behavior
NonExistentTimeError: 2024-04-26 00:00:00
Even though nonexistent="shift_forward"
is provided, date_range()
still fails because internal normalization generates a boundary at 2024-04-26 00:00:00
, which does not exist in Cairo (clocks jump from 23:59 → 01:00).
Additional note
The problem can be observed even before calling date_range()
:
pd.Timestamp("2024-04-26", tz="Africa/Cairo")
raises:
NonExistentTimeError: 2024-04-26 00:00:00
To make it work, one must manually adjust the timestamp:
# These work correctly
pd.Timestamp("2024-04-26 01:00:00", tz="Africa/Cairo")
pd.Timestamp("2024-04-26").tz_localize("Africa/Cairo", nonexistent="shift_forward")
However, date_range()
should handle this internally when nonexistent
and ambiguous
are specified.
Expected Behavior
The nonexistent
and ambiguous
arguments should consistently apply to all generated timestamps,
including internal boundaries produced during normalization.
Expected output:
DatetimeIndex(
['2024-04-26 01:00:00+02:00', '2024-04-27 00:00:00+02:00'],
dtype='datetime64[ns, Africa/Cairo]',
freq='D'
)
Installed Versions
INSTALLED VERSIONS
commit : 9c8bc3e
python : 3.11.13
python-bits : 64
OS : Linux
OS-release : 5.15.167.4-microsoft-standard-WSL2
Version : #1 SMP Tue Nov 5 00:21:55 UTC 2024
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.3.3
numpy : 2.3.3
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : None
IPython : 9.6.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None