Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): fix error message when creating DataFrame from 0-dimensional NumPy array #13729

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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