{"record":{"id":"a3b1c12f0865370c","repo":"pola-rs/polars","slug":"time-zone-of-dtype-dtype-tz-r-differs-from-tim","errorCode":null,"errorMessage":"time zone of dtype ({dtype_tz!r}) differs from time zone of value ({value_tz!r})","messagePattern":"time zone of dtype \\((.+?)\\) differs from time zone of value \\((.+?)\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/lit.py","lineNumber":155,"sourceCode":"                    value = value.astimezone(timezone.utc)\n                    tz = \"UTC\"\n\n            # dtype and value both have same time zone\n            elif str(value_tz) == dtype_tz:\n                tz = str(value_tz)\n\n            # given a fixed offset from UTC that matches the dtype tz offset\n            elif hasattr(value_tz, \"utcoffset\") and getattr(\n                ZoneInfo(dtype_tz).utcoffset(value), \"seconds\", 0\n            ) == getattr(value_tz.utcoffset(value), \"seconds\", 1):\n                tz = dtype_tz\n            else:\n                # value has time zone that differs from dtype time zone\n                msg = (\n                    f\"time zone of dtype ({dtype_tz!r}) differs from time zone of \"\n                    f\"value ({value_tz!r})\"\n                )\n                raise TypeError(msg)\n\n        dt_utc = value.replace(tzinfo=timezone.utc)\n        dt_utc_s = pl.Series(\"literal\", [dt_utc]).cast(Datetime(time_unit))\n        if tz is not None:\n            dt_utc_s = dt_utc_s.dt.replace_time_zone(\n                tz, ambiguous=\"earliest\" if value.fold == 0 else \"latest\"\n            )\n        expr = wrap_expr(plr.lit(dt_utc_s._s, allow_object=False, is_scalar=True))\n        return expr\n\n    elif isinstance(value, timedelta):\n        value_s = pl.Series(\"literal\", [value])\n        if dtype is not None and (tu := getattr(dtype, \"time_unit\", None)) is not None:\n            tu = cast(\"TimeUnit\", tu)\n            value_s = value_s.cast(Duration(tu))\n        expr = wrap_expr(plr.lit(value_s._s, allow_object=False, is_scalar=True))\n        return expr\n","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/lit.py#L137-L173","documentation":"`pl.lit` was given a tz-aware datetime and a `Datetime` dtype whose time zone differs from the value's time zone. Polars will not silently reinterpret time zones, so it raises rather than producing shifted timestamps.","triggerScenarios":"`pl.lit(datetime_with_tz, dtype=pl.Datetime('us', 'Other/Zone'))` where the Python datetime's `tzinfo` resolves to a different zone name than the dtype's `time_zone`.","commonSituations":"Mixing 'UTC' with a named zone like 'Europe/Paris'; building literals from user-supplied datetimes with arbitrary tzinfo; DST-related code that swaps tzinfo.","solutions":["Make the dtype time zone match the value's, e.g. `pl.Datetime('us', 'UTC')` for a UTC-aware value","Convert the value first: `value.astimezone(zoneinfo.ZoneInfo(dtype_tz))`","Use a tz-naive dtype + `dt.replace_time_zone` on the resulting expression if you want wall-clock semantics"],"exampleFix":"// before\npl.lit(dt_paris, dtype=pl.Datetime('us', 'UTC'))\n// after\npl.lit(dt_paris.astimezone(ZoneInfo('Europe/Paris')), dtype=pl.Datetime('us', 'Europe/Paris'))","handlingStrategy":"validation","validationCode":"value_tz = str(value.tzinfo) if value.tzinfo else None\nif value_tz and dtype.time_zone and value_tz != dtype.time_zone:\n    value = value.astimezone(ZoneInfo(dtype.time_zone))","typeGuard":"def tz_matches(value, dtype) -> bool:\n    return value.tzinfo is None or dtype.time_zone is None or str(value.tzinfo) == dtype.time_zone","tryCatchPattern":"try:\n    expr = pl.lit(value, dtype=dt_dtype)\nexcept TypeError as e:\n    if 'time zone of dtype' in str(e):\n        value = value.astimezone(ZoneInfo(dt_dtype.time_zone))\n        expr = pl.lit(value, dtype=dt_dtype)\n    else:\n        raise","preventionTips":["Normalize all datetimes to a canonical zone before pl.lit","Keep dtype time_zone and value tzinfo sourced from the same constant"],"tags":["python","polars","datetime","timezone"],"backgroundTag":"dtype-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}