{"record":{"id":"040bb505d15f0329","repo":"pola-rs/polars","slug":"invalid-or-out-of-range-datetime","errorCode":null,"errorMessage":"invalid or out-of-range datetime","messagePattern":"invalid or out-of-range datetime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/temporal_conversions.rs","lineNumber":34,"sourceCode":"pub const NANOSECONDS: i64 = 1_000_000_000;\n/// Number of milliseconds in a day\npub const MILLISECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MILLISECONDS;\n/// Number of microseconds in a day\npub const MICROSECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MICROSECONDS;\n/// Number of nanoseconds in a day\npub const NANOSECONDS_IN_DAY: i64 = SECONDS_IN_DAY * NANOSECONDS;\n/// Number of days between 0001-01-01 and 1970-01-01\npub const EPOCH_DAYS_FROM_CE: i32 = 719_163;\n\n#[inline]\nfn unix_epoch() -> NaiveDateTime {\n    DateTime::UNIX_EPOCH.naive_utc()\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDateTime`]\n#[inline]\npub fn date32_to_datetime(v: i32) -> NaiveDateTime {\n    date32_to_datetime_opt(v).expect(\"invalid or out-of-range datetime\")\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDateTime`]\n#[inline]\npub fn date32_to_datetime_opt(v: i32) -> Option<NaiveDateTime> {\n    let delta = TimeDelta::try_days(v.into())?;\n    unix_epoch().checked_add_signed(delta)\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDate`]\n#[inline]\npub fn date32_to_date(days: i32) -> NaiveDate {\n    date32_to_date_opt(days).expect(\"out-of-range date\")\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDate`]\n#[inline]\npub fn date32_to_date_opt(days: i32) -> Option<NaiveDate> {","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/crates/polars-arrow/src/temporal_conversions.rs#L16-L52","documentation":"Panics inside date32_to_datetime when an i32 date32 value (days since 1970-01-01) cannot become a chrono NaiveDateTime: TimeDelta::try_days fails or unix_epoch().checked_add_signed(delta) overflows chrono's representable range (roughly years -262144..262143). i32 extremes span about +/-5.8 million days, far outside that window, so extreme or sentinel values panic instead of producing null.","triggerScenarios":"Calling date32_to_datetime (directly or via Date-to-Datetime temporal kernels) on data containing i32::MIN, i32::MAX, or garbage - e.g. millisecond epochs mistakenly stored in a date32 column.","commonSituations":"Unit mismatches between producer and consumer (epoch-ms written into a Date column); corrupt binary or misparsed CSVs interpreted as dates; arrow data from other systems with uninitialized values.","solutions":["Use the fallible sibling date32_to_datetime_opt(v) and map None to null or skip the row","Sanitize the column first: null out or clip values outside chrono's date range","Fix the upstream unit/type mismatch before converting","Prefer high-level Polars casts (strict=False) which produce nulls instead of panicking"],"exampleFix":"// before\nlet dt = date32_to_datetime(days); // panics on i32::MIN/MAX\n\n// after\nlet dt = match date32_to_datetime_opt(days) {\n    Some(dt) => dt,\n    None => continue, // or insert a null\n};","handlingStrategy":"validation","validationCode":"use polars_arrow::temporal_conversions::date32_to_datetime_opt;\n\nlet valid: Vec<i32> = days.iter()\n    .copied()\n    .filter(|&d| date32_to_datetime_opt(d).is_some())\n    .collect();","typeGuard":"fn date32_representable(days: i32) -> bool {\n    polars_arrow::temporal_conversions::date32_to_datetime_opt(days).is_some()\n}","tryCatchPattern":null,"preventionTips":["Prefer the _opt conversion variants in any code touching untrusted temporal data","Null out sentinel values (i32::MIN/MAX) before Date -> Datetime conversions","Verify producer/consumer agree on date32 units (days, not ms)","Keep conversion panics out of request handlers by validating batches at ingestion"],"tags":["rust","polars","arrow","temporal","date","panic"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}