{"record":{"id":"2db79f9402531d8d","repo":"pandas-dev/pandas","slug":"data-is-already-tz-aware-inferred-tz-unable-to","errorCode":null,"errorMessage":"data is already tz-aware {inferred_tz}, unable to set specified tz: {tz}","messagePattern":"data is already tz-aware (.+?), unable to set specified tz: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":2946,"sourceCode":"    Parameters\n    ----------\n    tz : tzinfo or None\n    inferred_tz : tzinfo or None\n\n    Returns\n    -------\n    tz : tzinfo or None\n\n    Raises\n    ------\n    TypeError : if both timezones are present but do not match\n    \"\"\"\n    if tz is None:\n        tz = inferred_tz\n    elif inferred_tz is None:\n        pass\n    elif not timezones.tz_compare(tz, inferred_tz):\n        raise TypeError(\n            f\"data is already tz-aware {inferred_tz}, unable to set specified tz: {tz}\"\n        )\n    return tz\n\n\ndef _validate_dt64_dtype(dtype):\n    \"\"\"\n    Check that a dtype, if passed, represents either a numpy datetime64[ns]\n    dtype or a pandas DatetimeTZDtype.\n\n    Parameters\n    ----------\n    dtype : object\n\n    Returns\n    -------\n    dtype : None, numpy.dtype, or DatetimeTZDtype\n","sourceCodeStart":2928,"sourceCodeEnd":2964,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimes.py#L2928-L2964","documentation":"Raised by _validate_tz_from_dtype when both a tz argument and an inferred tz (from the data) are present and they disagree per timezones.tz_compare. The function's contract (documented in its Raises section) is to refuse silently substituting one tz for another. Both dtype-level tz and per-element tz must agree.","triggerScenarios":"Calling `pd.DatetimeIndex(values_with_tz_A, tz='B')`, `pd.to_datetime(values, utc=False)` where values already carry tz A and you pass tz B, or constructing a Series/Index with conflicting tz sources. Also reached via the DatetimeIndex constructor when `dtype='datetime64[ns, US/Pacific]'` is passed for data inferred as US/Eastern.","commonSituations":"Loading tz-aware data from Parquet/SQL and then re-stamping with a tz via the constructor. Mixing a tz keyword argument with tz-aware Timestamps in a list. Misreading source data's tz and supplying the wrong override.","solutions":["Pick one source of truth: either drop the `tz=` argument and let pandas infer, or strip tz from the data and pass the desired tz explicitly.","Convert the data's tz first: `s.dt.tz_convert(target_tz)` and then construct without a separate tz arg.","If the data tz is wrong, fix it at ingest rather than overriding at construction."],"exampleFix":"// before\ndti = pd.DatetimeIndex(list_of_est_timestamps, tz='US/Pacific')\n\n// after\ndti = pd.DatetimeIndex(list_of_est_timestamps).tz_convert('US/Pacific')","handlingStrategy":"validation","validationCode":"import pandas as pd\nfrom pandas.core.tools.datetimes import _validate_tz_from_dtype  # illustrative\n\ndef reconcile_tz(data_tz, kwarg_tz):\n    if data_tz is not None and kwarg_tz is not None:\n        if not pd.core.dtypes.common.timezones.tz_compare(data_tz, kwarg_tz):\n            raise ValueError('tz mismatch')\n    return kwarg_tz or data_tz","typeGuard":"def tz_consistent(data_tz, kwarg_tz) -> bool:\n    if data_tz is None or kwarg_tz is None:\n        return True\n    import pandas as pd\n    return pd.core.dtypes.common.timezones.tz_compare(data_tz, kwarg_tz)","tryCatchPattern":"try:\n    dti = pd.DatetimeIndex(values, tz=tz)\nexcept TypeError as e:\n    if 'already tz-aware' in str(e):\n        dti = pd.DatetimeIndex(values).tz_convert(tz)\n    else:\n        raise","preventionTips":["Supply tz through one channel only.","When data already carries tz, omit the tz kwarg and convert post-construction."],"tags":["datetime","timezone","construction","dtype-validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}