How to use the awkward.type.fromarray function in awkward

To help you get started, we’ve selected a few awkward examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scikit-hep / awkward-array / awkward / util.py View on Github external
if isinstance(tpe, awkward.type.TableType):
            starts, stops = None, None
            out, deferred, unflattened = None, {}, None

            for n in tpe.columns:
                if not isinstance(n, str):
                    raise ValueError("column names must be strings")

                arrayn = unwrap(array[n])
                tpen = tpe[n]
                colsn = cols + (n,) if seriously else cols

                if isinstance(arrayn, awkward.array.objects.ObjectArray) and not isinstance(arrayn, awkward.array.objects.StringArray):
                    arrayn = arrayn.content
                if not isinstance(tpen, (numpy.dtype, str, bytes, awkward.type.Type)):
                    tpen = awkward.type.fromarray(arrayn).to
                if isinstance(tpen, numpy.dtype):
                    columns.append(colsn)
                    tmp = arrayn

                elif isinstance(tpen, type) and issubclass(tpen, (str, bytes)):
                    columns.append(colsn)
                    tmp = arrayn

                elif isinstance(tpen, awkward.type.ArrayType) and tpen.takes == numpy.inf:
                    tmp = JaggedArray(arrayn.starts, arrayn.stops, recurse(arrayn.content, tpen.to, colsn, True))

                elif isinstance(tpen, awkward.type.TableType):
                    tmp = recurse(arrayn, tpen, colsn, True)

                elif isinstance(tpen, awkward.type.OptionType) and isinstance(arrayn.content, numpy.ndarray):
                    columns.append(colsn)
github scikit-hep / awkward-array / awkward / array / virtual.py View on Github external
def materialize(self):
        array = self._util_toarray(self._generator(*self._args, **self._kwargs), self.DEFAULTTYPE)
        if self._setitem is not None:
            for n, x in self._setitem.items():
                array[n] = x
        if self._delitem is not None:
            for n in self._delitem:
                del array[n]

        if self._type is not None:
            materializedtype = awkward.type.fromarray(array)
            if ((isinstance(self._type, awkward.type.Type) and not self._type._eq(materializedtype, set(), ignoremask=True)) or
                (not isinstance(self._type, awkward.type.Type) and not self._type == materializedtype)):
                raise TypeError("materialized array has type\n\n{0}\n\nexpected type\n\n{1}".format(awkward.type._str(awkward.type.fromarray(array), indent="    "), awkward.type._str(self._type, indent="    ")))

        if self._cache is None:
            # states (1), (2), and (6)
            self._array = array
        else:
            # states (3) and (4)
            self._array = self.key
            self._cache[self._array] = array

        return array
github scikit-hep / awkward-array / awkward / array / sparse.py View on Github external
def type(self):
        return awkward.type.ArrayType(self._length, awkward.type.fromarray(self._content).to)
github scikit-hep / awkward-array / awkward / array / chunked.py View on Github external
def knowtype(self, at):
        if not 0 <= at < len(self._chunks):
            raise ValueError("cannot knowtype at chunkid {0} with {1} chunks".format(at, len(self._chunks)))
        chunk = self._chunks[at]
        if len(chunk) == 0:
            self._types[at] = ()
        else:
            self._types[at] = awkward.type.fromarray(chunk).to
        return self._types[at]
github scikit-hep / awkward-array / impl_flatpandas.py View on Github external
if isinstance(array[n], awkward.array.jagged.JaggedArray):
                    if unflattened is None:
                        localindex.insert(0, out[n].localindex.flatten())
                    else:
                        oldloc = unflattened.content.localindex
                        tab = JaggedArray(oldloc.starts, oldloc.stops, Table({"oldloc": oldloc.content}))
                        tab["newloc"] = array[n].localindex.flatten()
                        localindex.insert(0, tab["newloc"].flatten())
                    break

            return out[tpe.columns]

        else:
            return recurse(Table({"": array}), awkward.type.TableType(**{"": tpe}), cols, False)[""]

    tmp = recurse(array, awkward.type.fromarray(array).to, (), True)
    if isinstance(tmp, awkward.array.jagged.JaggedArray):
        tmp = tmp.flatten()

    deepest = max(len(x) for x in columns)

    out = {}
    for i, col in enumerate(columns):
        x = tmp
        for c in col:
            x = x[c]
        columns[i] = col + ("",) * (deepest - len(col))
        out[columns[i]] = x

    index = globalindex + localindex
    if len(index) == 1:
        index = pandas.Index(index[0])
github scikit-hep / awkward-array / awkward / util.py View on Github external
if isinstance(arrayn, awkward.array.jagged.JaggedArray):
                    if unflattened is None:
                        localindex.insert(0, out[n].localindex.flatten())
                    else:
                        oldloc = unflattened.content.localindex
                        tab = JaggedArray(oldloc.starts, oldloc.stops, Table({"oldloc": oldloc.content}))
                        tab["newloc"] = arrayn.localindex.flatten()
                        localindex.insert(0, tab["newloc"].flatten())
                    break

            return out[tpe.columns]

        else:
            return recurse(Table({"": array}), awkward.type.TableType(**{"": tpe}), cols, False)[""]

    tmp = recurse(array, awkward.type.fromarray(array).to, (), True)
    if isinstance(tmp, awkward.array.jagged.JaggedArray):
        tmp = tmp.flatten()

    deepest = max(len(x) for x in columns)

    out = {}
    for i, col in enumerate(columns):
        x = tmp
        for c in col:
            x = x[c]
        columns[i] = col + ("",) * (deepest - len(col))
        out[columns[i]] = x

    index = globalindex + localindex
    if len(index) == 1:
        index = pandas.Index(index[0])