Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparison between Datetime/Timedelta columns and NULL scalars #7504

Merged
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
2 changes: 2 additions & 0 deletions python/cudf/cudf/core/column/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def normalize_binop_value(self, other: DatetimeLikeScalar) -> ScalarLike:
return cudf.Scalar(None, dtype=other.dtype)

return cudf.Scalar(other)
elif other is None:
return cudf.Scalar(other, dtype=self.dtype)
else:
raise TypeError(f"cannot normalize {type(other)}")

Expand Down
2 changes: 2 additions & 0 deletions python/cudf/cudf/core/column/timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ def normalize_binop_value(self, other) -> BinaryOperand:
return cudf.Scalar(other)
elif np.isscalar(other):
return cudf.Scalar(other)
elif other is None:
return cudf.Scalar(other, dtype=self.dtype)
else:
raise TypeError(f"cannot normalize {type(other)}")

Expand Down
45 changes: 45 additions & 0 deletions python/cudf/cudf/tests/test_binops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,51 @@ def decimal_series(input, dtype):
utils.assert_eq(expect, got)


@pytest.mark.parametrize(
"dtype",
[
"uint8",
"uint16",
"uint32",
"uint64",
"int8",
"int16",
"int32",
"int64",
"float32",
"float64",
"str",
"datetime64[ns]",
"datetime64[us]",
"datetime64[ms]",
"datetime64[s]",
"timedelta64[ns]",
"timedelta64[us]",
"timedelta64[ms]",
"timedelta64[s]",
],
)
@pytest.mark.parametrize("null_scalar", [None, cudf.NA, np.datetime64("NaT")])
@pytest.mark.parametrize("cmpop", _cmpops)
def test_column_null_scalar_comparison(dtype, null_scalar, cmpop):
# This test is meant to validate that comparing
# a series of any dtype with a null scalar produces
# a new series where all the elements are <NA>.

if isinstance(null_scalar, np.datetime64):
if np.dtype(dtype).kind not in "mM":
pytest.skip()
null_scalar = null_scalar.astype(dtype)

dtype = np.dtype(dtype)

data = [1, 2, 3, 4, 5]
sr = cudf.Series(data, dtype=dtype)
result = cmpop(sr, null_scalar)

assert result.isnull().all()


@pytest.mark.parametrize("fn", ["eq", "ne", "lt", "gt", "le", "ge"])
def test_equality_ops_index_mismatch(fn):
a = cudf.Series(
Expand Down