imaginglss.utils.npyquery module¶
npyquery, Query mini-language for numpy arrays.
We reuse the python expression parser to construct the AST. This is inspired by TinyDB, and may have been inspired by other python database implementations as well.
- Override Column.apply for customized access to objects (e.g.
- Pandas dataframe or imaginglss’s ColumnStore )
Simple manipulation of the AST is allowed. For example,
node.assume(a, b)
allows one to replace ‘a’ with ‘b’
in the expression.
Examples¶
>>> d = dtype([
('BlackholeMass', 'f4'),
('Position', ('f4', 3))])
>>> data = numpy.zeros(4, dtype=d)
>>> data['BlackholeMass'][:] = numpy.arange(4)
>>> data['Position'][:] = numpy.arange(12).reshape(3, 1)
>>> query = (Column('BlackholeMass') > 2.0)
>>> query &= (Column('BlackholeMass') > 4.0)
>>> query &= (Column('Position')[2] > 1.0)
>>> print(query.apply(data))
>>> query = query.assume(Column('BlackholeMass'), 1.0)
>>> print(query.apply(data))
-
class
imaginglss.utils.npyquery.
Column
(name)[source]¶ Bases:
imaginglss.utils.npyquery.Node
Represents accessing a column from the data array
-
names
¶
-
-
class
imaginglss.utils.npyquery.
Expr
(operator, function, operands)[source]¶ Bases:
imaginglss.utils.npyquery.Node
Represents an expression.
e.g. comparing a column with a number.
An expression can have multiple operands.
>>> for o in expr.operands: >>> ..... >>> print expr[0], expr[1]
-
flatten
(operands)[source]¶ Flattens operands of associative operators.
e.g. (a + b) + (c + d) becomes a + b + c + d
-
operands
¶
-
-
class
imaginglss.utils.npyquery.
GetItem
(obj, index)[source]¶ Bases:
imaginglss.utils.npyquery.Node
Represents accesing an item in a vector column.
Astonishing that pandas can’t do this.
-
obj
¶
-
-
class
imaginglss.utils.npyquery.
Literal
(value)[source]¶ Bases:
imaginglss.utils.npyquery.Node
Represents a literal constant.
-
class
imaginglss.utils.npyquery.
Node
[source]¶ Bases:
object
A node in the query expression.
Parameters: array : ndarray or alike
applying the query node to the array and returns items satisfying the array.
-
T
¶
-
names
¶ returns a list of column names used in this node.
This function is recursive
-
-
class
imaginglss.utils.npyquery.
Transpose
(node)[source]¶ Bases:
imaginglss.utils.npyquery.Node
Represents a transpose. (.T attribute)
-
obj
¶
-