Replies: 2 comments 4 replies
-
ak.zip and ak.unzip were not intended to be perfect inverses of each other. ak.flatten and ak.unflatten are not perfect opposites either, and for the same reason: to invert ak.zip, you need both ak.fields and ak.unzip; to invert ak.unflatten, you need both ak.num and ak.flatten. Personally, I feel like it's enough to document this. As a user, I wouldn't expect perfect symmetry. (Though I'm not a user; actual users are invited to chime in below!) I would, however, want convenience, and having to unpack a pair of The main use-case for ak.unzip is to turn the "everything is in one array" result from something like ak.cartesian or ak.combinations into "parts of that calculation are in different arrays so that I can use them in separate formulas." At times, I've thought that ak.cartesian and ak.combinations had the wrong interface—that they should return results in split form—but adding ak.unzip eliminated that need, since it can be a two-part idiom. Unlike an extra function argument in ak.cartesian and ak.combinations, a separate ak.unzip function can be used in other contexts as well. That, anyway, was the motivation. |
Beta Was this translation helpful? Give feedback.
-
From time to time i end up having arrays where i want to turn a RecordArray(ListOffsetArray) into a ListOffsetArray(RecordArray). Usually i try to avoid creating it like that, but just for an example: >>> import awkward as ak
>>> array = ak.Array([{"a": [1, 2], "b": [3, 4]}, {"a": [5], "b": [6]}])
>>> array.typestr
'2 * {a: var * int64, b: var * int64}' to make it into a >>> ak.zip(dict(zip(array.fields, ak.unzip(array)))) which is a bit clumsy. In case others have that use case as well - would it make sense for |
Beta Was this translation helpful? Give feedback.
-
Description of new feature
By name,
ak.unzip
is the logical inverse ofak.zip
. However, at the moment, the API is not symmetrical;ak.zip
can produce tuples or named records from the input argument, whereasak.unzip
always returns a tuple of arrays.If this can be improved upon, then one option is to return a mapping or a tuple according to the array type. This would work, but I'm not convinced that returning different types is the best design.
Perhaps a better solution would be to return two tuples; one for the keys, and one for values?
Although users would still need to handle the two cases differently (named records vs tuples), this would be as simple as testing
len(keys)
(or justbool(keys)
if one is being Pythonic). This is also true of usingak.fields
withak.unzip
, so I don't think it's a strong counterargument.This issue proposes to clean-up
ak.unzip
, but clearly it would entail a breaking change.Beta Was this translation helpful? Give feedback.
All reactions