-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_polars.py
99 lines (79 loc) · 2.38 KB
/
test_polars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
import pytest
pl = pytest.importorskip("polars")
pytest.importorskip("akimbo.polars")
def test_simple():
s = pl.Series([[1, 2, 3], [], [4, 5]])
s2 = s.ak[:, -1:]
assert s2.to_list() == [[3], [], [5]]
def test_apply():
s = pl.Series([[1, 2, 3], [], [4, 5]])
s2 = s.ak.apply(np.negative)
assert s2.to_list() == [[-1, -2, -3], [], [-4, -5]]
def test_apply_where():
data = [
{"a": [1, 2, 3], "b": [1, 2, 3]},
{"a": [1, 2, 3], "b": [1, 2, 3]},
{"a": [1, 2, 3], "b": [1, 2, 3]},
]
s = pl.Series(data)
s2 = s.ak.apply(np.negative, where="a")
assert s2[0] == {"a": [-1, -2, -3], "b": [1, 2, 3]}
def test_pack_unpack():
data = [
{"a": [1, 2, 3], "b": [1, 2, 3]},
{"a": [1, 2, 3], "b": [1, 2, 3]},
{"a": [1, 2, 3], "b": [1, 2, 3]},
]
s = pl.Series(data)
df = s.ak.unpack()
assert df["a"].to_list() == [[1, 2, 3]] * 3
s2 = df.ak.pack()
assert s.to_list() == s2.to_list()
def test_operator():
s = pl.Series([[1, 2, 3], [], [4, 5]])
s2 = s.ak + 1
assert s2.to_list() == [[2, 3, 4], [], [5, 6]]
def test_ufunc():
s = pl.Series([[1, 2, 3], [], [4, 5]])
s2 = np.negative(s.ak)
assert s2.to_list() == [[-1, -2, -3], [], [-4, -5]]
s2 = np.add(s.ak, 1)
assert s2.to_list() == [[2, 3, 4], [], [5, 6]]
df = pl.DataFrame({"a": s})
df2 = df.ak + 1
assert df2["a"].to_list() == [[2, 3, 4], [], [5, 6]]
def test_unexplode():
df = pl.DataFrame(
{
"x": [1, 1, 1, 2, 1, 3, 3, 1],
"y": [1, 1, 1, 2, 1, 3, 3, 1],
"z": [1, 1, 1, 2, 1, 3, 3, 2],
}
)
out = df.ak.unexplode("x")
compact = out["grouped"].to_list()
expected = [
[
{"y": 1, "z": 1},
{"y": 1, "z": 1},
{"y": 1, "z": 1},
{"y": 1, "z": 1},
{"y": 1, "z": 2},
],
[{"y": 2, "z": 2}],
[{"y": 3, "z": 3}, {"y": 3, "z": 3}],
]
assert compact == expected
out = df.ak.unexplode("x", "y")
compact = out["grouped"].to_list()
expected = [
[{"z": 1}, {"z": 1}, {"z": 1}, {"z": 1}, {"z": 2}],
[{"z": 2}],
[{"z": 3}, {"z": 3}],
]
assert compact == expected
with pytest.raises(ValueError):
df.ak.unexplode("x", "y", "z")
with pytest.raises(ValueError):
df.ak.unexplode("unknown")