Skip to content

Commit

Permalink
MAINT: Adapt numerical promotion to NumPy 2 and Pandas 2.2
Browse files Browse the repository at this point in the history
Pandas keeps using weak promotion even for strongly typed
"scalars" (i.e. 0-d objects).
This tries to (mostly) match that, but there may be better ways to
do it.  I am having difficulty to think of the best way though.
  • Loading branch information
seberg committed May 31, 2024
1 parent 9e5d611 commit 314d047
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions python/cudf/cudf/core/column/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,28 @@ def normalize_binop_value(
if isinstance(other, cudf.Scalar):
if self.dtype == other.dtype:
return other

# expensive device-host transfer just to
# adjust the dtype
other = other.value

# NumPy 2 needs a Python scalar to do weak promotion, but
# pandas forces weak promotion always
# TODO: We could use 0, 0.0, and 0j for promotion to avoid copies.
if other.dtype.kind in "ifc":
other = other.item()
elif not isinstance(other, (int, float, complex)):
# Go via NumPy to get the value
other = np.array(other)
if other.dtype.kind in "ifc":
other = other.item()

# Try and match pandas and hence numpy. Deduce the common
# dtype via the _value_ of other, and the dtype of self. TODO:
# When NEP50 is accepted, this might want changed or
# simplified.
# This is not at all simple:
# np.result_type(np.int64(0), np.uint8)
# dtype via the _value_ of other, and the dtype of self on NumPy 1.x
# with NumPy 2, we force weak promotion even for our/NumPy scalars
# to match pandas 2.2.
# Weak promotion is not at all simple:
# np.result_type(0, np.uint8)
# => np.uint8
# np.result_type(np.asarray([0], dtype=np.int64), np.uint8)
# => np.int64
Expand Down

0 comments on commit 314d047

Please sign in to comment.