Skip to content

Commit

Permalink
fix(python): fix error message when creating DataFrame from 0-dimensi…
Browse files Browse the repository at this point in the history
…onal NumPy array (#13729)

Co-authored-by: Wainberg <m.wainberg@utoronto.ca>
  • Loading branch information
Wainberg and Wainberg committed Jan 15, 2024
1 parent f21e5c7 commit 9286a85
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion py-polars/polars/utils/_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,10 @@ def numpy_to_pydf(
msg = f"`orient` must be one of {{'col', 'row', None}}, got {orient!r}"
raise ValueError(msg)
else:
msg = f"cannot create DataFrame from array with more than two dimensions; shape = {shape}"
if shape == ():
msg = "cannot create DataFrame from zero-dimensional array"
else:
msg = f"cannot create DataFrame from array with more than two dimensions; shape = {shape}"
raise ValueError(msg)

if schema is not None and len(schema) != n_columns:
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/interop/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ def test_from_numpy() -> None:
assert df.shape == (3, 2)
assert df.rows() == [(1, 4), (2, 5), (3, 6)]
assert df.schema == {"a": pl.UInt32, "b": pl.UInt32}
with pytest.raises(
ValueError,
match="cannot create DataFrame from array with more than two dimensions",
):
_ = pl.from_numpy(np.array([[[1]]]))
with pytest.raises(
ValueError, match="cannot create DataFrame from zero-dimensional array"
):
_ = pl.from_numpy(np.array(1))


def test_from_numpy_structured() -> None:
Expand Down

0 comments on commit 9286a85

Please sign in to comment.