Search code examples
seriesrapidscudf

How to use cudf.Series.applymap()?


Can someone please provide a few examples of how to use the applymap method on a cuDF Series?

Below is copied from the docs and here is a link to the documentation.

applymap(self, udf, out_dtype=None)
  Apply a elemenwise function to transform the values in the Column.

  The user function is expected to take one argument and return the result, which will be stored to the output Series. The function cannot reference globals except for other simple scalar objects.

  Parameters
    udf : function
      Wrapped by `numba.cuda.jit` for call on the GPU as a device function.

    out_dtype : numpy.dtype; optional
      The dtype for use in the output. By default, the result will have the same dtype as the source.

  Returns
    result: Series
      The mask and index are preserved.

Solution

  • The API docs have an example of using applymap as of the latest release https://rapidsai.github.io/projects/cudf/en/0.9.0/10min.html#Applymap:

    def add_ten(num):
        return num + 10
    
    print(df['a'].applymap(add_ten))
    

    In general you pass a function expecting to operate against a scalar and that function is JIT compiled into vectorized GPU code and executed on the GPU against each element in the Series.