diff --git a/crates/polars-core/src/frame/mod.rs b/crates/polars-core/src/frame/mod.rs index 34662cd0a26f..da29d8da070b 100644 --- a/crates/polars-core/src/frame/mod.rs +++ b/crates/polars-core/src/frame/mod.rs @@ -281,7 +281,7 @@ impl DataFrame { polars_ensure!( col.len() == height, ShapeMismatch: "could not create a new DataFrame: series {:?} has length {} while series {:?} has length {}", - columns[0].len(), height, col.name(), col.len() + columns[0].name(), height, col.name(), col.len() ); } diff --git a/py-polars/tests/unit/dataframe/test_df.py b/py-polars/tests/unit/dataframe/test_df.py index c375e1952347..2953d018430f 100644 --- a/py-polars/tests/unit/dataframe/test_df.py +++ b/py-polars/tests/unit/dataframe/test_df.py @@ -23,6 +23,7 @@ DuplicateError, InvalidOperationError, OutOfBoundsError, + ShapeError, ) from polars.testing import ( assert_frame_equal, @@ -2997,3 +2998,11 @@ def test_get_column_index() -> None: with pytest.raises(ColumnNotFoundError, match="missing"): df.get_column_index("missing") + + +def test_dataframe_creation_with_different_series_lengths_19795() -> None: + with pytest.raises( + ShapeError, + match='could not create a new DataFrame: series "a" has length 2 while series "b" has length 1', + ): + pl.DataFrame({"a": [1, 2], "b": [1]})