diff --git a/bigframes/dataframe.py b/bigframes/dataframe.py index bb17bb9cb2..602865abd9 100644 --- a/bigframes/dataframe.py +++ b/bigframes/dataframe.py @@ -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) 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`).