From 7fed05fc9c9a804f9415ba1475b857374b1ee47f Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:46:01 -0700 Subject: [PATCH 1/2] Fix binop with lhs numpy datetime scalar --- python/cudf/cudf/core/column/column.py | 4 ++-- python/cudf/cudf/tests/test_binops.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/python/cudf/cudf/core/column/column.py b/python/cudf/cudf/core/column/column.py index d2cd6e8ac8f..d2f9d208c77 100644 --- a/python/cudf/cudf/core/column/column.py +++ b/python/cudf/cudf/core/column/column.py @@ -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( diff --git a/python/cudf/cudf/tests/test_binops.py b/python/cudf/cudf/tests/test_binops.py index 949fa909b5b..9c8d8eb4506 100644 --- a/python/cudf/cudf/tests/test_binops.py +++ b/python/cudf/cudf/tests/test_binops.py @@ -3431,3 +3431,15 @@ def test_binop_eq_ne_index_series(data1, data2): expected = gi.to_pandas() != gs.to_pandas() assert_eq(expected, actual) + + +def test_binop_lhs_numpy_datetime_scalar(): + dt1 = np.datetime64("2024-03-04T18:24:35.67") + dt2 = np.datetime64("2024-03-04T18:24:35.670870310") + result = dt1 < cudf.Series([dt2]) + expected = dt1 < pd.Series([dt2]) + assert_eq(result, expected) + + result = dt2 < cudf.Series([dt1]) + expected = dt2 < pd.Series([dt1]) + assert_eq(result, expected) From d56fc807d79080b88f5c3009e95e341644d24196 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:51:50 -0700 Subject: [PATCH 2/2] test timedelta too --- python/cudf/cudf/tests/test_binops.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/python/cudf/cudf/tests/test_binops.py b/python/cudf/cudf/tests/test_binops.py index 9c8d8eb4506..71b6bbd688d 100644 --- a/python/cudf/cudf/tests/test_binops.py +++ b/python/cudf/cudf/tests/test_binops.py @@ -3433,13 +3433,14 @@ def test_binop_eq_ne_index_series(data1, data2): assert_eq(expected, actual) -def test_binop_lhs_numpy_datetime_scalar(): - dt1 = np.datetime64("2024-03-04T18:24:35.67") - dt2 = np.datetime64("2024-03-04T18:24:35.670870310") - result = dt1 < cudf.Series([dt2]) - expected = dt1 < pd.Series([dt2]) +@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 = dt2 < cudf.Series([dt1]) - expected = dt2 < pd.Series([dt1]) + result = slr2 < cudf.Series([slr1]) + expected = slr2 < pd.Series([slr1]) assert_eq(result, expected)