Skip to content

Commit a894ca0

Browse files
authored
Add (implicit) handling for torch tensors in is_scalar (#14623)
PyTorch tensors advertise that they support the number API, and hence answer "True" to the question pd.api.types.is_scalar(torch_tensor). This trips up some of our data ingest, since in as_index we check if the input is a scalar (and raise) before handing off to as_column. To handle this, if we get True back from pandas' is_scalar call, additionally check that the object has an empty shape attribute (if it exists). See also: - pytorch/pytorch#99646 - pandas-dev/pandas#52701 Authors: - Lawrence Mitchell (https://github.com/wence-) Approvers: - Ashwin Srinath (https://github.com/shwina) URL: #14623
1 parent 420dc5d commit a894ca0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

python/cudf/cudf/api/types.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,17 @@ def is_scalar(val):
135135
cudf._lib.scalar.DeviceScalar,
136136
cudf.core.tools.datetimes.DateOffset,
137137
),
138-
) or pd_types.is_scalar(val)
138+
) or (
139+
pd_types.is_scalar(val)
140+
# Pytorch tensors advertise that they support the number
141+
# protocol, and therefore return True for PyNumber_Check even
142+
# when they have a shape. So, if we get through this, let's
143+
# additionally check that if they have a shape property that
144+
# it is empty.
145+
# See https://github.com/pytorch/pytorch/issues/99646
146+
# and https://github.com/pandas-dev/pandas/issues/52701
147+
and len(getattr(val, "shape", ())) == 0
148+
)
139149

140150

141151
def _is_scalar_or_zero_d_array(val):

0 commit comments

Comments
 (0)