{"record":{"id":"b39df205f616898d","repo":"pandas-dev/pandas","slug":"cannot-pass-both-a-timezone-aware-dtype-and-tz-non","errorCode":null,"errorMessage":"Cannot pass both a timezone-aware dtype and tz=None","messagePattern":"Cannot pass both a timezone-aware dtype and tz=None","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":3048,"sourceCode":"    ------\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\n) -> tzinfo | None:\n    \"\"\"","sourceCodeStart":3030,"sourceCodeEnd":3066,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L3030-L3066","documentation":"Raised by _validate_tz_from_dtype (with explicit_tz_none) when the user passes a timezone-aware dtype together with tz=None explicitly. Passing tz=None to force naive data conflicts with a tz-aware dtype, so it is rejected. ValueError.","triggerScenarios":"pd.DatetimeIndex(data, dtype='datetime64[ns, UTC]', tz=None); a wrapper that always forwards tz=None as a default colliding with a tz-aware dtype.","commonSituations":"API defaults where tz=None means 'no preference' but the dtype already encodes a tz; mixing legacy code that passes tz=None with new dtype-aware code.","solutions":["Drop tz=None and let the dtype's tz win.","Or pass a tz-naive dtype ('datetime64[ns]') if you genuinely want naive output.","Refactor wrappers to use a sentinel (e.g. lib.no_default) instead of tz=None when 'unspecified' is meant."],"exampleFix":"# before\npd.DatetimeIndex(data, dtype='datetime64[ns, UTC]', tz=None)\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 None:\n        raise ValueError('tz-aware dtype conflicts with explicit tz=None')\n    return tz or dt_tz","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use a sentinel for 'unspecified' rather than tz=None.","Drop tz=None when the dtype encodes a tz.","Document whether tz=None means 'naive' or 'unspecified'. "],"tags":["datetime","timezone","dtype","pandas"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}