Skip to content

feat: Add DataFrame ~ operator #721

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 2 commits into from
May 23, 2024
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
5 changes: 5 additions & 0 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,11 @@ def __ne__(self, other) -> DataFrame: # type: ignore

__ne__.__doc__ = inspect.getdoc(vendored_pandas_frame.DataFrame.__ne__)

def __invert__(self) -> DataFrame:
return self._apply_unary_op(ops.invert_op)

__invert__.__doc__ = inspect.getdoc(vendored_pandas_frame.DataFrame.__invert__)

def le(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.le_op, axis=axis)

Expand Down
10 changes: 10 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,16 @@ def test_df_abs(scalars_dfs):
assert_pandas_df_equal(bf_result, pd_result)


def test_df_invert(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
columns = ["int64_col", "bool_col"]

bf_result = (~scalars_df[columns]).to_pandas()
pd_result = ~scalars_pandas_df[columns]

assert_pandas_df_equal(bf_result, pd_result)


def test_df_isnull(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs

Expand Down
24 changes: 24 additions & 0 deletions third_party/bigframes_vendored/pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,30 @@ def __eq__(self, other):
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

def __invert__(self) -> DataFrame:
"""
Returns the bitwise inversion of the DataFrame, element-wise
using operator `~`.

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

>>> df = bpd.DataFrame({'a':[True, False, True], 'b':[-1, 0, 1]})
>>> ~df
a b
0 False 0
1 True -1
2 False -2
<BLANKLINE>
[3 rows x 2 columns]

Returns:
DataFrame: The result of inverting elements in the input.
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

def ne(self, other, axis: str | int = "columns") -> DataFrame:
"""
Get not equal to of DataFrame and other, element-wise (binary operator `ne`).
Expand Down