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
pd.date_range(pd.Timestamp("1916-08-01", tz="Europe/Oslo"), pd.Timestamp("1916-12-01", tz="Europe/Oslo"), freq="1MS", ambiguous=True)
Issue Description
The code above produced the following error
File "...\pandas\core\indexes\datetimes.py", line 945, in date_range
dtarr = DatetimeArray._generate_range(
File "...\pandas\core\arrays\datetimes.py", line 460, in _generate_range
i8values = tzconversion.tz_localize_to_utc(
File "pandas\_libs\tslibs\tzconversion.pyx", line 362, in pandas._libs.tslibs.tzconversion.tz_localize_to_utc
pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from 1916-10-01 00:00:00, try using the 'ambiguous' argument
Note that if you give the time zone as an argument to the function and use naive timestamps, you do not get the bug
>>> pd.date_range("1916-08-01", "1916-12-01", tz="Europe/Oslo", freq="1MS", ambiguous=True)
DatetimeIndex(['1916-08-01 00:00:00+02:00', 1916-09-01 00:00:00+02:00',
1916-10-01 00:00:00+02:00', 1916-11-01 00:00:00+01:00',
1916-12-01 00:00:00+01:00],
dtype='datetime64[ns, Europe/Oslo]', length=5, freq='1MS')
Expected Behavior
>>> pd.date_range(pd.Timestamp("1916-08-01", tz="Europe/Oslo"), pd.Timestamp("1916-12-01", tz="Europe/Oslo"), freq="1MS", ambiguous=True)
DatetimeIndex(['1916-08-01 00:00:00+02:00', 1916-09-01 00:00:00+02:00',
1916-10-01 00:00:00+02:00', 1916-11-01 00:00:00+01:00',
1916-12-01 00:00:00+01:00],
dtype='datetime64[ns, Europe/Oslo]', length=5, freq='1MS')
The problem is that 1916-10-01 00:00:00 is an ambiguous in Europe/Oslo as this is the DST transition, thus there are two possible UTC times 1916-10-01 00:00:00+02:00
and 1916-10-01 00:00:00+01:00
. The ambiguous argument to date_range
is not utilized.
Installed Versions
INSTALLED VERSIONS
commit : 37ea63d
python : 3.8.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 2.0.1
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.6.1
pip : 23.0.1
Cython : None
pytest : 7.3.0
hypothesis : None
sphinx : 5.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None