pola-rs/polars · error · ValueError
`dtype` must be of type
Error message
`dtype` must be of type {Date, Datetime, Time} What it means
Expr.str.strptime() dispatches on `dtype`: Date calls to_date, Datetime calls to_datetime, Time calls to_time. Any other dtype (String, Int64, etc.) has no string-parsing path, so Polars raises this ValueError. It means you passed a dtype the method cannot convert to.
Solutions
- Pass pl.Date, pl.Datetime, or pl.Time as dtype.
- If you need a different target type, parse to Date/Datetime first, then cast with .cast(...).
- Fix the variable feeding dtype so it resolves to one of the three temporal dtypes.
Example fix
// before
df.select(pl.col('s').str.strptime(pl.Utf8, '%Y-%m-%d'))
// after
df.select(pl.col('s').str.strptime(pl.Date, '%Y-%m-%d')) Defensive patterns
Strategy: validation
Validate before calling
if dtype not in (pl.Date, pl.Datetime, pl.Time):
raise ValueError(f'dtype must be Date/Datetime/Time, got {dtype}') Type guard
def is_temporal_dtype(dtype) -> bool:
return dtype in (pl.Date, pl.Datetime, pl.Time) Try / catch
try:
expr = pl.col('s').str.strptime(dtype, fmt)
except ValueError:
expr = pl.col('s').str.strptime(pl.Datetime, fmt) Prevention
- strptime's dtype is the TARGET type, not the source column type.
- Use Literal or an explicit enum of allowed dtypes in wrappers.
- For non-temporal targets, parse to a temporal type then .cast().
When it happens
Trigger: Calling Expr.str.strptime(pl.Int64, fmt) or strptime(pl.Utf8, ...) — any dtype outside {Date, Datetime, Time}, often from parameterizing dtype generically.
Common situations: Passing the source column dtype by mistake instead of the target dtype; dtype chosen via config/dict lookup; assuming strptime converts to numeric types.
Related errors
- arr.dot query vector must be one-dimensional
- can not match overlapping patterns when leftmost == True
- cannot compare datetime.datetime to Series of type
- cannot construct PySeries for type
- cannot convert DataFrame to
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/6b68152f636136ee.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/expr/string.py:330
if dtype == Date:
return self.to_date(format, strict=strict, exact=exact, cache=cache)
elif dtype == Datetime:
time_unit = getattr(dtype, "time_unit", None)
time_zone = getattr(dtype, "time_zone", None)
return self.to_datetime(
format,
time_unit=time_unit,
time_zone=time_zone,
strict=strict,
exact=exact,
cache=cache,
ambiguous=ambiguous,
)
elif dtype == Time:
return self.to_time(format, strict=strict, cache=cache)
else:
msg = "`dtype` must be of type {Date, Datetime, Time}"
raise ValueError(msg)
@unstable()
def to_decimal(self, *, scale: int) -> Expr:
"""
Convert a String column into a Decimal column.
.. engine-support:: in-memory, streaming, distributed
.. warning::
This functionality is considered **unstable**. It may be changed
at any point without it being considered a breaking change.
.. versionchanged:: 1.33.0
Parameter `inference_length` was removed and `scale` was made non-optional.
Parameters
----------
scaleView on GitHub (pinned to fe841f959e)