Skip to content

Instantly share code, notes, and snippets.

@rschroll
Last active February 7, 2020 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rschroll/c51449f5554e317a62fc83dd336673d5 to your computer and use it in GitHub Desktop.
Save rschroll/c51449f5554e317a62fc83dd336673d5 to your computer and use it in GitHub Desktop.
Pandas Pipeline
import pandas as pd
from inspect import isroutine, signature
from functools import wraps
__all__ = ['pipeline']
if '_all' not in globals(): # Don't redefine this on reloads; otherwise we load the new all
_all = all # This will get overridden when we define methods from pd.DataFrame
def is_pandas(obj):
return isinstance(obj, pd.core.base.PandasObject)
class PandasPipeline():
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __rrshift__(self, df):
if is_pandas(df):
return self.func(df, *self.args, **self.kw)
return NotImplemented
def pipeline(n_df=1):
def decorator(func):
@wraps(func)
def decorated(*args, **kw):
if len(args) >= n_df and _all(is_pandas(arg) for arg in args[:n_df]):
try:
signature(func).bind(*args, **kw)
except TypeError:
# Didn't have the right number of arguments, so try a pipeline
pass
else:
return func(*args, **kw)
return PandasPipeline(func, *args, **kw)
return decorated
if isroutine(n_df):
func = n_df
n_df = 1 # Note that this changes n_df in decorated.
return decorator(func)
return decorator
def _load_methods():
def method_pipeline(func):
@wraps(func)
def decorated(*args, **kw):
return PandasPipeline(func, *args, **kw)
return decorated
global_dict = globals()
for method in dir(pd.DataFrame):
if 'a' <= method[0] <= 'z':
global_dict[method] = method_pipeline(getattr(pd.DataFrame, method))
__all__.append(method)
_load_methods()
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment