Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ak.unzip and testing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d036ab1
commit 249684d
Showing
2 changed files
with
118 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE | ||
|
||
|
||
import pytest # noqa: F401 | ||
import numpy as np # noqa: F401 | ||
import awkward as ak # noqa: F401 | ||
|
||
to_list = ak._v2.operations.convert.to_list | ||
|
||
|
||
def test(): | ||
a = ak._v2.Array([{"this": 100}]) | ||
b = ak._v2.Array([{"this": 90, "that": 100}]) | ||
c = ak._v2.operations.structure.concatenate((a, b)) | ||
|
||
with pytest.raises(ValueError): | ||
ak._v2.operations.structure.unzip(c) | ||
|
||
a = ak._v2.Array([{"this": 100}]) | ||
b = ak._v2.Array([{"this": 90}]) | ||
c = ak._v2.operations.structure.concatenate((a, b)) | ||
|
||
(tmp,) = ak._v2.operations.structure.unzip(c) | ||
|
||
assert tmp.tolist() == [100, 90] | ||
|
||
|
||
def test_zip_and_unzip(): | ||
x = ak._v2.Array([[1, 2, 3], [], [4, 5], [6], [7, 8, 9, 10]]) | ||
y = ak._v2.Array([1.1, 2.2, 3.3, 4.4, 5.5]) | ||
|
||
one = ak._v2.operations.structure.zip({"x": x, "y": y}) | ||
two = ak._v2.operations.structure.zip({"x": x, "y": y}, depth_limit=1) | ||
xx, yy = ak._v2.operations.structure.unzip(two) | ||
assert isinstance(one.layout, ak._v2.contents.Content) | ||
assert isinstance(two.layout, ak._v2.contents.Content) | ||
assert isinstance(xx.layout, ak._v2.contents.Content) | ||
assert isinstance(yy.layout, ak._v2.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] |