pola-rs/polars · error

expected index type found

Error message

expected index type found {self:?}

What it means

AnyValue::idx() extracts the index-sized physical value (IdxSize = u32, or u64 with the 'bigidx' feature). If the AnyValue holds any other variant the code panics, because idx() is only meaningful for values that are actually row indices.

Solutions

  1. Match the AnyValue yourself and convert with as_idx-style logic handling both UInt32 and UInt64 variants.
  2. Ensure the bigidx feature setting is consistent between polars-core and your code / dependent crates (cargo tree -f for polars-core builds).
  3. Use av.extract::<IdxSize>() or downcast the series to IdxCa instead of idx().
  4. Guard with a check that the value is one of the accepted UInt variants before calling idx().

Example fix

// before
let idx = av.idx(); // panics for UInt64 when bigidx is off
// after
let idx = match av {
    AnyValue::UInt32(v) => v as IdxSize,
    AnyValue::UInt64(v) => IdxSize::try_from(v).expect("index overflow"),
    _ => return Err(PolarsError::ComputeError("not an index value".into())),
};
Defensive patterns

Strategy: type-guard

Validate before calling

fn to_idx(av: &AnyValue) -> Option<IdxSize> {
    match av {
        AnyValue::UInt32(v) => Some(*v),
        AnyValue::UInt64(v) => IdxSize::try_from(*v).ok(),
        _ => None,
    }
}

Type guard

let idx = to_idx(&av).ok_or_else(|| PolarsError::ComputeError("not an index".into()))?;

Prevention

When it happens

Trigger: Calling av.idx() on an AnyValue that is Int32/Int64/UInt64 (when bigidx is off) or any non-integer variant; reading row-index values from a series compiled with a different bigidx setting than your code expects.

Common situations: Generic code assuming idx-typed values are u32 while the crate was built with bigidx (or vice versa); calling idx() on values extracted from arbitrary columns.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at crates/polars-core/src/datatypes/any_value.rs:677

        self.strict_cast(dtype).ok_or_else(
            || polars_err!(ComputeError: "cannot cast any-value {:?} to dtype '{}'", self, dtype),
        )
    }

    pub fn cast(&self, dtype: &'a DataType) -> AnyValue<'a> {
        match self.strict_cast(dtype) {
            Some(av) => av,
            None => AnyValue::Null,
        }
    }

    pub fn idx(&self) -> IdxSize {
        match self {
            #[cfg(not(feature = "bigidx"))]
            Self::UInt32(v) => *v,
            #[cfg(feature = "bigidx")]
            Self::UInt64(v) => *v,
            _ => panic!("expected index type found {self:?}"),
        }
    }

    pub fn str_value(&self) -> Cow<'a, str> {
        match self {
            Self::String(s) => Cow::Borrowed(s),
            Self::StringOwned(s) => Cow::Owned(s.to_string()),
            Self::Null => Cow::Borrowed("null"),
            #[cfg(feature = "dtype-categorical")]
            Self::Categorical(cat, map) | Self::Enum(cat, map) => {
                Cow::Borrowed(unsafe { map.cat_to_str_unchecked(*cat) })
            },
            #[cfg(feature = "dtype-categorical")]
            Self::CategoricalOwned(cat, map) | Self::EnumOwned(cat, map) => {
                Cow::Owned(unsafe { map.cat_to_str_unchecked(*cat) }.to_owned())
            },
            av => Cow::Owned(av.to_string()),
        }

View on GitHub (pinned to fe841f959e)