Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/test_convert.py
6939 views
1
from __future__ import annotations
2
3
import pytest
4
5
import polars as pl
6
from polars.exceptions import ComputeError, NoDataError
7
8
9
def test_from_records_schema_inference() -> None:
10
data = [[1, 2.1, 3], [4, 5, 6.4]]
11
12
with pytest.raises(TypeError, match="unexpected value"):
13
pl.from_records(data)
14
15
result = pl.from_records(data, strict=False)
16
assert result.to_dict(as_series=False) == {
17
"column_0": [1.0, 2.1, 3.0],
18
"column_1": [4.0, 5.0, 6.4],
19
}
20
21
22
def test_from_dicts_schema_inference() -> None:
23
data = [{"a": 1, "b": 2}, {"a": 3.1, "b": 4.5}]
24
result = pl.from_dicts(data) # type: ignore[arg-type]
25
assert result.to_dict(as_series=False) == {
26
"a": [1.0, 3.1],
27
"b": [2.0, 4.5],
28
}
29
30
31
def test_from_dicts_nested_nulls() -> None:
32
result = pl.from_dicts([{"a": [None, None]}, {"a": [1, 2]}])
33
assert result.to_dict(as_series=False) == {"a": [[None, None], [1, 2]]}
34
35
36
def test_from_dicts_empty() -> None:
37
with pytest.raises(NoDataError, match="no data, cannot infer schema"):
38
pl.from_dicts([])
39
40
41
def test_from_dicts_all_cols_6716() -> None:
42
dicts = [{"a": None} for _ in range(20)] + [{"a": "crash"}]
43
44
with pytest.raises(
45
ComputeError, match="make sure that all rows have the same schema"
46
):
47
pl.from_dicts(dicts, infer_schema_length=20)
48
assert pl.from_dicts(dicts, infer_schema_length=None).dtypes == [pl.String]
49
50
51
def test_dict_float_string_roundtrip_18882() -> None:
52
assert pl.from_dicts([{"A": "0.1"}]).to_dicts() == [{"A": "0.1"}]
53
54