pola-rs/polars · error

into_duration not implemented for

Error message

into_duration not implemented for {dt:?}

What it means

Series::into_duration only implements the conversion for Int64-backed data (the physical representation of Duration). Any other input dtype reaches a catch-all `dt =>` arm that panics. The `dtype-duration` feature is on (otherwise error 140 fires instead), but the source series dtype is not convertible.

Solutions

  1. Cast the column to Int64 first: series.cast(&DataType::Int64)?.into_duration(timeunit)
  2. If the column is a Date/Datetime and you want a duration, compute the difference explicitly (e.g. subtracting a baseline) rather than casting directly
  3. Check the source schema (parquet/CSV dtypes) and force Int64 at read time via schema overrides
  4. If the values are floats representing durations, round/truncate to Int64 before conversion

Example fix

// before
let dur = s.into_duration(TimeUnit::Nanoseconds);
// after
let dur = s.cast(&DataType::Int64)?.into_duration(TimeUnit::Nanoseconds);
Defensive patterns

Strategy: validation

Validate before calling

fn can_into_duration(s: &polars::prelude::Series) -> bool {
    matches!(s.dtype(), polars::prelude::DataType::Int64)
}
if !can_into_duration(&s) { s.cast(&DataType::Int64)?; }

Type guard

fn is_i64_series(s: &Series) -> bool { matches!(s.dtype(), DataType::Int64) }

Prevention

When it happens

Trigger: Calling into_duration on a Series whose dtype is not Int64 — e.g. a UInt64, Float64, or Datetime column — typically through a cast to Duration performed by finish_cast.

Common situations: Reading data whose column was inferred as Float or Date and then casting to Duration; converting epoch values stored as floats; schema mismatches after a parquet/CSV read where the column is not Int64.

Related errors


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

Appendix: source

Thrown at crates/polars-core/src/series/mod.rs:1022

        {
            panic!("activate feature dtype-duration")
        }
        #[cfg(feature = "dtype-duration")]
        match self.dtype() {
            DataType::Int64 => self
                .i64()
                .unwrap()
                .clone()
                .into_duration(timeunit)
                .into_series(),
            DataType::Duration(_) => self
                .duration()
                .unwrap()
                .physical()
                .clone()
                .into_duration(timeunit)
                .into_series(),
            dt => panic!("into_duration not implemented for {dt:?}"),
        }
    }

    // used for formatting
    pub fn str_value(&self, index: usize) -> PolarsResult<Cow<'_, str>> {
        Ok(self.0.get(index)?.str_value())
    }
    /// Get the head of the Series.
    pub fn head(&self, length: Option<usize>) -> Series {
        let len = length.unwrap_or(HEAD_DEFAULT_LENGTH);
        self.slice(0, std::cmp::min(len, self.len()))
    }

    /// Get the tail of the Series.
    pub fn tail(&self, length: Option<usize>) -> Series {
        let len = length.unwrap_or(TAIL_DEFAULT_LENGTH);
        let len = std::cmp::min(len, self.len());
        self.slice(-(len as i64), len)

View on GitHub (pinned to fe841f959e)