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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
from __future__ import annotations
import numpy as np
import pytest
import awkward as ak
to_list = ak.operations.to_list
def test_singletons():
array = ak.Array([1.1, 2.2, None, 3.3, None, None, 4.4, 5.5])
assert to_list(ak.operations.singletons(array)) == [
[1.1],
[2.2],
[],
[3.3],
[],
[],
[4.4],
[5.5],
]
assert to_list(
ak.operations.singletons(ak.Array([1.1, 2.2, None, 3.3, None, None, 4.4, 5.5]))
) == [[1.1], [2.2], [], [3.3], [], [], [4.4], [5.5]]
assert to_list(
ak.operations.singletons(
ak.Array([[1.1, 2.2, None], [3.3, None], [None], [4.4, 5.5]]), axis=1
)
) == [[[1.1], [2.2], []], [[3.3], []], [[]], [[4.4], [5.5]]]
assert to_list(
ak.operations.singletons(
ak.Array([[[1.1, 2.2, None]], [[3.3, None]], [[None]], [[4.4, 5.5]]]),
axis=2,
)
) == [[[[1.1], [2.2], []]], [[[3.3], []]], [[[]]], [[[4.4], [5.5]]]]
def test_firsts():
assert to_list(
ak.operations.firsts(
ak.operations.singletons(
ak.Array([1.1, 2.2, None, 3.3, None, None, 4.4, 5.5])
),
axis=1,
)
) == [1.1, 2.2, None, 3.3, None, None, 4.4, 5.5]
assert to_list(
ak.operations.firsts(
ak.operations.singletons(
ak.Array([[1.1, 2.2, None], [3.3, None], [None], [4.4, 5.5]]), axis=1
),
axis=2,
)
) == [[1.1, 2.2, None], [3.3, None], [None], [4.4, 5.5]]
assert to_list(
ak.operations.firsts(
ak.operations.singletons(
ak.Array([[[1.1, 2.2, None]], [[3.3, None]], [[None]], [[4.4, 5.5]]]),
axis=2,
),
axis=3,
)
) == [[[1.1, 2.2, None]], [[3.3, None]], [[None]], [[4.4, 5.5]]]
def test_allow_missing():
array = ak.Array([1.1, 2.2, None, 3.3, None, None, 4.4, 5.5])
ak.operations.to_numpy(array)
with pytest.raises(ValueError):
ak.operations.to_numpy(array, allow_missing=False)
def test_flatten0():
array = ak.Array([1.1, 2.2, None, 3.3, None, None, 4.4, 5.5])
assert to_list(ak.operations.flatten(array, axis=0)) == [
1.1,
2.2,
3.3,
4.4,
5.5,
]
content0 = ak.operations.from_iter(
[1.1, 2.2, None, 3.3, None, None, 4.4, 5.5], highlevel=False
)
content1 = ak.operations.from_iter(
["one", None, "two", None, "three"], highlevel=False
)
array = ak.Array(
ak.contents.UnionArray(
ak.index.Index8(
np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=np.int8)
),
ak.index.Index64(
np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 4, 7], dtype=np.int64)
),
[content0, content1],
)
)
assert to_list(array) == [
1.1,
"one",
2.2,
None,
None,
"two",
3.3,
None,
None,
None,
4.4,
"three",
5.5,
]
assert to_list(ak.operations.flatten(array, axis=0)) == [
1.1,
"one",
2.2,
"two",
3.3,
4.4,
"three",
5.5,
]
|