Skip to content

Add logical operator support (__and__/__or__ methods on dataframe and column) #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions spec/API_specification/dataframe_api/column_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def __eq__(self, other: Column | Scalar) -> Column:
"""
Compare for equality.

Nulls should follow Kleene Logic.

Parameters
----------
other : Column or Scalar
Expand All @@ -89,6 +91,8 @@ def __ne__(self, other: Column | Scalar) -> Column:
"""
Compare for non-equality.

Nulls should follow Kleene Logic.

Parameters
----------
other : Column or Scalar
Expand Down Expand Up @@ -165,9 +169,51 @@ def __lt__(self, other: Column | Scalar) -> Column:
Column
"""

def __and__(self, other: Column | Scalar) -> Column:
def __and__(self, other: Column[bool] | bool) -> Column[bool]:
"""
Apply logical 'and' to `other` Column (or scalar) and this Column.

Nulls should follow Kleene Logic.

Parameters
----------
other : Column[bool] or bool
If Column, must have same length.

Returns
-------
Column

Raises
------
ValueError
If `self` or `other` is not boolean.
"""

def __or__(self, other: Column[bool] | bool) -> Column[bool]:
"""
Apply logical 'or' to `other` Column (or scalar) and this column.

Nulls should follow Kleene Logic.

Parameters
----------
other : Column[bool] or Scalar
If Column, must have same length.

Returns
-------
Column[bool]

Raises
------
ValueError
If `self` or `other` is not boolean.
"""

def __add__(self, other: Column | Scalar) -> Column:
"""
Add `other` dataframe or scalar to this column.
Add `other` column or scalar to this column.

Parameters
----------
Expand All @@ -183,7 +229,7 @@ def __and__(self, other: Column | Scalar) -> Column:

def __sub__(self, other: Column | Scalar) -> Column:
"""
Subtract `other` dataframe or scalar from this column.
Subtract `other` column or scalar from this column.

Parameters
----------
Expand All @@ -199,7 +245,7 @@ def __sub__(self, other: Column | Scalar) -> Column:

def __mul__(self, other: Column | Scalar) -> Column:
"""
Multiply `other` dataframe or scalar with this column.
Multiply `other` column or scalar with this column.

Parameters
----------
Expand Down Expand Up @@ -231,7 +277,7 @@ def __truediv__(self, other: Column | Scalar) -> Column:

def __floordiv__(self, other: Column | Scalar) -> Column:
"""
Floor-divide `other` dataframe or scalar to this column.
Floor-divide `other` column or scalar to this column.

Parameters
----------
Expand Down
46 changes: 46 additions & 0 deletions spec/API_specification/dataframe_api/dataframe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def __eq__(self, other: DataFrame | Scalar) -> DataFrame:
"""
Compare for equality.

Nulls should follow Kleene Logic.

Parameters
----------
other : DataFrame or Scalar
Expand All @@ -329,6 +331,8 @@ def __ne__(self, other: DataFrame | Scalar) -> DataFrame:
"""
Compare for non-equality.

Nulls should follow Kleene Logic.

Parameters
----------
other : DataFrame or Scalar
Expand Down Expand Up @@ -410,6 +414,48 @@ def __lt__(self, other: DataFrame | Scalar) -> DataFrame:
"""
...

def __and__(self, other: DataFrame[bool] | bool) -> DataFrame[bool]:
"""
Apply logical 'and' to `other` DataFrame (or scalar) and this dataframe.

Nulls should follow Kleene Logic.

Parameters
----------
other : DataFrame[bool] or bool
If DataFrame, must have same length.

Returns
-------
DataFrame[bool]

Raises
------
ValueError
If `self` or `other` is not boolean.
"""

def __or__(self, other: DataFrame[bool] | bool) -> DataFrame[bool]:
"""
Apply logical 'or' to `other` DataFrame (or scalar) and this DataFrame.

Nulls should follow Kleene Logic.

Parameters
----------
other : DataFrame[bool] or bool
If DataFrame, must have same length.

Returns
-------
DataFrame[bool]

Raises
------
ValueError
If `self` or `other` is not boolean.
"""

def __add__(self, other: DataFrame | Scalar) -> DataFrame:
"""
Add `other` dataframe or scalar to this dataframe.
Expand Down