1

Getting this error while converting array to cuPy array: TypeError: Implicit conversion to a host NumPy array via array is not allowed, To explicitly construct a GPU array, consider using cupy.asarray(...) To explicitly construct a host array, consider using .to_array()

image

    '''
      import cudf
      import cuml
      import cupy

      hospital_data=cudf.read_csv('nin-health-facilities.csv')
      cupy_lat = cupy.asarray(hospital_data['latitude'])
      cupy_long =cupy.asarray(hospital_data['longitude'])
    '''

Error:

TypeError                                 Traceback (most recent call last)
<ipython-input-33-999a5fd5e344> in <module>()
      4 
      5 hospital_data=cudf.read_csv('nin-health-facilities.csv')
----> 6 cupy_lat = cupy.asarray(hospital_data['latitude'])
      7 cupy_long =cupy.asarray(hospital_data['longitude'])

1 frames
cupy/_core/core.pyx in cupy._core.core.array()

cupy/_core/core.pyx in cupy._core.core.array()

cupy/_core/core.pyx in cupy._core.core._send_object_to_gpu()

/usr/local/lib/python3.7/site-packages/cudf/core/frame.py in __array__(self, dtype)
   1649     def __array__(self, dtype=None):
   1650         raise TypeError(
-> 1651             "Implicit conversion to a host NumPy array via __array__ is not "
   1652             "allowed, To explicitly construct a GPU array, consider using "
   1653             "cupy.asarray(...)\nTo explicitly construct a "

TypeError: Implicit conversion to a host NumPy array via __array__ is not allowed, To explicitly construct a GPU array, consider using cupy.asarray(...)
To explicitly construct a host array, consider using .to_array()
talonmies
  • 70,661
  • 34
  • 192
  • 269
heisenberg_88
  • 13
  • 1
  • 4

1 Answers1

3

Converting a cuDF series into a CuPy array with cupy.asarray(series) requires CuPy-compatible data types. You may want to double check your Series is int, float, or bool typed, rather than string, decimal, list, or struct typed.

import cudf
import cupy
​
s = cudf.Series([0,1,2])
cupy.asarray(s)
array([0, 1, 2])

The intention of the implicit conversion error message is to indicate you cannot pass a GPU object via the array protocol to a function that normally requires a CPU object. In this case, it's likely a red herring.

import cudf
import cupy
​
s = cudf.Series(["a","b","c"])
cupy.asarray(s)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_53245/4047643563.py in <module>
      3 
      4 s = cudf.Series(["a","b","c"])
----> 5 cupy.asarray(s)
...
TypeError: Implicit conversion to a host NumPy array via __array__ is not allowed, To explicitly construct a GPU array, consider using cupy.asarray(...)
To explicitly construct a host array, consider using .to_array()
Nick Becker
  • 4,059
  • 13
  • 19