From 07c05f40eee129386f0864ee8ceb64aeecbb7508 Mon Sep 17 00:00:00 2001 From: Trevor Bergeron Date: Thu, 23 May 2024 20:14:46 +0000 Subject: [PATCH] feat: Add DataFrame `~` operator --- bigframes/dataframe.py | 5 ++++ tests/system/small/test_dataframe.py | 10 ++++++++ .../bigframes_vendored/pandas/core/frame.py | 24 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/bigframes/dataframe.py b/bigframes/dataframe.py index e8f219d301..4b67ed65e1 100644 --- a/bigframes/dataframe.py +++ b/bigframes/dataframe.py @@ -903,6 +903,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) diff --git a/tests/system/small/test_dataframe.py b/tests/system/small/test_dataframe.py index 4b50922c0d..dbdcf7dafc 100644 --- a/tests/system/small/test_dataframe.py +++ b/tests/system/small/test_dataframe.py @@ -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 diff --git a/third_party/bigframes_vendored/pandas/core/frame.py b/third_party/bigframes_vendored/pandas/core/frame.py index 31d5e88c7e..01c7ab70ee 100644 --- a/third_party/bigframes_vendored/pandas/core/frame.py +++ b/third_party/bigframes_vendored/pandas/core/frame.py @@ -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 + + [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`).