Skip to content

Commit

Permalink
fix: Misleading ShapeError error message on dataframe creation (#19901
Browse files Browse the repository at this point in the history
)
  • Loading branch information
barak1412 authored Nov 21, 2024
1 parent 927b7b8 commit 3925085
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
DuplicateError,
InvalidOperationError,
OutOfBoundsError,
ShapeError,
)
from polars.testing import (
assert_frame_equal,
Expand Down Expand Up @@ -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]})

0 comments on commit 3925085

Please sign in to comment.