Skip to content

BUG: Series.dt.tz_localize not recognizing DST transition with zoneinfo timezone when ambiguous="infer" #48442

Closed
@jamesdow21

Description

@jamesdow21

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
import pytz
from zoneinfo import ZoneInfo
from datetime import datetime, timedelta

dts = [datetime(2022, 11, 6, 1, 0) + timedelta(minutes = 15*i) for i in range(4)]*2 + [datetime(2022, 11, 6, 2, 0) + timedelta(minutes = 15*i) for i in range(2)]

s = pd.Series(dts)

zoneinfo_infer = s.dt.tz_localize(ZoneInfo('US/Eastern'), ambiguous = 'infer')
print('Result of `tz_localize` with zoneinfo: (all ambiguous times showing UTC -4:00')
print(zoneinfo_infer)
print('-'*80)

pytz_infer = s.dt.tz_localize(pytz.timezone('US/Eastern'), ambiguous = 'infer')
print('Result of `tz_localize` with pytz: (correctly showing DST transition')
print(pytz_infer)


# should raise AmbiguousTimeError, but doesn't
s.dt.tz_localize(ZoneInfo('US/Eastern'))

# does raise AmbiguousTimeError
s.dt.tz_localize(pytz.timezone('US/Eastern'))

Issue Description

On 1.5.0rc0

Series.dt.tz_localize using a zoneinfo.Zoneinfo timezone does not correctly recognize a daylight savings time transition

Looks like the cause of this is:

Expected Behavior

Usage of zoneinfo should match that of pytz or dateutil

Installed Versions

INSTALLED VERSIONS

commit : 224458e
python : 3.10.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.5.0rc0
numpy : 1.23.1
pytz : 2022.1
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : 2022.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions