pola-rs/polars · error

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

Error message

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

What it means

Series::from_physical_unchecked matches (from_dtype, to_dtype) pairs; the catch-all arm returns this PolarsError when no conversion from the series' physical representation to the requested target dtype is implemented (e.g. an unhandled physical/target pair). Means the requested from_physical cast is not a supported mapping in polars-core.

Source

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

                self.struct_()
                    .unwrap()
                    .from_physical_unchecked(to.as_slice())
                    .map(|ca| ca.into_series())
            },

            #[cfg(feature = "dtype-map")]
            (D::List(_), D::Map(_, _)) => {
                let storage = self.from_physical_unchecked(&dtype.map_storage_dtype().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 68506541d2)

Solutions

  1. Provide a physical representation that is valid for the logical dtype (e.g. Int32 for Date32).
  2. Cast the physical array to the expected physical type before calling from_physical.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: This panic/expect fires when execution reaches an unguarded state described by: "invalid from_physical({dtype:?}) for {:?}". Typical triggers: unsupported dtype or feature-gated code path reached at runtime, invalid user input or environment variable value, calling API methods in the wrong order or on mismatched types, or data (lengths, offsets, ranges) violating the function's preconditions.

Common situations: Encountered when input data or configuration does not meet the preconditions of the throwing code path ("invalid from_physical({dtype:?}) for {:?}"). Common cases: missing Cargo feature flags, mismatched dtypes/lengths between arrays or series, out-of-range temporal values, malformed environment variables, or operations on unsupported/complex nested types.


AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19). Data as JSON: /api/errors/75435f8debbe0cc4. Report an issue: GitHub.