Skip to content

Commit

Permalink
fix(python): address DataFrame construction error with lists of `nump…
Browse files Browse the repository at this point in the history
…y` arrays (#11905)
  • Loading branch information
alexander-beedie authored Oct 21, 2023
1 parent a75eaca commit 96b465e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion py-polars/polars/utils/_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,11 @@ def _sequence_of_sequence_to_pydf(
local_schema_override = (
include_unknowns(schema_overrides, column_names) if schema_overrides else {}
)
if column_names and first_element and len(first_element) != len(column_names):
if (
column_names
and len(first_element) > 0
and len(first_element) != len(column_names)
):
raise ShapeError("the row data does not match the number of columns")

unpack_nested = False
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/unit/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ def test_init_ndarray(monkeypatch: Any) -> None:
assert df.shape == (2, 1)
assert df.rows() == [([0, 1, 2, 3, 4],), ([5, 6, 7, 8, 9],)]

test_rows = [(1, 2), (3, 4)]
df = pl.DataFrame([np.array(test_rows[0]), np.array(test_rows[1])], orient="row")
assert_frame_equal(df, pl.DataFrame(test_rows, orient="row"))

# numpy arrays containing NaN
df0 = pl.DataFrame(
data={"x": [1.0, 2.5, float("nan")], "y": [4.0, float("nan"), 6.5]},
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_string_numeric_comp_err() -> None:
def test_panic_error() -> None:
with pytest.raises(
pl.PolarsPanicError,
match="""dimensions cannot be empty""",
match="dimensions cannot be empty",
):
pl.Series("a", [1, 2, 3]).reshape(())

Expand Down

0 comments on commit 96b465e

Please sign in to comment.