ak.unflatten
------------

Defined in `awkward.operations.structure <https://github.com/scikit-hep/awkward-1.0/blob/80bbef0738a6b7928333d7c705ee1b359991de5b/src/awkward/operations/structure.py>`__ on `line 2023 <https://github.com/scikit-hep/awkward-1.0/blob/80bbef0738a6b7928333d7c705ee1b359991de5b/src/awkward/operations/structure.py#L2023>`__.

.. py:function:: ak.unflatten(array, counts, axis=0, highlevel=True, behavior=None)


    :param array: Data to create an array with an additional level from.
    :param counts: Number of elements the new level should have.
               If an integer, the new level will be regularly sized; otherwise,
               it will consist of variable-length lists with the given lengths.
    :type counts: int or array
    :param axis: The dimension at which this operation is applied. The
             outermost dimension is ``0``, followed by ``1``, etc., and negative
             values count backward from the innermost: ``-1`` is the innermost
             dimension, ``-2`` is the next level up, etc.
    :type axis: int
    :param highlevel: If True, return an :py:obj:`ak.Array`; otherwise, return
                  a low-level :py:obj:`ak.layout.Content` subclass.
    :type highlevel: bool
    :param behavior: Custom :py:obj:`ak.behavior` for the output array, if
                 high-level.
    :type behavior: None or dict

Returns an array with an additional level of nesting. This is roughly the
inverse of :py:obj:`ak.flatten`, where ``counts`` were obtained by :py:obj:`ak.num` (both with
``axis=1``).

For example,

.. code-block:: python


    >>> original = ak.Array([[0, 1, 2], [], [3, 4], [5], [6, 7, 8, 9]])
    >>> counts = ak.num(original)
    >>> array = ak.flatten(original)
    >>> counts
    <Array [3, 0, 2, 1, 4] type='5 * int64'>
    >>> array
    <Array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] type='10 * int64'>
    >>> ak.unflatten(array, counts)
    <Array [[0, 1, 2], [], ... [5], [6, 7, 8, 9]] type='5 * var * int64'>

An inner dimension can be unflattened by setting the ``axis`` parameter, but
operations like this constrain the ``counts`` more tightly.

For example, we can subdivide an already divided list:

.. code-block:: python


    >>> original = ak.Array([[1, 2, 3, 4], [], [5, 6, 7], [8, 9]])
    >>> print(ak.unflatten(original, [2, 2, 1, 2, 1, 1], axis=1))
    [[[1, 2], [3, 4]], [], [[5], [6, 7]], [[8], [9]]]

But the counts have to add up to the lengths of those lists. We can't mix
values from the first ``[1, 2, 3, 4]`` with values from the next ``[5, 6, 7]``.

.. code-block:: python


    >>> print(ak.unflatten(original, [2, 1, 2, 2, 1, 1], axis=1))
    Traceback (most recent call last):
    ...
    ValueError: structure imposed by 'counts' does not fit in the array at axis=1

Also note that new lists created by this function cannot cross partitions
(which is only possible at ``axis=0``, anyway).

See also :py:obj:`ak.num` and :py:obj:`ak.flatten`.

