Skip to content

Commit

Permalink
feat(python): Only allow inputs of type Sequence in from_records (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Mar 27, 2024
1 parent 03c5f73 commit 1a37f2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions py-polars/polars/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ def from_records(
│ 3 ┆ 6 │
└─────┴─────┘
"""
if not isinstance(data, Sequence):
msg = (
f"expected data of type Sequence, got {type(data).__name__!r}"
"\n\nHint: Try passing your data to the DataFrame constructor instead,"
" e.g. `pl.DataFrame(data)`."
)
raise TypeError(msg)

return wrap_df(
sequence_to_pydf(
data,
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/unit/interop/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,20 @@ def test_from_records() -> None:
assert df.rows() == [(1, 4), (2, 5), (3, 6)]


# https://github.com/pola-rs/polars/issues/15195
@pytest.mark.parametrize(
"input",
[
pl.Series([1, 2]),
pl.Series([{"a": 1, "b": 2}]),
pl.DataFrame({"a": [1, 2], "b": [3, 4]}),
],
)
def test_from_records_non_sequence_input(input: Any) -> None:
with pytest.raises(TypeError, match="expected data of type Sequence"):
pl.from_records(input)


def test_from_arrow() -> None:
data = pa.table({"a": [1, 2, 3], "b": [4, 5, 6]})
df = pl.from_arrow(data)
Expand Down

0 comments on commit 1a37f2b

Please sign in to comment.