Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- import pandas as pd
- class SinkInto(object):
- def __init__(self, function, *args, **kwargs):
- self.args = args
- self.kwargs = kwargs
- self.function = function
- def __rrshift__(self, other):
- return self.function(other, *self.args, **self.kwargs)
- def __repr__(self):
- return "<SinkInto {} args={} kwargs={}>".format(
- self.function,
- self.args,
- self.kwargs
- )
- def pipe(original):
- class PipeInto(object):
- function = original
- def __init__(self, *args, **kwargs):
- self.args = args
- self.kwargs = kwargs
- def __rrshift__(self, other):
- return self.function(other, *self.args, **self.kwargs)
- def __repr__(self):
- return "<SinkInto {} args={} kwargs={}>".format(
- self.function,
- self.args,
- self.kwargs
- )
- return PipeInto
- @pipe
- def select(df, *args):
- cols = [x for x in args]
- return df[cols]
- @pipe
- def rename(df, **kwargs):
- for name, value in kwargs.items():
- df = df.rename(columns={'%s' % name: '%s' % value})
- return df
- df = pd.DataFrame({'one' : [1., 2., 3., 4., 4.],
- 'two' : [4., 3., 2., 1., 3.]})
- df >> select('one') >> rename(one='first')
- # Output
- Traceback (most recent call last):
- File "decorator_test.py", line 58, in <module>
- df >> select('one') >> rename(one='first')
- File "decorator_test.py", line 31, in __rrshift__
- return self.function(other, *self.args, **self.kwargs)
- File "decorator_test.py", line 45, in select
- return df[cols]
- TypeError: 'PipeInto' object has no attribute '__getitem__'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement