pola-rs/polars · error

out-of-range date

Error message

out-of-range date

What it means

`date32_to_date` converts an i32 day count (days since Unix epoch) into a `NaiveDate` by adding `EPOCH_DAYS_FROM_CE`. The underlying `date32_to_date_opt` uses `NaiveDate::from_num_days_from_ce_opt`, which returns `None` when the total days-since-CE value is outside chrono's supported date range; the wrapper then panics with "out-of-range date".

Solutions

  1. Use `date32_to_date_opt(days)` and handle `None` instead of the panicking wrapper.
  2. Range-check the day value before conversion (e.g. keep `EPOCH_DAYS_FROM_CE + days` within chrono's limits).
  3. Scrub sentinel values from the source data before calling the converter.

Example fix

// before
let d = date32_to_date(days); // panics on extreme input
// after
let d = date32_to_date_opt(days)
    .ok_or_else(|| anyhow!("date32 days {days} out of range"))?;
Defensive patterns

Strategy: validation

Validate before calling

fn date32_days_in_range(days: i32) -> bool {
    NaiveDate::from_num_days_from_ce_opt(EPOCH_DAYS_FROM_CE + days).is_some()
}

Try / catch

match date32_to_date_opt(days) {
    Some(d) => /* use d */,
    None => /* treat as corrupt/null value */,
}

Prevention

When it happens

Trigger: Calling `date32_to_date(days)` where `EPOCH_DAYS_FROM_CE + days` exceeds chrono's `NaiveDate` limits (~±262,000 years from year 1 CE), e.g. `days = i32::MAX` or heavily negative values.

Common situations: Sentinel/garbage values in a date32 column; display/formatting paths (`to_string`, `any_value_into_py_object`) encountering unvalidated data; tests using extreme values.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at crates/polars-arrow/src/temporal_conversions.rs:47

}

/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
#[inline]
pub fn date32_to_datetime(v: i32) -> NaiveDateTime {
    date32_to_datetime_opt(v).expect("invalid or out-of-range datetime")
}

/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
#[inline]
pub fn date32_to_datetime_opt(v: i32) -> Option<NaiveDateTime> {
    let delta = TimeDelta::try_days(v.into())?;
    unix_epoch().checked_add_signed(delta)
}

/// converts a `i32` representing a `date32` to [`NaiveDate`]
#[inline]
pub fn date32_to_date(days: i32) -> NaiveDate {
    date32_to_date_opt(days).expect("out-of-range date")
}

/// converts a `i32` representing a `date32` to [`NaiveDate`]
#[inline]
pub fn date32_to_date_opt(days: i32) -> Option<NaiveDate> {
    NaiveDate::from_num_days_from_ce_opt(EPOCH_DAYS_FROM_CE + days)
}

/// converts a `i64` representing a `date64` to [`NaiveDateTime`]
#[inline]
pub fn date64_to_datetime(v: i64) -> NaiveDateTime {
    TimeDelta::try_milliseconds(v)
        .and_then(|delta| unix_epoch().checked_add_signed(delta))
        .expect("invalid or out-of-range datetime")
}

/// converts a `i64` representing a `date64` to [`NaiveDate`]
#[inline]

View on GitHub (pinned to fe841f959e)