pola-rs/polars · error

invalid from_physical

Error message

invalid from_physical({dtype:?}) for {:?}

What it means

Series::from_physical_unchecked panics when asked to reinterpret a series' physical representation into a logical dtype that the physical backing data does not support. It only handles the concrete (physical, logical) mappings it knows; anything else hits the catch-all panic.

Solutions

  1. Only call from_physical_unchecked with the exact physical dtype expected by the target logical type
  2. Prefer the safe from_physical / cast_with_options API which returns PolarsResult instead of panicking
  3. Verify self.dtype() before conversion and route unsupported dtypes through cast instead

Example fix

// before
let s = series.from_physical_unchecked(&DataType::Datetime(TimeUnit::Ns, None))?;
// after
let s = series.cast_with_options(&DataType::Datetime(TimeUnit::Ns, None), CastOptions::NonStrict)?;
Defensive patterns

Strategy: try-catch

Validate before calling

if !matches!(series.dtype(), DataType::Int32 | DataType::Int64 | DataType::Float64) { /* route to cast instead */ }

Type guard

fn is_physical(dtype: &DataType) -> bool { matches!(dtype, DataType::Int32 | DataType::Int64 | DataType::Float32 | DataType::Float64) }

Try / catch

// prefer safe API
let s: PolarsResult<Series> = series.cast_with_options(&target_dtype, CastOptions::NonStrict);

Prevention

When it happens

Trigger: Calling from_physical_unchecked / try_from_physical_rec (or the internal callers restore_logical, repeat_by, scatter) with a dtype parameter that is not the physical counterpart of the series' actual dtype, e.g. converting Int64 physical to a datetime variant it cannot map.

Common situations: Hand-rolling dtype restoration after reading raw arrow data; custom extension-type storage dtype mismatches; version drift where a logical type gained new physical representations.

Related errors


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

Appendix: source

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

            #[cfg(feature = "dtype-map")]
            (D::List(_), D::Map(_, _)) => {
                use crate::chunked_array::logical::ensure_live_entries_non_null;

                let storage = self.from_physical_unchecked(&dtype.map_storage_dtype().unwrap())?;
                // Nulls that propagation left under null rows are legal; only live rows
                // must own no null entry or key.
                ensure_live_entries_non_null(storage.list().unwrap())?;
                Ok(MapChunked::from_storage_unchecked(dtype.clone(), storage).into_series())
            },
            #[cfg(feature = "dtype-extension")]
            (_, D::Extension(typ, storage)) => {
                let storage_series = self.from_physical_unchecked(storage.as_ref())?;
                let ext = ExtensionChunked::from_storage(typ.clone(), storage_series);
                Ok(ext.into_series())
            },

            _ => panic!("invalid from_physical({dtype:?}) for {:?}", self.dtype()),
        }
    }

    #[cfg(feature = "dtype-extension")]
    pub fn into_extension(self, typ: ExtensionTypeInstance) -> Series {
        assert!(!self.dtype().is_extension());
        let ext = ExtensionChunked::from_storage(typ, self);
        ext.into_series()
    }

    /// Cast numerical types to f64, and keep floats as is.
    pub fn to_float(&self) -> PolarsResult<Series> {
        match self.dtype() {
            DataType::Float32 | DataType::Float64 => Ok(self.clone()),
            _ => self.cast_with_options(&DataType::Float64, CastOptions::Overflowing),
        }
    }

View on GitHub (pinned to fe841f959e)