pola-rs/polars · error
cannot create datetime from other type. dtype
Error message
cannot create datetime from other type. dtype: {} What it means
AnyValue::as_datetime() coerces a scalar into a Datetime AnyValue and only accepts AnyValue::Int64 (the physical timestamp count) or Null. Any other variant (including Int32) panics, reporting the value's dtype, because the physical timestamp representation must be i64.
Solutions
- Provide the literal as i64 in the column's TimeUnit (cast your i32/f64 to i64 first), or use lit(value).cast(DataType::Datetime(tu, tz)).
- Parse strings with str.to_datetime instead of scalar coercion.
- If starting from Date values, cast to Datetime at the expression level rather than calling as_datetime on the scalar.
- Validate the AnyValue variant is Int64/Null before calling as_datetime in plugin code.
Example fix
// before let av: AnyValue<'_> = AnyValue::Int32(ts); // wrong width let d = av.as_datetime(TimeUnit::Microseconds, None); // panics // after let av: AnyValue<'_> = AnyValue::Int64(ts as i64); let d = av.as_datetime(TimeUnit::Microseconds, None); // or in expressions: lit(ts).cast(DataType::Datetime(TimeUnit::Microseconds, None))
Defensive patterns
Strategy: validation
Validate before calling
// scalar must be Int64 (timestamp) or Null before as_datetime
if !matches!(av, AnyValue::Int64(_) | AnyValue::Null) {
return Err(PolarsError::ComputeError("datetime literal must be i64 timestamp or Null".into()));
} Prevention
- Cast literals with lit(v).cast(DataType::Datetime(tu, tz))
- Convert i32/f64 timestamps to i64 before scalar coercion
- Parse datetime strings with str.to_datetime instead
When it happens
Trigger: Calling as_datetime(tu, tz) with an AnyValue::Int32, String, or Float — e.g. an int32 epoch seconds literal passed where an i64 microsecond timestamp is expected.
Common situations: supplying epoch values via bindings where they arrive as i32 or float; fill/filter literals not matching a Datetime column's i64 representation; mixing Date (i32) values with datetime coercion paths.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- cannot create date from other type. dtype
- can only convert date/datetime to NaiveDateTime
- can only convert date/datetime to NaiveTime
- could not cast
- 'float' object cannot be interpreted as a
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/713134f41f4e2713.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-core/src/datatypes/any_value.rs:962
}
impl<'a> AnyValue<'a> {
#[cfg(any(feature = "dtype-date", feature = "dtype-datetime"))]
pub(crate) fn as_date(&self) -> AnyValue<'static> {
match self {
#[cfg(feature = "dtype-date")]
AnyValue::Int32(v) => AnyValue::Date(*v),
AnyValue::Null => AnyValue::Null,
av => panic!("cannot create date from other type. dtype: {}", av.dtype()),
}
}
#[cfg(feature = "dtype-datetime")]
pub(crate) fn as_datetime(&self, tu: TimeUnit, tz: Option<&'a TimeZone>) -> AnyValue<'a> {
match self {
AnyValue::Int64(v) => AnyValue::Datetime(*v, tu, tz),
AnyValue::Null => AnyValue::Null,
av => panic!(
"cannot create datetime from other type. dtype: {}",
av.dtype()
),
}
}
#[cfg(feature = "dtype-datetime")]
pub(crate) fn as_datetime_owned(
&self,
tu: TimeUnit,
tz: Option<Arc<TimeZone>>,
) -> AnyValue<'static> {
match self {
AnyValue::Int64(v) => AnyValue::DatetimeOwned(*v, tu, tz),
AnyValue::Null => AnyValue::Null,
av => panic!(
"cannot create datetime from other type. dtype: {}",
av.dtype()View on GitHub (pinned to fe841f959e)