{"record":{"id":"6b68152f636136ee","repo":"pola-rs/polars","slug":"dtype-must-be-of-type-date-datetime-time","errorCode":null,"errorMessage":"`dtype` must be of type {Date, Datetime, Time}","messagePattern":"`dtype` must be of type (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/string.py","lineNumber":332,"sourceCode":"        if dtype == Date:\n            return self.to_date(format, strict=strict, exact=exact, cache=cache)\n        elif dtype == Datetime:\n            time_unit = getattr(dtype, \"time_unit\", None)\n            time_zone = getattr(dtype, \"time_zone\", None)\n            return self.to_datetime(\n                format,\n                time_unit=time_unit,\n                time_zone=time_zone,\n                strict=strict,\n                exact=exact,\n                cache=cache,\n                ambiguous=ambiguous,\n            )\n        elif dtype == Time:\n            return self.to_time(format, strict=strict, cache=cache)\n        else:\n            msg = \"`dtype` must be of type {Date, Datetime, Time}\"\n            raise ValueError(msg)\n\n    @deprecate_nonkeyword_arguments(allowed_args=[\"self\"], version=\"1.20.0\")\n    @unstable()\n    def to_decimal(self, *, scale: int) -> Expr:\n        \"\"\"\n        Convert a String column into a Decimal column.\n\n        .. engine-support:: in-memory, streaming, distributed\n\n        .. warning::\n            This functionality is considered **unstable**. It may be changed\n            at any point without it being considered a breaking change.\n\n        .. versionchanged:: 1.20.0\n            Parameter `inference_length` should now be passed as a keyword argument.\n\n        .. versionchanged:: 1.33.0\n            Parameter `inference_length` was removed and `scale` was made non-optional.","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/string.py#L314-L350","documentation":"Expr.str.strptime is a dispatcher over the target temporal dtype: dtype=pl.Date routes to to_date, pl.Datetime to to_datetime (honouring time_unit/time_zone if given an instantiated class), and pl.Time to to_time. Any dtype outside {Date, Datetime, Time} — numeric types, String, Duration, None — reaches the else branch and raises ValueError, because string parsing to that type is undefined.","triggerScenarios":"pl.col('s').str.strptime(pl.Int64), str.strptime(None) via an unset variable, or str.strptime(pl.String). Parametrized temporal classes (pl.Datetime('ms')) are fine — they compare equal to the base class.","commonSituations":"Parsing strings that actually hold numbers (e.g. '123') with strptime instead of cast; dtype passed through a generic column-spec where non-temporal types slip in; forgetting that ISO strings to Duration is not supported via strptime.","solutions":["For numeric-looking strings use .cast(pl.Int64) (add .str.strip_chars() if needed) — str.strptime is temporal-only.","For timestamps pass pl.Date, pl.Datetime, or pl.Time.","In generic pipelines, branch: temporal spec -> str.strptime, otherwise -> cast."],"exampleFix":"# before\npl.col('s').str.strptime(pl.Int64, strict=False)\n\n# after\npl.col('s').cast(pl.Int64, strict=False)\n# temporal stays:\npl.col('s').str.strptime(pl.Datetime, '%Y-%m-%d')","handlingStrategy":"type-guard","validationCode":"import polars as pl\nfrom polars import Date, Datetime, Time\nassert dtype in (Date, Datetime, Time) or isinstance(dtype, (Date, Datetime, Time)), f'strptime dtype must be temporal, got {dtype!r}'","typeGuard":"import polars as pl\n\ndef is_temporal_dtype(dt) -> bool:\n    return dt in (pl.Date, pl.Datetime, pl.Time) or isinstance(dt, (pl.Date, pl.Datetime, pl.Time))","tryCatchPattern":null,"preventionTips":["str.strptime is for Date/Datetime/Time only — everything else is a cast.","Passing instantiated classes like pl.Datetime('ms', 'UTC') is supported and recommended for timezone-aware parsing."],"tags":["validation","polars","expression","string","dtype","temporal"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}