Skip to content

Commit

Permalink
Fix Series/Dataframe Mixed Arithmetic (#7491)
Browse files Browse the repository at this point in the history
Fixes #7385

Authors:
  - @brandon-b-miller

Approvers:
  - GALI PREM SAGAR (@galipremsagar)
  - Michael Wang (@isVoid)

URL: #7491
  • Loading branch information
brandon-b-miller authored Mar 18, 2021
1 parent 3349764 commit 168c489
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
8 changes: 2 additions & 6 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,11 +1518,7 @@ def fallback(col, fn):
else:
if col not in df_cols:
r_opr = other_cols[col]
l_opr = Series(
column_empty(
len(self), masked=True, dtype=other.dtype
)
)
l_opr = Series(as_column(np.nan, length=len(self)))
if col not in other_cols_keys:
r_opr = None
l_opr = self[col]
Expand Down Expand Up @@ -2198,7 +2194,7 @@ def rpow(self, other, axis="columns", level=None, fill_value=None):
return self._apply_op("rpow", other, fill_value)

def __rpow__(self, other):
return self._apply_op("__pow__", other)
return self._apply_op("__rpow__", other)

def floordiv(self, other, axis="columns", level=None, fill_value=None):
"""
Expand Down
4 changes: 1 addition & 3 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,9 +1501,7 @@ def _binaryop(
If ``reflect`` is ``True``, swap the order of the operands.
"""
if isinstance(other, cudf.DataFrame):
# TODO: fn is not the same as arg expected by _apply_op
# e.g. for fn = 'and', _apply_op equivalent is '__and__'
return other._apply_op(self, fn)
return NotImplemented

result_name = utils.get_result_name(self, other)
if isinstance(other, Series):
Expand Down
42 changes: 21 additions & 21 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4996,13 +4996,13 @@ def test_cov_nans():
@pytest.mark.parametrize(
"gsr",
[
cudf.Series([1, 2, 3]),
cudf.Series([1, 2, 3], index=["a", "b", "c"]),
cudf.Series([1, 2, 3], index=["a", "b", "d"]),
cudf.Series([1, 2], index=["a", "b"]),
cudf.Series([1, 2, 3], index=cudf.core.index.RangeIndex(0, 3)),
cudf.Series([4, 2, 3]),
cudf.Series([4, 2, 3], index=["a", "b", "c"]),
cudf.Series([4, 2, 3], index=["a", "b", "d"]),
cudf.Series([4, 2], index=["a", "b"]),
cudf.Series([4, 2, 3], index=cudf.core.index.RangeIndex(0, 3)),
pytest.param(
cudf.Series([1, 2, 3, 4, 5], index=["a", "b", "d", "0", "12"]),
cudf.Series([4, 2, 3, 4, 5], index=["a", "b", "d", "0", "12"]),
marks=pytest.mark.xfail,
),
],
Expand All @@ -5017,32 +5017,32 @@ def test_cov_nans():
operator.truediv,
operator.mod,
operator.pow,
# comparison ops will temporarily XFAIL
# see PR https://github.com/rapidsai/cudf/pull/7491
pytest.param(operator.eq, marks=pytest.mark.xfail()),
pytest.param(operator.lt, marks=pytest.mark.xfail()),
pytest.param(operator.le, marks=pytest.mark.xfail()),
pytest.param(operator.gt, marks=pytest.mark.xfail()),
pytest.param(operator.ge, marks=pytest.mark.xfail()),
pytest.param(operator.ne, marks=pytest.mark.xfail()),
operator.eq,
operator.lt,
operator.le,
operator.gt,
operator.ge,
operator.ne,
],
)
def test_df_sr_binop(gsr, colnames, op):
data = [[0, 2, 5], [3, None, 5], [6, 7, np.nan]]
data = [[3.0, 2.0, 5.0], [3.0, None, 5.0], [6.0, 7.0, np.nan]]
data = dict(zip(colnames, data))

gsr = gsr.astype("float64")

gdf = cudf.DataFrame(data)
pdf = pd.DataFrame.from_dict(data)
pdf = gdf.to_pandas(nullable=True)

psr = gsr.to_pandas()
psr = gsr.to_pandas(nullable=True)

expect = op(pdf, psr)
got = op(gdf, gsr)
assert_eq(expect.astype(float), got.astype(float))
got = op(gdf, gsr).to_pandas(nullable=True)
assert_eq(expect, got, check_dtype=False)

expect = op(psr, pdf)
got = op(psr, pdf)
assert_eq(expect.astype(float), got.astype(float))
got = op(gsr, gdf).to_pandas(nullable=True)
assert_eq(expect, got, check_dtype=False)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 168c489

Please sign in to comment.