Skip to content

Commit 52c8233

Browse files
fix: DataFrame string addition respects order (#1894)
1 parent 3d607cb commit 52c8233

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

‎bigframes/dataframe.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,14 +1046,17 @@ def radd(
10461046
) -> DataFrame:
10471047
# TODO(swast): Support fill_value parameter.
10481048
# TODO(swast): Support level parameter with MultiIndex.
1049-
return self.add(other, axis=axis)
1049+
return self._apply_binop(other, ops.add_op, axis=axis, reverse=True)
10501050

10511051
def __add__(self, other) -> DataFrame:
10521052
return self.add(other)
10531053

10541054
__add__.__doc__ = inspect.getdoc(vendored_pandas_frame.DataFrame.__add__)
10551055

1056-
__radd__ = __add__
1056+
def __radd__(self, other) -> DataFrame:
1057+
return self.radd(other)
1058+
1059+
__radd__.__doc__ = inspect.getdoc(vendored_pandas_frame.DataFrame.__radd__)
10571060

10581061
def sub(
10591062
self,

‎tests/system/small/test_dataframe.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,22 @@ def test_scalar_binop(scalars_dfs, op, other_scalar, reverse_operands):
25782578
assert_pandas_df_equal(bf_result, pd_result)
25792579

25802580

2581+
def test_dataframe_string_radd_const(scalars_dfs):
2582+
pytest.importorskip(
2583+
"pandas",
2584+
minversion="2.0.0",
2585+
reason="PyArrow string addition requires pandas 2.0+",
2586+
)
2587+
2588+
scalars_df, scalars_pandas_df = scalars_dfs
2589+
columns = ["string_col", "string_col"]
2590+
2591+
bf_result = ("prefix" + scalars_df[columns]).to_pandas()
2592+
pd_result = "prefix" + scalars_pandas_df[columns]
2593+
2594+
assert_pandas_df_equal(bf_result, pd_result)
2595+
2596+
25812597
@pytest.mark.parametrize(("other_scalar"), [1, -2])
25822598
def test_mod(scalars_dfs, other_scalar):
25832599
# Zero case excluded as pandas produces 0 result for Int64 inputs rather than NA/NaN.

‎third_party/bigframes_vendored/pandas/core/frame.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,6 +3067,20 @@ def radd(self, other, axis: str | int = "columns") -> DataFrame:
30673067
"""
30683068
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
30693069

3070+
def __radd__(self, other) -> DataFrame:
3071+
"""Get addition of other and DataFrame, element-wise (binary operator `+`).
3072+
3073+
Equivalent to ``DataFrame.radd(other)``.
3074+
3075+
Args:
3076+
other (float, int, or Series):
3077+
Any single or multiple element data structure, or list-like object.
3078+
3079+
Returns:
3080+
bigframes.pandas.DataFrame: DataFrame result of the arithmetic operation.
3081+
"""
3082+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
3083+
30703084
def sub(self, other, axis: str | int = "columns") -> DataFrame:
30713085
"""Get subtraction of DataFrame and other, element-wise (binary operator `-`).
30723086

0 commit comments

Comments
 (0)