pandas-dev/pandas · error · ValueError
cannot supply both a tz and a dtype with a tz
Error message
cannot supply both a tz and a dtype with a tz
What it means
Raised by _validate_tz_from_dtype when the user supplies both an explicit tz= argument and a dtype that itself carries a tz, and the two disagree. The two tz sources conflict, so pandas refuses to guess. ValueError.
Source
Thrown at pandas/core/arrays/datetimes.py:3046
Raises
------
ValueError : on tzinfo mismatch
"""
if dtype is not None:
if isinstance(dtype, str):
try:
dtype = DatetimeTZDtype.construct_from_string(dtype)
except TypeError:
# Things like `datetime64[ns]`, which is OK for the
# constructors, but also nonsense, which should be validated
# but not by us. We *do* allow non-existent tz errors to
# go through
pass
dtz = getattr(dtype, "tz", None)
if dtz is not None:
if tz is not None and not timezones.tz_compare(tz, dtz):
raise ValueError("cannot supply both a tz and a dtype with a tz")
if explicit_tz_none:
raise ValueError("Cannot pass both a timezone-aware dtype and tz=None")
tz = dtz
if tz is not None and lib.is_np_dtype(dtype, "M"):
# We also need to check for the case where the user passed a
# tz-naive dtype (i.e. datetime64[ns])
if tz is not None and not timezones.tz_compare(tz, dtz):
raise ValueError(
"cannot supply both a tz and a "
"timezone-naive dtype (i.e. datetime64[ns])"
)
return tz
def _infer_tz_from_endpoints(
start: Timestamp, end: Timestamp, tz: tzinfo | NoneView on GitHub (pinned to 71959b8cb9)
Solutions
- Provide the tz in exactly one place — prefer the dtype string and omit tz=.
- Or pass a tz-naive dtype plus tz= and let pandas build the aware dtype.
- Make sure when both are present they reference the same zone.
Example fix
# before pd.DatetimeIndex(data, dtype='datetime64[ns, UTC]', tz='US/Eastern') # after pd.DatetimeIndex(data, dtype='datetime64[ns, UTC]')
Defensive patterns
Strategy: validation
Validate before calling
def resolve_tz(dtype=None, tz=None):
dt_tz = getattr(dtype, 'tz', None)
if dt_tz is not None and tz is not None and str(dt_tz) != str(tz):
raise ValueError('conflict between dtype tz and tz= argument')
return tz or dt_tz Prevention
- Specify tz in exactly one place.
- Prefer the dtype string for aware data.
- Don't forward both parameters from wrappers.
When it happens
Trigger: pd.DatetimeIndex(data, dtype='datetime64[ns, UTC]', tz='US/Eastern'); constructing a Series/DatetimeIndex with both a tz-aware dtype string and a tz kwarg pointing at different zones.
Common situations: Building dtype strings dynamically and also passing tz; refactor that left both parameters populated.
Related errors
- Cannot pass both a timezone-aware dtype and tz=None
- cannot supply both a tz and a timezone-naive dtype (i.e. dat
- Cannot compare tz-naive and tz-aware datetime-like objects.
- Cannot compare tz-naive and tz-aware datetime-like objects
- Cannot convert tz-naive timestamps, use tz_localize to local
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/10eeed4c42c7c3d2.
Report an issue: GitHub.