Skip to content

Commit 354abc1

Browse files
feat: Add DataFrame ~ operator (#721)
1 parent d31cebd commit 354abc1

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

bigframes/dataframe.py

+5
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,11 @@ def __ne__(self, other) -> DataFrame: # type: ignore
904904

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

907+
def __invert__(self) -> DataFrame:
908+
return self._apply_unary_op(ops.invert_op)
909+
910+
__invert__.__doc__ = inspect.getdoc(vendored_pandas_frame.DataFrame.__invert__)
911+
907912
def le(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
908913
return self._apply_binop(other, ops.le_op, axis=axis)
909914

tests/system/small/test_dataframe.py

+10
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,16 @@ def test_df_abs(scalars_dfs):
16991699
assert_pandas_df_equal(bf_result, pd_result)
17001700

17011701

1702+
def test_df_invert(scalars_dfs):
1703+
scalars_df, scalars_pandas_df = scalars_dfs
1704+
columns = ["int64_col", "bool_col"]
1705+
1706+
bf_result = (~scalars_df[columns]).to_pandas()
1707+
pd_result = ~scalars_pandas_df[columns]
1708+
1709+
assert_pandas_df_equal(bf_result, pd_result)
1710+
1711+
17021712
def test_df_isnull(scalars_dfs):
17031713
scalars_df, scalars_pandas_df = scalars_dfs
17041714

third_party/bigframes_vendored/pandas/core/frame.py

+24
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,30 @@ def __eq__(self, other):
20032003
"""
20042004
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
20052005

2006+
def __invert__(self) -> DataFrame:
2007+
"""
2008+
Returns the bitwise inversion of the DataFrame, element-wise
2009+
using operator `~`.
2010+
2011+
**Examples:**
2012+
2013+
>>> import bigframes.pandas as bpd
2014+
>>> bpd.options.display.progress_bar = None
2015+
2016+
>>> df = bpd.DataFrame({'a':[True, False, True], 'b':[-1, 0, 1]})
2017+
>>> ~df
2018+
a b
2019+
0 False 0
2020+
1 True -1
2021+
2 False -2
2022+
<BLANKLINE>
2023+
[3 rows x 2 columns]
2024+
2025+
Returns:
2026+
DataFrame: The result of inverting elements in the input.
2027+
"""
2028+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
2029+
20062030
def ne(self, other, axis: str | int = "columns") -> DataFrame:
20072031
"""
20082032
Get not equal to of DataFrame and other, element-wise (binary operator `ne`).

0 commit comments

Comments
 (0)