pola-rs/polars · error · ValueError
invalid `time_unit` Expected one of
Error message
invalid `time_unit`
Expected one of {'ns','us','ms'}, got {time_unit!r}. What it means
ValueError raised by Datetime.__init__: the time_unit argument is not one of 'ms', 'us', 'ns', which are the only resolutions a Datetime dtype can carry. The guard fires during dtype construction, echoing the invalid time_unit.
Solutions
- Use time_unit 'ns', 'us', or 'ms'.
Example fix
pl.Datetime(time_unit='us')
Defensive patterns
Strategy: validation
When it happens
Trigger: Invalid time_unit for a Datetime/Duration dtype.
Common situations: See trigger scenarios.
AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19).
Data as JSON: /api/errors/76979527a3709c81.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/datatypes/classes.py:608
-----
The underlying representation of this type is a 64-bit signed integer.
The integer indicates the number of time units since the Unix epoch
(1970-01-01 00:00:00). The number can be negative to indicate datetimes before the
epoch.
"""
time_unit: TimeUnit
time_zone: str | None
def __init__(
self, time_unit: TimeUnit = "us", time_zone: str | tzinfo | None = None
) -> None:
if time_unit not in ("ms", "us", "ns"):
msg = (
"invalid `time_unit`"
f"\n\nExpected one of {{'ns','us','ms'}}, got {time_unit!r}."
)
raise ValueError(msg)
if isinstance(time_zone, tzinfo):
time_zone = str(time_zone)
self.time_unit = time_unit
self.time_zone = time_zone
def __eq__(self, other: PolarsDataType) -> bool: # type: ignore[override]
# allow comparing object instances to class
if type(other) is DataTypeClass and issubclass(other, Datetime):
return True
elif isinstance(other, Datetime):
return (
self.time_unit == other.time_unit and self.time_zone == other.time_zone
)
else:
return False
View on GitHub (pinned to 68506541d2)