I would like to create a simply chain pipeline, I found this simple example:
"""
From https://stackoverflow.com/questions/33658355/piping-output-from-one-function-to-another-using-python-infix-syntax
"""
import collections
def pipe(original):
"""
"""
class PipeInto(object):
data = {'function': original}
def __init__(self, *args, **kwargs):
self.data['args'] = args
self.data['kwargs'] = kwargs
def __rrshift__(self, other):
return self.data['function'](
other,
*self.data['args'],
**self.data['kwargs']
)
def __call__(self):
return self.data['function'](
*self.data['args'],
**self.data['kwargs']
)
return PipeInto
@pipe
def select(df, *args):
cols = [x for x in args]
return df[cols]
While the df >> select('one')
works fine, the pipe= select(df, 'one')
returns an object which needs to be called. How can select(df, 'one')
work as a simple function call which returns the filtered DataFrame?