Skip to content

Commit 234027e

Browse files
authored
test: add a test on flattering empty arrays (#3700)
* Add a test on flattering empty arrays * Update a comment
1 parent 672a1e2 commit 234027e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/test_2377_empty_index.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,21 @@ def test_empty_index(a: ak.Array, idx: Iterable) -> None:
8787
# e.g., "0 * complex128", "0 * var * union[complex128, datetime64[us]]"
8888
expected_typestr = str(ak.types.ArrayType(a.type.content, 0))
8989
assert result.typestr == expected_typestr
90+
91+
92+
def test_flatten_empty_array() -> None:
93+
"""Assert empty arrays can be flattened unless its type forbids it."""
94+
95+
# An empty array of unknown type can be flattened as unknown type can be
96+
# flattened, e.g., an empty array of nested arrays.
97+
a = ak.Array([]) # type='0 * unknown'
98+
assert ak.flatten(a).to_list() == []
99+
100+
# The empty sub-array is an empty array of arrays of integers.
101+
a = ak.Array([[[1]], []]) # type='2 * var * var * int64'
102+
assert ak.flatten(a, axis=2).to_list() == [[1], []]
103+
104+
# The type forbids flattening.
105+
a = ak.from_numpy(np.array([], dtype=int)) # type='0 * int64'
106+
with pytest.raises(ak.errors.AxisError):
107+
ak.flatten(a)

0 commit comments

Comments
 (0)