Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/interop/test_to_init_repr.py
6939 views
1
from __future__ import annotations
2
3
import zoneinfo # noqa: F401
4
from datetime import date, datetime, time, timedelta, timezone
5
6
import pytest
7
8
import polars as pl
9
from polars.testing import assert_frame_equal
10
11
12
@pytest.mark.may_fail_cloud # reason: Object type serialization
13
def test_to_init_repr() -> None:
14
# round-trip various types
15
df = (
16
pl.LazyFrame(
17
{
18
"a": [1, 2, None],
19
"b": [4.5, 5.5, 6.5],
20
"c": ["x", "y", "z"],
21
"d": [True, False, True],
22
"e": [None, "", None],
23
"f": [date(2022, 7, 5), date(2023, 2, 5), date(2023, 8, 5)],
24
"g": [time(0, 0, 0, 1), time(12, 30, 45), time(23, 59, 59, 999000)],
25
"h": [
26
datetime(2022, 7, 5, 10, 30, 45, 4560),
27
datetime(2023, 10, 12, 20, 3, 8, 11),
28
None,
29
],
30
"i": [
31
datetime(2022, 7, 5, 10, 30, 45, 4560),
32
datetime(2023, 10, 12, 20, 3, 8, 11),
33
None,
34
],
35
"null": [None, None, None],
36
"enum": ["a", "b", "c"],
37
"duration": [timedelta(days=1), timedelta(days=2), None],
38
"binary": [bytes([0]), bytes([0, 1]), bytes([0, 1, 2])],
39
"object": [timezone.utc, timezone.utc, timezone.utc],
40
},
41
)
42
.with_columns(
43
pl.col("c").cast(pl.Categorical),
44
pl.col("h").cast(pl.Datetime("ns")),
45
pl.col("i").dt.replace_time_zone("Australia/Melbourne"),
46
pl.col("enum").cast(pl.Enum(["a", "b", "c"])),
47
)
48
.collect()
49
)
50
51
result = eval(df.to_init_repr().replace("datetime.", ""))
52
expected = df
53
# drop "object" because it can not be compared by assert_frame_equal
54
assert_frame_equal(result.drop("object"), expected.drop("object"))
55
56
57
def test_to_init_repr_nested_dtype() -> None:
58
# round-trip nested types
59
df = pl.LazyFrame(
60
{
61
"list": pl.Series(values=[[1], [2], [3]], dtype=pl.List(pl.Int32)),
62
"list_list": pl.Series(
63
values=[[[1]], [[2]], [[3]]], dtype=pl.List(pl.List(pl.Int8))
64
),
65
"array": pl.Series(
66
values=[[1.0], [2.0], [3.0]],
67
dtype=pl.Array(pl.Float32, 1),
68
),
69
"struct": pl.Series(
70
values=[
71
{"x": "foo", "y": [1, 2]},
72
{"x": "bar", "y": [3, 4, 5]},
73
{"x": "foobar", "y": []},
74
],
75
dtype=pl.Struct({"x": pl.String, "y": pl.List(pl.Int8)}),
76
),
77
},
78
).collect()
79
80
assert_frame_equal(eval(df.to_init_repr()), df)
81
82
83
def test_to_init_repr_nested_dtype_roundtrip() -> None:
84
# round-trip nested types
85
df = pl.LazyFrame(
86
{
87
"list": pl.Series(values=[[1], [2], [3]], dtype=pl.List(pl.Int32)),
88
"list_list": pl.Series(
89
values=[[[1]], [[2]], [[3]]], dtype=pl.List(pl.List(pl.Int8))
90
),
91
"array": pl.Series(
92
values=[[1.0], [2.0], [3.0]],
93
dtype=pl.Array(pl.Float32, 1),
94
),
95
"struct": pl.Series(
96
values=[
97
{"x": "foo", "y": [1, 2]},
98
{"x": "bar", "y": [3, 4, 5]},
99
{"x": "foobar", "y": []},
100
],
101
dtype=pl.Struct({"x": pl.String, "y": pl.List(pl.Int8)}),
102
),
103
},
104
).collect()
105
106
assert_frame_equal(eval(df.to_init_repr()), df)
107
108