{"record":{"id":"973fe7ffd85a6c3c","repo":"pandas-dev/pandas","slug":"cannot-supply-both-a-tz-and-a-timezone-naive-dtype","errorCode":null,"errorMessage":"cannot supply both a tz and a timezone-naive dtype (i.e. datetime64[ns])","messagePattern":"cannot supply both a tz and a timezone-naive dtype \\(i\\.e\\. datetime64\\[ns\\]\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":3055,"sourceCode":"            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\n) -> tzinfo | None:\n    \"\"\"\n    If a timezone is not explicitly given via `tz`, see if one can\n    be inferred from the `start` and `end` endpoints.  If more than one\n    of these inputs provides a timezone, require that they all agree.\n\n    Parameters\n    ----------\n    start : Timestamp","sourceCodeStart":3037,"sourceCodeEnd":3073,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L3037-L3073","documentation":"Raised by _validate_tz_from_dtype when a tz= argument is supplied alongside a tz-naive numpy datetime dtype (datetime64[ns]) and they conflict. The dtype declares 'no tz' while tz asks for one; pandas treats that as contradictory input. ValueError.","triggerScenarios":"pd.DatetimeIndex(data, dtype='datetime64[ns]', tz='UTC'); passing a plain datetime64[ns] dtype plus a tz to astype or a constructor.","commonSituations":"Helper that always sets dtype='datetime64[ns]' then also forwards a tz kwarg; copy-pasted dtype that should have been a DatetimeTZDtype string.","solutions":["Drop the dtype and just pass tz=, letting pandas build the aware dtype.","Or change the dtype to a tz-aware form like 'datetime64[ns, UTC]' and omit tz=.","Avoid hardcoding tz-naive dtypes in code that also accepts a tz argument."],"exampleFix":"# before\npd.DatetimeIndex(data, dtype='datetime64[ns]', tz='UTC')\n# after\npd.DatetimeIndex(data, tz='UTC')","handlingStrategy":"validation","validationCode":"def resolve_tz(dtype=None, tz=None):\n    is_naive_dt = isinstance(dtype, np.dtype) and dtype.kind == 'M' and getattr(dtype, 'tz', None) is None\n    if is_naive_dt and tz is not None:\n        raise ValueError('tz-naive dtype conflicts with tz= argument')\n    return tz","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't hardcode datetime64[ns] dtype when tz is configurable.","Omit dtype and pass tz= to let pandas build the aware dtype.","Use DatetimeTZDtype strings for aware paths."],"tags":["datetime","timezone","dtype","pandas"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}