Skip to content

Commit

Permalink
Fix binop with LHS numpy datetimelike scalar (#17226)
Browse files Browse the repository at this point in the history
closes #17087

For binops, cudf tries to convert a 0D numpy array to a numpy scalar via `.dtype.type(value)`, but `.dtype.type` requires other parameters if its a `numpy.datetime64` or `numpy.timedelta64`. Indexing via `[()]` will perform this conversion correctly.

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #17226
  • Loading branch information
mroeschke authored Oct 31, 2024
1 parent a83debb commit 6055393
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ def _wrap_binop_normalization(self, other):
if cudf.utils.utils.is_na_like(other):
return cudf.Scalar(other, dtype=self.dtype)
if isinstance(other, np.ndarray) and other.ndim == 0:
# Try and maintain the dtype
other = other.dtype.type(other.item())
# Return numpy scalar
other = other[()]
return self.normalize_binop_value(other)

def _scatter_by_slice(
Expand Down
13 changes: 13 additions & 0 deletions python/cudf/cudf/tests/test_binops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3431,3 +3431,16 @@ def test_binop_eq_ne_index_series(data1, data2):
expected = gi.to_pandas() != gs.to_pandas()

assert_eq(expected, actual)


@pytest.mark.parametrize("scalar", [np.datetime64, np.timedelta64])
def test_binop_lhs_numpy_datetimelike_scalar(scalar):
slr1 = scalar(1, "ms")
slr2 = scalar(1, "ns")
result = slr1 < cudf.Series([slr2])
expected = slr1 < pd.Series([slr2])
assert_eq(result, expected)

result = slr2 < cudf.Series([slr1])
expected = slr2 < pd.Series([slr1])
assert_eq(result, expected)

0 comments on commit 6055393

Please sign in to comment.