Skip to content

Commit

Permalink
Feat: add optiontype_outside_record arg to ak.zip for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Feb 23, 2022
1 parent 49f269f commit 811c267
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/awkward/_v2/operations/structure/ak_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def zip(
highlevel=True,
behavior=None,
right_broadcast=False,
optiontype_outside_record=False,
):

"""
Expand All @@ -35,6 +36,8 @@ def zip(
high-level.
right_broadcast (bool): If True, follow rules for implicit
right-broadcasting, as described in #ak.broadcast_arrays.
optiontype_outside_record (bool): If True, continue broadcasting past
any option types before creating the new #ak.layout.RecordArray node.
Combines `arrays` into a single structure as the fields of a collection
of records or the slots of a collection of tuples. If the `arrays` have
Expand Down Expand Up @@ -104,6 +107,22 @@ def zip(
As an extreme, `depth_limit=1` is a handy way to make a record structure
at the outermost level, regardless of whether the fields have matching
structure or not.
When zipping together arrays with optional values, it can be useful to create
the #ak.layout.RecordArray node after the option types. By default, #ak.zip
does not do this:
>>> one = ak.Array([1, 2, None])
>>> two = ak.Array([None, 5, 6])
>>> ak.zip([one, two])
<Array [(1, None), (2, 5), (None, 6)] type='3 * (?int64, ?int64)'>
If the `optiontype_outside_record` option is set to `True`, Awkward will continue to
broadcast the arrays together at the depth_limit until it reaches non-option
types. This effectively takes the union of the option mask:
>>> ak.zip([one, two], optiontype_outside_record=True)
<Array [None, (2, 5), None] type='3 * ?(int64, int64)'>
"""
if depth_limit is not None and depth_limit <= 0:
raise ValueError("depth_limit must be None or at least 1")
Expand Down Expand Up @@ -165,6 +184,10 @@ def action(inputs, depth, **ignore):
for x in inputs
)
):
# If we want to zip after option types at this depth
if optiontype_outside_record and any(x.is_OptionType for x in inputs):
return None

return (
ak._v2.contents.RecordArray(
inputs, recordlookup, parameters=parameters
Expand Down

0 comments on commit 811c267

Please sign in to comment.