def ifelse(cond, then, else_): """ Returns a Series that is the same length as cond, picking elements from then and else_ based on the truth of cond. If then or else_ are instances of Intention, then they will only be evaluated when needed.""" def outfunc(df): cond_out = maybe_eval(df, cond) then_out = cond_eval(cond_out.any(), then, df) else_out = cond_eval(not cond_out.all(), else_, df) return pd.Series(tiled_where(cond_out, then_out, else_out)) return Intention(outfunc) if any_intention( cond, then, else_) else pd.Series(tiled_where(cond, then, else_))
from itertools import chain from operator import and_, or_, not_ from dfply import make_symbolic, Intention from dfply.base import _set_magic_method from collections import OrderedDict from toolz import curry setattr(Intention, '__invert__', _set_magic_method('__invert__')) X = Intention() EXAMPLE_VALUES = {int: 2, str: 'a', float: 2.5} def merge(d1, d2): return dict(chain(d1.items(), d2.items())) def extract_type(type_tuple): return tuple(t for t in type_tuple if t is not type(None))[0] #Automatic type inference? def extract_types(row): return { c: extract_type(f.type) for c, f in row._precord_fields.items() if c not in ('columns', 'col_types') }
def case_when(*args): return Intention(eval_and_case_when(args)) if any_intention( args) else ifelse_and_coalesce(args)
def coalesce(*args): return Intention(arg_eval_and_combine(args)) if any_intention( args) else combine_all(args)
Input: A statement Output: n rows of a statement """ s = maybe_wrap(stmt) return s.limit(num) def _original(stmt): return stmt.original if isinstance(stmt, Alias) else stmt def table_dict(s): return {t.name: t for t in _original(s).froms} D = Intention(function=table_dict) """ Access a table using D intention of table name ex: D['heroes'] Access a column using D['heroes'].c.gender """ T = Intention(function=lambda s: _original(s).froms[0].c) """ References the current table with T intention """ def column_dict(stmt): s = maybe_wrap(stmt) if len(s.froms) == 1: return {col.name: col for col in s.froms[0].c} elif len(s.froms) > 1: