TypeError: First element of field tuple is neither a tuple nor str, with cuDF.DataFrame.apply(func,axis)

Viewed 32

I am trying to apply histogram row-wise using the apply function but getting an error. Below code is the implementation

def f(row):
    return np.histogram(row, bins=5,range=(1,10))

import torch
import cudf as df
torch.manual_seed(1)
bins = 3
feature = torch.randint(1, 50, (3, 4,7))
selected_feature = feature[:,:,3].float().cuda() # read all 4 elements in the 2nd dimension
feature_df = df.DataFrame(selected_feature)
print(feature_df.head(5))
hist_feature = feature_df.apply(f,axis=1)

Below is the output. Would really appreciate it if someone can guide me on how to apply custom functions on dataframes

0     1     2     3
0   4.0  47.0  11.0   3.0
1   6.0  12.0   9.0  17.0
2  46.0  45.0  31.0  23.0

File /opt/conda/lib/python3.8/site-packages/nvtx/nvtx.py:101, in annotate.__call__.<locals>.inner(*args, **kwargs)
     [98](file:///opt/conda/lib/python3.8/site-packages/nvtx/nvtx.py?line=97) @wraps(func)
     [99](file:///opt/conda/lib/python3.8/site-packages/nvtx/nvtx.py?line=98) def inner(*args, **kwargs):
    [100](file:///opt/conda/lib/python3.8/site-packages/nvtx/nvtx.py?line=99)     libnvtx_push_range(self.attributes, self.domain.handle)
--> [101](file:///opt/conda/lib/python3.8/site-packages/nvtx/nvtx.py?line=100)     result = func(*args, **kwargs)
    [102](file:///opt/conda/lib/python3.8/site-packages/nvtx/nvtx.py?line=101)     libnvtx_pop_range(self.domain.handle)
    [103](file:///opt/conda/lib/python3.8/site-packages/nvtx/nvtx.py?line=102)     return result

File /opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py:144, in get_udf_return_type(frame, func, args)
    [129](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=128) """
    [130](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=129) Get the return type of a masked UDF for a given set of argument dtypes. It
    [131](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=130) is assumed that the function consumes a dictionary whose keys are strings
   (...)
    [139](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=138) compute the answer for that row.
    [140](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=139) """
    [142](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=141) # present a row containing all fields to the UDF and try and compile
    [143](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=142) row_type = get_frame_row_type(
--> [144](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=143)     np.dtype(list(all_dtypes_from_frame(frame).items()))
    [145](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=144) )
    [146](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=145) compile_sig = (row_type, *(typeof(arg) for arg in args))
    [148](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=147) # Get the return type. The PTX is also returned by compile_udf, but is not
    [149](file:///opt/conda/lib/python3.8/site-packages/cudf/core/udf/pipeline.py?line=148) # needed here.

TypeError: First element of field tuple is neither a tuple nor str
1 Answers

Unfortunately, I have come to know from the rapidsai/cudf github community that it doesn't support the use of np.histogram inside user-defined functions yet. Also, the apply in cuDF isn't as generic as apply in pandas.

Related