File: test_2185_merge_union_of_records.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (108 lines) | stat: -rw-r--r-- 2,989 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import numpy as np  # noqa: F401

import awkward as ak


def test_merge_union_of_records():
    a1 = ak.Array([{"a": 1, "b": 2}])
    a2 = ak.Array([{"b": 3.3, "c": 4.4}])
    c = ak.concatenate((a1, a2))

    assert c.tolist() == [{"a": 1, "b": 2}, {"b": 3.3, "c": 4.4}]

    assert str(c.type) == "2 * union[{a: int64, b: int64}, {b: float64, c: float64}]"

    d = ak.merge_union_of_records(c)

    assert d.tolist() == [{"a": 1, "b": 2, "c": None}, {"a": None, "b": 3.3, "c": 4.4}]

    assert str(d.type) == "2 * {a: ?int64, b: float64, c: ?float64}"


def test_merge_union_of_records_2():
    a1 = ak.Array([{"a": 1, "b": 2}])
    a2 = ak.Array([{"b": 3.3, "c": 4.4}, {"b": None, "c": None}])
    c = ak.concatenate((a1, a2))

    assert c.tolist() == [
        {"a": 1, "b": 2},
        {"b": 3.3, "c": 4.4},
        {"b": None, "c": None},
    ]

    assert str(c.type) == "3 * union[{a: int64, b: int64}, {b: ?float64, c: ?float64}]"

    d = ak.merge_union_of_records(c)

    assert d.tolist() == [
        {"a": 1, "b": 2, "c": None},
        {"a": None, "b": 3.3, "c": 4.4},
        {"a": None, "b": None, "c": None},
    ]

    assert str(d.type) == "3 * {a: ?int64, b: ?float64, c: ?float64}"


def test_merge_union_of_records_3():
    a1 = ak.Array([[[[{"a": 1, "b": 2}]]]])
    a2 = ak.Array([[[[{"b": 3.3, "c": 4.4}]]]])
    c = ak.concatenate((a1, a2), axis=-1)

    assert c.tolist() == [[[[{"a": 1, "b": 2}, {"b": 3.3, "c": 4.4}]]]]

    assert (
        str(c.type)
        == "1 * var * var * var * union[{a: int64, b: int64}, {b: float64, c: float64}]"
    )

    d = ak.merge_union_of_records(c, axis=-1)

    assert d.tolist() == [
        [[[{"a": 1, "b": 2, "c": None}, {"a": None, "b": 3.3, "c": 4.4}]]]
    ]

    assert str(d.type) == "1 * var * var * var * {a: ?int64, b: float64, c: ?float64}"


def test_merge_option_of_records():
    a = ak.Array([None, {"a": 1, "b": 2}])

    assert str(a.type) == "2 * ?{a: int64, b: int64}"

    b = ak.merge_option_of_records(a)

    assert b.tolist() == [{"a": None, "b": None}, {"a": 1, "b": 2}]

    assert str(b.type) == "2 * {a: ?int64, b: ?int64}"


def test_merge_option_of_records_2():
    a = ak.Array([None, {"a": 1, "b": 2}, {"a": None, "b": None}])

    assert str(a.type) == "3 * ?{a: ?int64, b: ?int64}"

    b = ak.merge_option_of_records(a)

    assert b.tolist() == [
        {"a": None, "b": None},
        {"a": 1, "b": 2},
        {"a": None, "b": None},
    ]

    assert str(b.type) == "3 * {a: ?int64, b: ?int64}"


def test_merge_option_of_records_3():
    a = ak.Array([[[[None, {"a": 1, "b": 2}]]]])

    assert str(a.type) == "1 * var * var * var * ?{a: int64, b: int64}"

    b = ak.merge_option_of_records(a, axis=-1)

    assert b.tolist() == [[[[{"a": None, "b": None}, {"a": 1, "b": 2}]]]]

    assert str(b.type) == "1 * var * var * var * {a: ?int64, b: ?int64}"