Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/usr/bin/env python
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-array/blob/master/LICENSE
import collections
import numbers
import awkward.array.base
import awkward.type
import awkward.util
class MaskedArray(awkward.array.base.AwkwardArrayWithContent):
"""
MaskedArray
"""
masked = None
def __init__(self, mask, content, maskedwhen=True):
self.mask = mask
self.content = content
self.maskedwhen = maskedwhen
@classmethod
def fromcontent(cls, content, maskedwhen=True):
if maskedwhen:
mask = cls.numpy.zeros(len(content), dtype=cls.MASKTYPE)
else:
import pandas
import awkward.array.base
import awkward.array.chunked
import awkward.array.jagged
import awkward.array.objects
import awkward.array.table
import awkward.array.virtual
import awkward.type
if isinstance(array, awkward.array.base.AwkwardArray):
numpy = array.numpy
JaggedArray = array.JaggedArray
Table = array.Table
else:
JaggedArray = awkward.array.jagged.JaggedArray
Table = awkward.array.table.Table
def unwrap(a):
if isinstance(a, awkward.array.chunked.ChunkedArray):
chunks = [unwrap(x) for x in a.chunks]
if any(isinstance(x, awkward.array.jagged.JaggedArray) for x in chunks):
return awkward.array.jagged.JaggedArray.concatenate(chunks)
else:
return numpy.concatenate([x.regular() for x in chunks])
elif isinstance(a, awkward.array.virtual.VirtualArray):
return a.array
else:
return a
globalindex = [None]
localindex = []
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])
else:
def _reduce(self, ufunc, identity, dtype, regularaxis):
if self._util_hasjagged(self._content):
return self.copy(content=self._content._reduce(ufunc, identity, dtype, regularaxis))
elif isinstance(self._content, awkward.array.table.Table):
out = awkward.array.table.Table()
for n, x in self._content._contents.items():
out[n] = self.copy(content=x)
return out._reduce(ufunc, identity, dtype, regularaxis)
else:
return ufunc.reduce(self._prepare(identity, dtype))
def recurse(x):
if isinstance(x, awkward.array.objects.ObjectArray):
return x.copy(content=recurse(x.content))
elif isinstance(x, awkward.array.table.Table):
content = x.empty_like()
for n in x.columns:
content[n] = recurse(x[n])
return content
elif good is None:
if len(x.shape) == 0:
content = self.numpy.full(len(parents), x, dtype=x.dtype)
else:
content = x.reshape(-1)[parents]
return content
else:
class DelegatedProperty(Delegated):
def _get_result(self, obj, type=None):
return getattr(object.__getattribute__(obj, '_data'), self.name)
class DelegatedMethod(Delegated):
def __get__(self, obj, type=None):
index = object.__getattribute__(obj, '_index')
name = object.__getattribute__(bj, '_name')
method = getattr(object.__getattribute__(obj, '_data'), self.name)
return delegated_method(method, index, name)
class AwkwardType(ExtensionDtype):
name = 'awkward'
type = awkward.array.base.AwkwardArray
kind = 'O'
@classmethod
def construct_from_string(cls, string):
if string == cls.name:
return cls()
else:
raise TypeError("Cannot construct a '{}' from"
"'{}'".format(cls, string))
# -----------------------------------------------------------------------------
#
# Pandas accessors
# -----------------------------------------------------------------------------
def _mixin(methods, awkwardtype):
'''Like awkward.Methods.mixin but also captures methods in dir() and propagate docstr'''
if not issubclass(awkwardtype, awkward.array.base.AwkwardArray):
raise ValueError("Only mix in to awkward types or derived")
if not issubclass(methods, awkward.array.objects.Methods):
raise ValueError("Can only mixin methods deriving from awkward Methods ABC")
newtype = type(methods.__name__ + 'Array', (methods, awkwardtype), {})
newtype.__dir__ = lambda self: dir(methods) + awkwardtype.__dir__(self)
if six.PY3:
newtype.__doc__ = methods.__doc__
return newtype