Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/series/test_append.py
6939 views
1
import io
2
3
import pytest
4
5
import polars as pl
6
from polars.exceptions import SchemaError
7
from polars.testing import assert_series_equal
8
9
10
def test_append() -> None:
11
a = pl.Series("a", [1, 2])
12
b = pl.Series("b", [8, 9, None])
13
14
result = a.append(b)
15
16
expected = pl.Series("a", [1, 2, 8, 9, None])
17
assert_series_equal(a, expected)
18
assert_series_equal(result, expected)
19
assert a.n_chunks() == 2
20
21
22
def test_append_self_3915() -> None:
23
a = pl.Series("a", [1, 2])
24
25
a.append(a)
26
27
expected = pl.Series("a", [1, 2, 1, 2])
28
assert_series_equal(a, expected)
29
assert a.n_chunks() == 2
30
31
32
def test_append_bad_input() -> None:
33
a = pl.Series("a", [1, 2])
34
b = a.to_frame()
35
36
with pytest.raises(
37
TypeError,
38
match="expected `other` .*to be a 'Series'.* not 'DataFrame'",
39
):
40
a.append(b) # type: ignore[arg-type]
41
42
with pytest.raises(
43
TypeError,
44
match="expected `other` .*to be a 'Series'.* not 'LazyFrame'",
45
):
46
a.append(b.lazy()) # type: ignore[arg-type]
47
48
49
def test_struct_schema_on_append_extend_3452() -> None:
50
housing1_data = [
51
{
52
"city": "Chicago",
53
"address": "100 Main St",
54
"price": 250000,
55
"nbr_bedrooms": 3,
56
},
57
{
58
"city": "New York",
59
"address": "100 First Ave",
60
"price": 450000,
61
"nbr_bedrooms": 2,
62
},
63
]
64
65
housing2_data = [
66
{
67
"address": "303 Mockingbird Lane",
68
"city": "Los Angeles",
69
"nbr_bedrooms": 2,
70
"price": 450000,
71
},
72
{
73
"address": "404 Moldave Dr",
74
"city": "Miami Beach",
75
"nbr_bedrooms": 1,
76
"price": 250000,
77
},
78
]
79
housing1, housing2 = pl.Series(housing1_data), pl.Series(housing2_data)
80
with pytest.raises(
81
SchemaError,
82
):
83
housing1.append(housing2)
84
85
with pytest.raises(
86
SchemaError,
87
):
88
housing1.extend(housing2)
89
90
91
def test_append_mismatching_struct_with_null_22639() -> None:
92
a = pl.Series([{"x": "foo", "y": "bar"}])
93
b = pl.Series([{"z": "baz"}])
94
c = pl.Series([{"z": None}])
95
with pytest.raises(
96
SchemaError,
97
):
98
a.append(b)
99
with pytest.raises(
100
SchemaError,
101
):
102
a.append(c)
103
104
assert_series_equal(b.append(c), pl.Series([{"z": "baz"}, {"z": None}]))
105
106
107
def test_append_null_series() -> None:
108
a = pl.Series("a", [1, 2], pl.Int64)
109
b = pl.Series("b", [None, None], pl.Null)
110
111
result = a.append(b)
112
113
expected = pl.Series("a", [1, 2, None, None], pl.Int64)
114
assert_series_equal(a, expected)
115
assert_series_equal(result, expected)
116
assert a.n_chunks() == 2
117
118
119
def test_append_enum_22764() -> None:
120
f = io.BytesIO()
121
g = io.BytesIO()
122
pl.DataFrame({"someletter": ["A", "B"]}).write_csv(f)
123
124
schema = pl.Schema(
125
{
126
"someletter": pl.Enum(["A", "B"]),
127
}
128
)
129
pl.scan_csv(f, schema=schema).sink_parquet(g)
130
131