ibis icon indicating copy to clipboard operation
ibis copied to clipboard

Building Classes on top of ibis

Open ischlo opened this issue 10 months ago • 2 comments

Hello,

I am not sure whether this is an issue or a feature request, but here is a recent challenge I encountered building some workflows with ibis. I would like to build a class that inherits from Table, say MyTable, adding some properties and methods to it, and at the same time, I would like all the methods from Table, when applied to an instance of MyTable, to return a MyTable. Some simple examples of the desired behavior :

import ibis as ib
from ibis import _
import ibis.selectors as s
ib.options.interactive = True

class MyTable(ib.Table):
    pass

tab = MyTable(ib.memtable({"a" : [1,3,4,]}).op())
print(type(tab))  # <class '__main__.MyTable'>
type(tab.filter(_.a > 1)) # ibis.expr.types.relations.Table

Pandas provides this functionality through the _constructor method if it is defined in the children class, I was wondering whether there is an ibis way to do this. In pandas it would look like that :

class NewDF(pd.DataFrame): 

    @property
    def _constructor(self):
        return NewDF


ndf = NewDF({"a":[1,2,3,4]})

res = ndf.assign(b=lambda x: x["a"]*2)
type(res) # <class '__main__.NewDF'>

In a more advanced case, I would also like to extend the Backend class to my use case (automatically import some dependencies and add methods) and then have the methods from the Backend class, such as create_table return instance of MyTable.

Some python documentation suggests that in the parent class (Table), the methods return should contain something like self.__class__(expr), which would allow this behavior in inherited classes, but it does not seem that ibis does this. Could you recommend something ? Thank you in advance, Best,

ischlo avatar May 29 '25 16:05 ischlo

Doing this for table expressions wouldn't be too bad. It's not clear how column and scalar expressions would fit into this, that is, how would we make them subclassable?

cpcloud avatar Jun 02 '25 16:06 cpcloud

Hi, Thanks for your reply, I haven't considered this case, but it could have some interesting applications as well. However, in a first effort, having tables and connections subclassable with new methods acting on the original columns and scalars would be already a great feature in my opinion.

ischlo avatar Jun 12 '25 10:06 ischlo