pythonpy-datatable

Filter python datatable using `datatable.FExpr` with custom objects


Let's say I populate a Frame, where one column contains some custom object, like this:


class CustomObject:
    def __init__(self,x=None,label=None):
        self.x=x
        self.label=label


s1 = CustomObject(x="late", label="A")
s2 = CustomObject(x="pending", label="B")
s3 = CustomObject(x="closed", label="B")

from datatable import Frame, f
dt = Frame(a = [1,2,3], b = [s1,s2,s3], types=(int,"obj64"))

I would like to select rows from the frame where the label attribute takes on some value, say "B". I want to do this:

dt[f.b.label=="B",:]

AttributeError: 'datatable.FExpr' object has no attribute 'label'

but it doesn't work, and I understand why it doesn't work.

What is the best alternative approach?

This what I have tried, and it works. Is there a better way?

b_objects = dt["b"].to_list()[0]
dt[[x for x in range(dt.nrows) if b_objects[x].label=="B"],:]

Solution

  • This is somewhat simpler

    dt[[b.label=="B" for b in dt["b"].to_list()[0]],:]