8000 fix: DataFrame string addition respects order by TrevorBergeron · Pull Request #1894 · googleapis/python-bigquery-dataframes · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions third_party/bigframes_vendored/pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 `-`).

Expand Down
0