Expression

This module contains classes for representing expressions.

class expr.Expr

Bases: object

Representation of an expression for Column transformation.

__and__(other)

Operator & for logical AND.

Returns a Binary Expression AND between self and another Expr or constant

Example

>>> 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

BinaryExpr

__or__(other)

Operator | for logical OR.

Returns a Binary Expression OR between self and another Expr or constant

Example

>>> 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

BinaryExpr

__eq__(other)

Operator ==.

Returns a Binary Expression EQUAL between self and another Expr or constant

Example

>>> 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

BinaryExpr

__lt__(other)

Operator <.

Returns a Binary Expression LESS THAN between self and another Expr or constant

Example

>>> 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

BinaryExpr

__le__(other)

Operator <=.

Returns a Binary Expression LESS EQUAL between self and another Expr or constant

Example

>>> 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

BinaryExpr

__gt__(other)

Operator >.

Returns a Binary Expression GREATER THAN between self and another Expr or constant

Example

>>> 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

BinaryExpr

__ge__(other)

Operator >=.

Returns a Binary Expression GREATER EQUAL between self and another Expr or constant

Example

>>> 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

BinaryExpr

__ne__(other)

Operator !=.

Returns a Binary Expression NOT EQUAL between self and another Expr or constant

Example

>>> 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

BinaryExpr

__mod__(other)

Operator %.

Returns a Binary Expression Modulo between an Expr and another Expr or constant

Example

>>> 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

BinaryExpr

__add__(other)

Operator +.

Returns a Binary Expression Addition between an Expr and another Expr or constant

Example

>>> 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

BinaryExpr

__sub__(other)

Operator -.

Returns a Binary Expression Subtraction between an Expr and another Expr or constant

Example

>>> 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

BinaryExpr

__mul__(other)

Operator *.

Returns a Binary Expression Multiplication between an Expr and another Expr or constant

Example

>>> 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

BinaryExpr

__truediv__(other)

Operator /.

Returns a Binary Expression Division between an Expr and another Expr 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

BinaryExpr

__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

UnaryExpr

__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

UnaryExpr

__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

UnaryExpr

__invert__()

Operator ~ for logical NOT.

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

UnaryExpr

like(pattern)

Return BinaryExpr for pattern matching with the LIKE clause in SQL.

Parameters

pattern (str) – str: regex pattern

Returns

Expr

Return type

BinaryExpr

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 GreenplumPython DataFrame, or a list of values of the same type as the current Expr.

Returns

A boolean Expr whose values are of the same length as the current Expr.

Return type

InExpr

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)
class expr.BinaryExpr

Bases: Expr

Representation of a Binary Expression.

class expr.UnaryExpr

Bases: Expr

Representation of a Unary Expression.

class expr.InExpr

Bases: Expr