{"record":{"id":"10eeed4c42c7c3d2","repo":"pandas-dev/pandas","slug":"cannot-supply-both-a-tz-and-a-dtype-with-a-tz","errorCode":null,"errorMessage":"cannot supply both a tz and a dtype with a tz","messagePattern":"cannot supply both a tz and a dtype with a tz","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":3046,"sourceCode":"\n    Raises\n    ------\n    ValueError : on tzinfo mismatch\n    \"\"\"\n    if dtype is not None:\n        if isinstance(dtype, str):\n            try:\n                dtype = DatetimeTZDtype.construct_from_string(dtype)\n            except TypeError:\n                # Things like `datetime64[ns]`, which is OK for the\n                # constructors, but also nonsense, which should be validated\n                # but not by us. We *do* allow non-existent tz errors to\n                # go through\n                pass\n        dtz = getattr(dtype, \"tz\", None)\n        if dtz is not None:\n            if tz is not None and not timezones.tz_compare(tz, dtz):\n                raise ValueError(\"cannot supply both a tz and a dtype with a tz\")\n            if explicit_tz_none:\n                raise ValueError(\"Cannot pass both a timezone-aware dtype and tz=None\")\n            tz = dtz\n\n        if tz is not None and lib.is_np_dtype(dtype, \"M\"):\n            # We also need to check for the case where the user passed a\n            #  tz-naive dtype (i.e. datetime64[ns])\n            if tz is not None and not timezones.tz_compare(tz, dtz):\n                raise ValueError(\n                    \"cannot supply both a tz and a \"\n                    \"timezone-naive dtype (i.e. datetime64[ns])\"\n                )\n\n    return tz\n\n\ndef _infer_tz_from_endpoints(\n    start: Timestamp, end: Timestamp, tz: tzinfo | None","sourceCodeStart":3028,"sourceCodeEnd":3064,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L3028-L3064","documentation":"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.","triggerScenarios":"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.","commonSituations":"Building dtype strings dynamically and also passing tz; refactor that left both parameters populated.","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."],"exampleFix":"# before\npd.DatetimeIndex(data, dtype='datetime64[ns, UTC]', tz='US/Eastern')\n# after\npd.DatetimeIndex(data, dtype='datetime64[ns, UTC]')","handlingStrategy":"validation","validationCode":"def resolve_tz(dtype=None, tz=None):\n    dt_tz = getattr(dtype, 'tz', None)\n    if dt_tz is not None and tz is not None and str(dt_tz) != str(tz):\n        raise ValueError('conflict between dtype tz and tz= argument')\n    return tz or dt_tz","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Specify tz in exactly one place.","Prefer the dtype string for aware data.","Don't forward both parameters from wrappers."],"tags":["datetime","timezone","dtype","pandas"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}