{"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":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":3046,"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":3028,"sourceCodeEnd":3064,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimes.py#L3028-L3064","documentation":"Raised by _validate_tz_from_dtype when the dtype carries a tz (dtz is not None) and the caller explicitly passed `tz=None` (signaled by the explicit_tz_none flag, set when the user wrote tz=None literally rather than omitting it). This catches the contradictory request 'aware dtype but explicitly no tz'.","triggerScenarios":"Calling `pd.DatetimeIndex(values, dtype='datetime64[ns, UTC]', tz=None)` — the literal tz=None conflicts with the tz embedded in the dtype. Reached when library code forwards a user-supplied tz=None alongside a tz-aware dtype.","commonSituations":"Wrapper functions that pass `tz=tz` where tz defaults to None, combined with a tz-aware default dtype. Refactoring that introduced explicit_tz_none semantics (older pandas would silently drop).","solutions":["Drop the explicit `tz=None` and let the dtype's tz win.","If you genuinely want a tz-naive result, also pass a tz-naive dtype like 'datetime64[ns]'.","In library wrappers, treat `tz is None` as 'use dtype tz' (don't forward None literally when dtype carries tz)."],"exampleFix":"// before\ndti = pd.DatetimeIndex(values, dtype='datetime64[ns, UTC]', tz=None)\n\n// after\ndti = pd.DatetimeIndex(values, dtype='datetime64[ns, UTC]')","handlingStrategy":"validation","validationCode":"import pandas as pd\n\ndef drop_explicit_none(dtype_str, tz):\n    dtz = None\n    if dtype_str:\n        try:\n            d = pd.DatetimeTZDtype.construct_from_string(dtype_str)\n            dtz = getattr(d, 'tz', None)\n        except TypeError:\n            pass\n    if dtz is not None and tz is None:\n        return dtype_str, 'omit'  # do not forward tz=None\n    return dtype_str, tz","typeGuard":"def explicit_none_safe(dtype_str, explicit_tz_none) -> bool:\n    import pandas as pd\n    dtz = None\n    if dtype_str:\n        try:\n            d = pd.DatetimeTZDtype.construct_from_string(dtype_str)\n            dtz = getattr(d, 'tz', None)\n        except TypeError:\n            pass\n    return not (dtz is not None and explicit_tz_none)","tryCatchPattern":"try:\n    dti = pd.DatetimeIndex(values, dtype=dtype_str, tz=tz)\nexcept ValueError as e:\n    if 'timezone-aware dtype and tz=None' in str(e):\n        kwargs = {'dtype': dtype_str}\n        if tz is not None:\n            kwargs['tz'] = tz\n        dti = pd.DatetimeIndex(values, **kwargs)\n    else:\n        raise","preventionTips":["Treat `tz is None` as 'unset' and do not forward it literally when dtype carries tz.","Document tz=None semantics clearly in wrapper APIs."],"tags":["datetime","timezone","dtype","construction","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}