File: test_0898_unzip_heterogeneous_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 (61 lines) | stat: -rw-r--r-- 1,783 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
# 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 pytest

import awkward as ak

to_list = ak.operations.to_list


def test():
    a = ak.Array([{"this": 100}])
    b = ak.Array([{"this": 90, "that": 100}])
    c = ak.operations.concatenate((a, b))

    with pytest.raises(ValueError):
        ak.operations.unzip(c)

    a = ak.Array([{"this": 100}])
    b = ak.Array([{"this": 90}])
    c = ak.operations.concatenate((a, b))

    (tmp,) = ak.operations.unzip(c)

    assert tmp.to_list() == [100, 90]


def test_zip_and_unzip():
    x = ak.Array([[1, 2, 3], [], [4, 5], [6], [7, 8, 9, 10]])
    y = ak.Array([1.1, 2.2, 3.3, 4.4, 5.5])

    one = ak.operations.zip({"x": x, "y": y})
    two = ak.operations.zip({"x": x, "y": y}, depth_limit=1)
    xx, yy = ak.operations.unzip(two)
    assert isinstance(one.layout, ak.contents.Content)
    assert isinstance(two.layout, ak.contents.Content)
    assert isinstance(xx.layout, ak.contents.Content)
    assert isinstance(yy.layout, ak.contents.Content)
    assert to_list(one) == [
        [{"x": 1, "y": 1.1}, {"x": 2, "y": 1.1}, {"x": 3, "y": 1.1}],
        [],
        [{"x": 4, "y": 3.3}, {"x": 5, "y": 3.3}],
        [{"x": 6, "y": 4.4}],
        [
            {"x": 7, "y": 5.5},
            {"x": 8, "y": 5.5},
            {"x": 9, "y": 5.5},
            {"x": 10, "y": 5.5},
        ],
    ]
    assert to_list(two) == [
        {"x": [1, 2, 3], "y": 1.1},
        {"x": [], "y": 2.2},
        {"x": [4, 5], "y": 3.3},
        {"x": [6], "y": 4.4},
        {"x": [7, 8, 9, 10], "y": 5.5},
    ]
    assert to_list(xx) == [[1, 2, 3], [], [4, 5], [6], [7, 8, 9, 10]]
    assert to_list(yy) == [1.1, 2.2, 3.3, 4.4, 5.5]