Expression
This module contains classes for representing expressions.
- class expr.Expr
Bases:
object
Representation of an expression for
Column
transformation.- __and__(other)
Operator
&
for logicalAND
.Returns a Binary Expression AND between self and another
Expr
or constantExample
>>> import greenplumpython.builtins.functions as F >>> df = db.assign(id=lambda: F.generate_series(0, 9)) >>> df[lambda t: (t["id"] >= 3) & (t["id"] < 8)].order_by("id")[:] ---- id ---- 3 4 5 6 7 ---- (5 rows)
- Parameters
other (Any) –
- Return type
- __or__(other)
Operator
|
for logicalOR
.Returns a Binary Expression OR between self and another
Expr
or constantExample
>>> rows = [(1,), (2,), (3,), (-2,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df[lambda t: (t["id"] >= 3) | (t["id"] < 0)].order_by("id")[:] ---- id ---- -2 3 ---- (2 rows)
- Parameters
other (Any) –
- Return type
- __eq__(other)
Operator
==
.Returns a Binary Expression EQUAL between self and another
Expr
or constantExample
>>> rows = [(1,), (2,), (3,), (2,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df[lambda t: t["id"] == 2].order_by("id")[:] ---- id ---- 2 2 ---- (2 rows)
- Parameters
other (Any) –
- Return type
- __lt__(other)
Operator
<
.Returns a Binary Expression LESS THAN between self and another
Expr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df[lambda t: t["id"] < 3].order_by("id")[:] ---- id ---- 1 2 ---- (2 rows)
- Parameters
other (Any) –
- Return type
- __le__(other)
Operator
<=
.Returns a Binary Expression LESS EQUAL between self and another
Expr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df[lambda t: t["id"] <= 3].order_by("id")[:] ---- id ---- 1 2 3 ---- (3 rows)
- Parameters
other (Any) –
- Return type
- __gt__(other)
Operator
>
.Returns a Binary Expression GREATER THAN between self and another
Expr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df[lambda t: t["id"] > 3].order_by("id")[:] ---- id ---- 4 ---- (1 row)
- Parameters
other (Any) –
- Return type
- __ge__(other)
Operator
>=
.Returns a Binary Expression GREATER EQUAL between self and another
Expr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df[lambda t: t["id"] >= 3].order_by("id")[:] ---- id ---- 3 4 ---- (2 rows)
- Parameters
other (Any) –
- Return type
- __ne__(other)
Operator
!=
.Returns a Binary Expression NOT EQUAL between self and another
Expr
or constantExample
>>> rows = [(1,), (2,), (3,), (2,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df[lambda t: t["id"] != 2].order_by("id")[:] ---- id ---- 1 3 ---- (2 rows)
- Parameters
other (Any) –
- Return type
- __mod__(other)
Operator
%
.Returns a Binary Expression Modulo between an
Expr
and anotherExpr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(mod2=lambda t: t["id"] % 2).order_by("id")[:] ----------- id | mod2 ----+------ 1 | 1 2 | 0 3 | 1 4 | 0 ----------- (4 rows)
- Parameters
other (Any) –
- Return type
- __add__(other)
Operator
+
.Returns a Binary Expression Addition between an
Expr
and anotherExpr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(next=lambda t: t["id"] + 1).order_by("id")[:] ----------- id | next ----+------ 1 | 2 2 | 3 3 | 4 4 | 5 ----------- (4 rows)
- Parameters
other (Any) –
- Return type
- __sub__(other)
Operator
-
.Returns a Binary Expression Subtraction between an
Expr
and anotherExpr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(prev=lambda t: t["id"] - 1).order_by("id")[:] ----------- id | prev ----+------ 1 | 0 2 | 1 3 | 2 4 | 3 ----------- (4 rows)
- Parameters
other (Any) –
- Return type
- __mul__(other)
Operator
*
.Returns a Binary Expression Multiplication between an
Expr
and anotherExpr
or constantExample
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(double=lambda t: t["id"] * 2).order_by("id")[:] ------------- id | double ----+-------- 1 | 2 2 | 4 3 | 6 4 | 8 ------------- (4 rows)
- Parameters
other (Any) –
- Return type
- __truediv__(other)
Operator
/
.Returns a Binary Expression Division between an
Expr
and anotherExpr
or constant. It results integer division between two integers, and true division if one of the arguments is a float.Example
>>> rows = [(1,), (2,), (3,), (4,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(true_div=lambda t: t["id"] / 2).order_by("id")[:] --------------- id | true_div ----+---------- 1 | 0 2 | 1 3 | 1 4 | 2 --------------- (4 rows)
- Parameters
other (Any) –
- Return type
- __pos__()
Operator
+
.Returns a Unary Expression POSITIVE of self
Example
>>> rows = [(1,), (2,), (-3,), (-2,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(pos=lambda t: +t["id"]).order_by("id")[:] ---------- id | pos ----+----- -3 | -3 -2 | -2 1 | 1 2 | 2 ---------- (4 rows)
- Return type
- __neg__()
Operator
-
.Returns a Unary Expression NEGATIVE of self
Example
>>> rows = [(1,), (2,), (-3,), (-2,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(neg=lambda t: -t["id"]).order_by("id")[:] ---------- id | neg ----+----- -3 | 3 -2 | 2 1 | -1 2 | -2 ---------- (4 rows)
- Return type
- __abs__()
Operator
abs()
.Returns a Unary Expression ABSOLUTE of self
Example
>>> rows = [(1,), (2,), (-3,), (-2,)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(abs=lambda t: abs(t["id"])).order_by("id")[:] ---------- id | abs ----+----- -3 | 3 -2 | 2 1 | 1 2 | 2 ---------- (4 rows)
- Return type
- __invert__()
Operator
~
for logicalNOT
.Returns a Unary Expression NOT of self
Example
>>> rows = [(1, True,), (2, False,), (3, False,), (4, True,)] >>> df = db.create_dataframe(rows=rows, column_names=["id", "is"]) >>> df.assign(inv=lambda t: ~t["is"]).order_by("id")[:] -------------------- id | is | inv ----+-------+------- 1 | 1 | 0 2 | 0 | 1 3 | 0 | 1 4 | 1 | 0 -------------------- (4 rows)
- Return type
- like(pattern)
Return BinaryExpr for pattern matching with the LIKE clause in SQL.
- Parameters
pattern (str) – str: regex pattern
- Returns
Expr
- Return type
Example
>>> rows = [("aaa",), ("bba",), ("acac",)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign( ... a_start=lambda t: t["id"].like(r"a%"), ... a_end=lambda t: t["id"].like(r"%a"), ... a_middle=lambda t: t["id"].like(r"%a%"), ... a_sec_posi=lambda t: t["id"].like(r"_a%") ... ).order_by("id")[:] ------------------------------------------------ id | a_start | a_end | a_middle | a_sec_posi ------+---------+-------+----------+------------ aaa | 1 | 1 | 1 | 1 acac | 1 | 0 | 1 | 0 bba | 0 | 1 | 1 | 0 ------------------------------------------------ (3 rows)
- in_(container)
Test whether each value of current
Expr
is in the container.It is analogous to the built-in in operator of Python and SQL.
- Parameters
container (Union[Expr, List[Any]]) – A collection of values. It can either be another
Expr
representing a transformed column of GreenplumPythonDataFrame
, or a list of values of the same type as the currentExpr
.- Returns
A boolean
Expr
whose values are of the same length as the currentExpr
.- Return type
Example
>>> rows = [(i,) for i in range(5)] >>> df = db.create_dataframe(rows=rows, column_names=["id"]) >>> df.assign(in_list=lambda t: t["id"].in_([1, 2, 3])).order_by("id")[:] -------------- id | in_list ----+--------- 0 | 0 1 | 1 2 | 1 3 | 1 4 | 0 -------------- (5 rows)