pola-rs/polars · error

cannot create date from other type. dtype

Error message

cannot create date from other type. dtype: {}

What it means

AnyValue::as_date() reinterprets a scalar as a Date and only accepts AnyValue::Int32 (the physical days-since-epoch representation) or Null. Any other variant panics with its actual dtype, since converting e.g. a string or i64 to Date would be ambiguous.

Solutions

  1. Provide the literal in the correct physical form (i32 days since epoch) or cast the Series/literal first: lit(value).cast(DataType::Date).
  2. Convert strings with str.to_date(format) rather than feeding them through scalar coercion.
  3. If you have an i64 timestamp, cast to Datetime and then .cast(DataType::Date).
  4. Check the input value's type before the call and reject/convert non-Int32 values in the caller.

Example fix

// before
let av: AnyValue<'_> = AnyValue::Int64(days); // wrong width
let d = av.as_date(); // panics
// after
let av: AnyValue<'_> = AnyValue::Int32(days as i32);
let d = av.as_date();
// or in expressions: lit(value).cast(DataType::Date)
Defensive patterns

Strategy: validation

Validate before calling

// scalar must be Int32 (days) or Null before as_date
if !matches!(av, AnyValue::Int32(_) | AnyValue::Null) {
    return Err(PolarsError::ComputeError("date literal must be i32 days or Null".into()));
}

Prevention

When it happens

Trigger: Calling as_date() (pub(crate) — reached via public APIs that coerce literals/scalars to dates) with an AnyValue of type Int64, String, Float, etc., instead of Int32 or Null.

Common situations: Passing a Python int or str as a date literal into a date-typed expression; fill_null/filter with a literal whose physical type doesn't match the Date column's i32 days representation; bindings auto-converting scalars without a cast.

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


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/01e728ba14d75622. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-core/src/datatypes/any_value.rs:953

    T: Into<AnyValue<'a>>,
{
    #[inline]
    fn from(a: Option<T>) -> Self {
        match a {
            None => AnyValue::Null,
            Some(v) => v.into(),
        }
    }
}

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,

View on GitHub (pinned to fe841f959e)