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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
from __future__ import annotations
import pytest
import awkward as ak
def test_one_level_record():
base = ak.zip(
{"x": [1, 2, 3], "y": [8, 9, 10]},
)
assert ak.without_field(base, where=["x"]).to_list() == [
{"y": 8},
{"y": 9},
{"y": 10},
]
assert ak.fields(base) == ["x", "y"]
del base["x"]
assert base.to_list() == [
{"y": 8},
{"y": 9},
{"y": 10},
]
def test_one_level_tuple():
base = ak.zip(
[[1, 2, 3], [8, 9, 10]],
)
assert ak.without_field(base, where=["0"]).to_list() == [
(8,),
(9,),
(10,),
]
assert ak.fields(base) == ["0", "1"]
del base["0"]
assert base.to_list() == [
(8,),
(9,),
(10,),
]
def test_two_level_record():
base = ak.zip(
{"a": ak.zip({"x": [1, 2, 3], "y": [8, 9, 10]}), "b": [1, 2, 3]},
depth_limit=1,
)
assert ak.without_field(base, where=["a", "x"]).to_list() == [
{"b": 1, "a": {"y": 8}},
{"b": 2, "a": {"y": 9}},
{"b": 3, "a": {"y": 10}},
]
assert ak.fields(base) == ["a", "b"]
assert ak.fields(base["a"]) == ["x", "y"]
del base["a", "x"]
assert base.to_list() == [
{"b": 1, "a": {"y": 8}},
{"b": 2, "a": {"y": 9}},
{"b": 3, "a": {"y": 10}},
]
def test_two_level_tuple():
base = ak.zip(
[ak.zip([[1, 2, 3], [8, 9, 10]]), [1, 2, 3]],
depth_limit=1,
)
assert ak.without_field(base, where=["0", "0"]).to_list() == [
((8,), 1),
((9,), 2),
((10,), 3),
]
assert ak.fields(base) == ["0", "1"]
assert ak.fields(base["0"]) == ["0", "1"]
del base["0", "0"]
assert base.to_list() == [
((8,), 1),
((9,), 2),
((10,), 3),
]
def test_two_level_mixed():
base = ak.zip(
[ak.zip({"x": [1, 2, 3], "y": [8, 9, 10]}), [1, 2, 3]],
depth_limit=1,
)
assert ak.without_field(base, where=["0", "x"]).to_list() == [
({"y": 8}, 1),
({"y": 9}, 2),
({"y": 10}, 3),
]
assert ak.fields(base) == ["0", "1"]
assert ak.fields(base["0"]) == ["x", "y"]
del base["0", "x"]
assert base.to_list() == [
({"y": 8}, 1),
({"y": 9}, 2),
({"y": 10}, 3),
]
def test_one_level_delete_the_only_field():
base = ak.zip({"x": [1, 2, 3]})
assert ak.without_field(base, where=["x"]).to_list() == [
{},
{},
{},
]
assert ak.fields(base) == ["x"]
del base["x"]
assert base.to_list() == [
{},
{},
{},
]
def test_two_level_delete_the_only_field():
base = ak.zip({"a": ak.zip({"x": [1, 2, 3]})}, depth_limit=1)
assert ak.without_field(base, where=["a", "x"]).to_list() == [
{"a": {}},
{"a": {}},
{"a": {}},
]
assert ak.fields(base) == ["a"]
del base["a", "x"]
assert base.to_list() == [
{"a": {}},
{"a": {}},
{"a": {}},
]
def test_check_no_fields():
base = ak.zip({"a": ak.zip({"x": [1, 2, 3]})}, depth_limit=1)
with pytest.raises(IndexError, match=r"no field"):
ak.without_field(base, "x")
|