pola-rs/polars · error

only supported for scalars

Error message

only supported for scalars

What it means

During horizontal concatenation, when unit_length_as_scalar is set, polars expands height-1 frames to the output height by resizing each column. This branch only works on Column::Scalar variants; if a height-1 frame carries a materialized (non-scalar) column, the invariant is violated and it panics 'only supported for scalars'.

Source

Thrown at crates/polars-core/src/frame/horizontal.rs:124

            );
        }
        out_width = 0;

        owned_df = dfs
            .iter()
            .filter(|df| df.shape() != (0, 0))
            .cloned()
            .map(|mut df| {
                out_width += df.width();
                let h = df.height();

                if h != output_height {
                    if unit_length_as_scalar && h == 1 {
                        // SAFETY: We extend each scalar column length to
                        // `output_height`. Then, we set the height of the resulting dataframe.
                        unsafe { df.columns_mut() }.iter_mut().for_each(|c| {
                            let Column::Scalar(s) = c else {
                                panic!("only supported for scalars");
                            };

                            *c = Column::Scalar(s.resize(output_height));
                        });
                    } else {
                        let diff = output_height - h;

                        // SAFETY: We extend each column with nulls to the point of being of length
                        // `output_height`. Then, we set the height of the resulting dataframe.
                        unsafe { df.columns_mut() }.iter_mut().for_each(|c| {
                            *c = c.extend_constant(AnyValue::Null, diff).unwrap();
                        });
                    }
                    unsafe {
                        df.set_height(output_height);
                    }
                }

View on GitHub (pinned to 9b5d73fd00)

Solutions

  1. Upgrade polars to the latest patch release; this class of invariant break gets fixed upstream
  2. Work around it: materialize the height-1 frames fully (broadcast to output height yourself) before the horizontal concat
  3. If reproducible, file a polars issue with the minimal example and version information

Example fix

# before
pl.concat([df_wide, df_one_row], how="horizontal")

# after
# broadcast the height-1 frame to the target height first
height = df_wide.height
df_one_row = df_one_row.select(pl.all().repeat_by(height).explode())
pl.concat([df_wide, df_one_row], how="horizontal")
Defensive patterns

Strategy: fallback

Validate before calling

fn all_height1_are_scalar(df: &DataFrame) -> bool {
    df.height() == 1 && df.get_columns().iter().all(|c| matches!(c, Column::Scalar(_)))
}

Prevention

When it happens

Trigger: horizontally_concatenate (used by concat how="horizontal"/hstack style paths) where a DataFrame of height 1 contains materialized columns instead of scalar columns while scalar expansion is requested.

Common situations: Internal invariant break after operations that materialize scalar columns (e.g., certain casts, with_columns, or IPC round trips) before a horizontal concat; usually a polars bug rather than user error.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/98285ca7caf21891. Report an issue: GitHub.