diff --git a/bigframes/dataframe.py b/bigframes/dataframe.py index 1821c5791b..9f42e70923 100644 --- a/bigframes/dataframe.py +++ b/bigframes/dataframe.py @@ -1046,14 +1046,17 @@ def radd( ) -> DataFrame: # TODO(swast): Support fill_value parameter. # TODO(swast): Support level parameter with MultiIndex. - return self.add(other, axis=axis) + return self._apply_binop(other, ops.add_op, axis=axis, reverse=True) def __add__(self, other) -> DataFrame: return self.add(other) __add__.__doc__ = inspect.getdoc(vendored_pandas_frame.DataFrame.__add__) - __radd__ = __add__ + def __radd__(self, other) -> DataFrame: + return self.radd(other) + + __radd__.__doc__ = inspect.getdoc(vendored_pandas_frame.DataFrame.__radd__) def sub( self, diff --git a/tests/system/small/test_dataframe.py b/tests/system/small/test_dataframe.py index 70f551ef6f..a2b78fcfef 100644 --- a/tests/system/small/test_dataframe.py +++ b/tests/system/small/test_dataframe.py @@ -2578,6 +2578,22 @@ def test_scalar_binop(scalars_dfs, op, other_scalar, reverse_operands): assert_pandas_df_equal(bf_result, pd_result) +def test_dataframe_string_radd_const(scalars_dfs): + pytest.importorskip( + "pandas", + minversion="2.0.0", + reason="PyArrow string addition requires pandas 2.0+", + ) + + scalars_df, scalars_pandas_df = scalars_dfs + columns = ["string_col", "string_col"] + + bf_result = ("prefix" + scalars_df[columns]).to_pandas() + pd_result = "prefix" + scalars_pandas_df[columns] + + assert_pandas_df_equal(bf_result, pd_result) + + @pytest.mark.parametrize(("other_scalar"), [1, -2]) def test_mod(scalars_dfs, other_scalar): # Zero case excluded as pandas produces 0 result for Int64 inputs rather than NA/NaN. diff --git a/third_party/bigframes_vendored/pandas/core/frame.py b/third_party/bigframes_vendored/pandas/core/frame.py index 61abca74db..731e9a24eb 100644 --- a/third_party/bigframes_vendored/pandas/core/frame.py +++ b/third_party/bigframes_vendored/pandas/core/frame.py @@ -3067,6 +3067,20 @@ def radd(self, other, axis: str | int = "columns") -> DataFrame: """ raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) + def __radd__(self, other) -> DataFrame: + """Get addition of other and DataFrame, element-wise (binary operator `+`). + + Equivalent to ``DataFrame.radd(other)``. + + Args: + other (float, int, or Series): + Any single or multiple element data structure, or list-like object. + + Returns: + bigframes.pandas.DataFrame: DataFrame result of the arithmetic operation. + """ + raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) + def sub(self, other, axis: str | int = "columns") -> DataFrame: """Get subtraction of DataFrame and other, element-wise (binary operator `-`).