Python datatable - collection in a column

Viewed 78

Can python datatable have any collection as datatype for a column?

import datatable as dt

dt_with_collection = dt.Frame(A=range(5), B=[1,5,7,2,3], c=[(1,2), (3,4), (5,6), (7,8), (9,10)])
print(dt_with_collection)
TypeError: Cannot create column from a python list: element at index 0 is of type <class 'tuple'>. If you meant to create a column of type obj64, then you must request this type explicitly

I get this error when trying to do so.

1 Answers

You can specify the types in types

dt_with_collection = dt.Frame(
    A=range(5),
    B=[1,5,7,2,3],
    c=[(1,2), (3,4), (5,6), (7,8), (9,10)], 
    types=[dt.Type.int32, dt.Type.int32,dt.Type.obj64]
)

Its not straightforward to work with columns of type obj64, although other SO experts will be more knowledgeable about this than me. You could do something like this:

c_codes = (1,2,5,6)
data_with_collection["inTarget"] = np.array([set(x[0]).issubset(c_codes) for x in data_with_collection["c"].to_numpy()])
 
Related