Skip to content

BUG: Handling of Pydantic v2 time zone objects #59951

@toby-coleman

Description

@toby-coleman

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

from datetime import datetime, timezone
import pandas as pd
from pydantic import BaseModel

class Model(BaseModel):
    dt: datetime

model = Model(dt="2024-06-01 00:00:00+00:00")
dt2 = datetime(2024, 6, 10, tzinfo=timezone.utc)

dt_range = pd.date_range(model.dt, dt2)

Issue Description

Calling pd.date_range results the following error (full stack trace below): Inputs must both have the same timezone, UTC != UTC

The issue arises because:

  • model.dt has a timezone of type pydantic_core._pydantic_core.TzInfo; whereas
  • dt2 has a timezone of type datetime.timezone.

This appears to be related to the issue discussed here: pydantic/pydantic#8195 - not sure if this is best tackled in Pandas or Pydantic, but the current situation is very confusing when the error message reports UTC != UTC.

For reference my Pydantic versions are:

pydantic          2.9.2
pydantic_core     2.23.4
Traceback (most recent call last):
  File "/home/tcoleman/miniconda3/envs/tmp-pandas/lib/python3.12/site-packages/pandas/core/arrays/datetimes.py", line 2646, in _infer_tz_from_endpoints
    inferred_tz = timezones.infer_tzinfo(start, end)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "timezones.pyx", line 369, in pandas._libs.tslibs.timezones.infer_tzinfo
AssertionError: Inputs must both have the same timezone, UTC != UTC

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tcoleman/miniconda3/envs/tmp-pandas/lib/python3.12/site-packages/pandas/core/indexes/datetimes.py", line 1008, in date_range
    dtarr = DatetimeArray._generate_range(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tcoleman/miniconda3/envs/tmp-pandas/lib/python3.12/site-packages/pandas/core/arrays/datetimes.py", line 445, in _generate_range
    tz = _infer_tz_from_endpoints(start, end, tz)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tcoleman/miniconda3/envs/tmp-pandas/lib/python3.12/site-packages/pandas/core/arrays/datetimes.py", line 2649, in _infer_tz_from_endpoints
    raise TypeError(
TypeError: Start and end cannot both be tz-aware with different timezones

Expected Behavior

Ability to run pd.date_range without error when the timezones are the same.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.6
python-bits : 64
OS : Linux
OS-release : 5.15.123.1-microsoft-standard-WSL2
Version : #1 SMP Mon Aug 7 19:01:48 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.2.3
numpy : 2.1.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
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 : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions