<s> """<STR_LIT>""" <EOL> import operator as op <EOL> from functools import partial <EOL> from datetime import datetime <EOL> import numpy as np <EOL> import pandas as pd <EOL> from pandas . compat import PY3 , string_types , text_type <EOL> import pandas . core . common as com <EOL> from pandas . formats . printing import pprint_thing , pprint_thing_encoded <EOL> import pandas . lib as lib <EOL> from pandas . core . base import StringMixin <EOL> from pandas . computation . common import _ensure_decoded , _result_type_many <EOL> from pandas . computation . scope import _DEFAULT_GLOBALS <EOL> _reductions = '<STR_LIT>' , '<STR_LIT>' <EOL> _unary_math_ops = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> _binary_math_ops = ( '<STR_LIT>' , ) <EOL> _mathops = _unary_math_ops + _binary_math_ops <EOL> _LOCAL_TAG = '<STR_LIT>' <EOL> class UndefinedVariableError ( NameError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , is_local ) : <EOL> if is_local : <EOL> msg = '<STR_LIT>' <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> super ( UndefinedVariableError , self ) . __init__ ( msg . format ( name ) ) <EOL> class Term ( StringMixin ) : <EOL> def __new__ ( cls , name , env , side = None , encoding = None ) : <EOL> klass = Constant if not isinstance ( name , string_types ) else cls <EOL> supr_new = super ( Term , klass ) . __new__ <EOL> return supr_new ( klass ) <EOL> def __init__ ( self , name , env , side = None , encoding = None ) : <EOL> self . _name = name <EOL> self . env = env <EOL> self . side = side <EOL> tname = text_type ( name ) <EOL> self . is_local = ( tname . startswith ( _LOCAL_TAG ) or <EOL> tname in _DEFAULT_GLOBALS ) <EOL> self . _value = self . _resolve_name ( ) <EOL> self . encoding = encoding <EOL> @ property <EOL> def local_name ( self ) : <EOL> return self . name . replace ( _LOCAL_TAG , '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return pprint_thing ( self . name ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return self . value <EOL> def evaluate ( self , * args , ** kwargs ) : <EOL> return self <EOL> def _resolve_name ( self ) : <EOL> res = self . env . resolve ( self . local_name , is_local = self . is_local ) <EOL> self . update ( res ) <EOL> if hasattr ( res , '<STR_LIT>' ) and res . ndim > <NUM_LIT:2> : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return res <EOL> def update ( self , value ) : <EOL> """<STR_LIT>""" <EOL> key = self . name <EOL> if isinstance ( key , string_types ) : <EOL> self . env . swapkey ( self . local_name , key , new_value = value ) <EOL> self . value = value <EOL> @ property <EOL> def isscalar ( self ) : <EOL> return lib . isscalar ( self . _value ) <EOL> @ property <EOL> def type ( self ) : <EOL> try : <EOL> return self . _value . values . dtype <EOL> except AttributeError : <EOL> try : <EOL> return self . _value . dtype <EOL> except AttributeError : <EOL> return type ( self . _value ) <EOL> return_type = type <EOL> @ property <EOL> def raw ( self ) : <EOL> return pprint_thing ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . __class__ . __name__ , self . name , <EOL> self . type ) ) <EOL> @ property <EOL> def is_datetime ( self ) : <EOL> try : <EOL> t = self . type . type <EOL> except AttributeError : <EOL> t = self . type <EOL> return issubclass ( t , ( datetime , np . datetime64 ) ) <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _value <EOL> @ value . setter <EOL> def value ( self , new_value ) : <EOL> self . _value = new_value <EOL> @ property <EOL> def name ( self ) : <EOL> return self . _name <EOL> @ name . setter <EOL> def name ( self , new_name ) : <EOL> self . _name = new_name <EOL> @ property <EOL> def ndim ( self ) : <EOL> return self . _value . ndim <EOL> class Constant ( Term ) : <EOL> def __init__ ( self , value , env , side = None , encoding = None ) : <EOL> super ( Constant , self ) . __init__ ( value , env , side = side , <EOL> encoding = encoding ) <EOL> def _resolve_name ( self ) : <EOL> return self . _name <EOL> @ property <EOL> def name ( self ) : <EOL> return self . value <EOL> _bool_op_map = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:&>' , '<STR_LIT>' : '<STR_LIT:|>' } <EOL> class Op ( StringMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , op , operands , * args , ** kwargs ) : <EOL> self . op = _bool_op_map . get ( op , op ) <EOL> self . operands = operands <EOL> self . encoding = kwargs . get ( '<STR_LIT>' , None ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . operands ) <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> parened = ( '<STR_LIT>' . format ( pprint_thing ( opr ) ) <EOL> for opr in self . operands ) <EOL> return pprint_thing ( '<STR_LIT>' . format ( self . op ) . join ( parened ) ) <EOL> @ property <EOL> def return_type ( self ) : <EOL> if self . op in ( _cmp_ops_syms + _bool_ops_syms ) : <EOL> return np . bool_ <EOL> return _result_type_many ( * ( term . type for term in com . flatten ( self ) ) ) <EOL> @ property <EOL> def has_invalid_return_type ( self ) : <EOL> types = self . operand_types <EOL> obj_dtype_set = frozenset ( [ np . dtype ( '<STR_LIT:object>' ) ] ) <EOL> return self . return_type == object and types - obj_dtype_set <EOL> @ property <EOL> def operand_types ( self ) : <EOL> return frozenset ( term . type for term in com . flatten ( self ) ) <EOL> @ property <EOL> def isscalar ( self ) : <EOL> return all ( operand . isscalar for operand in self . operands ) <EOL> @ property <EOL> def is_datetime ( self ) : <EOL> try : <EOL> t = self . return_type . type <EOL> except AttributeError : <EOL> t = self . return_type <EOL> return issubclass ( t , ( datetime , np . datetime64 ) ) <EOL> def _in ( x , y ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return x . isin ( y ) <EOL> except AttributeError : <EOL> if com . is_list_like ( x ) : <EOL> try : <EOL> return y . isin ( x ) <EOL> except AttributeError : <EOL> pass <EOL> return x in y <EOL> def _not_in ( x , y ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return ~ x . isin ( y ) <EOL> except AttributeError : <EOL> if com . is_list_like ( x ) : <EOL> try : <EOL> return ~ y . isin ( x ) <EOL> except AttributeError : <EOL> pass <EOL> return x not in y <EOL> _cmp_ops_syms = '<STR_LIT:>>' , '<STR_LIT:<>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> _cmp_ops_funcs = op . gt , op . lt , op . ge , op . le , op . eq , op . ne , _in , _not_in <EOL> _cmp_ops_dict = dict ( zip ( _cmp_ops_syms , _cmp_ops_funcs ) ) <EOL> _bool_ops_syms = '<STR_LIT:&>' , '<STR_LIT:|>' , '<STR_LIT>' , '<STR_LIT>' <EOL> _bool_ops_funcs = op . and_ , op . or_ , op . and_ , op . or_ <EOL> _bool_ops_dict = dict ( zip ( _bool_ops_syms , _bool_ops_funcs ) ) <EOL> _arith_ops_syms = '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:*>' , '<STR_LIT:/>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%>' <EOL> _arith_ops_funcs = ( op . add , op . sub , op . mul , op . truediv if PY3 else op . div , <EOL> op . pow , op . floordiv , op . mod ) <EOL> _arith_ops_dict = dict ( zip ( _arith_ops_syms , _arith_ops_funcs ) ) <EOL> _special_case_arith_ops_syms = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%>' <EOL> _special_case_arith_ops_funcs = op . pow , op . floordiv , op . mod <EOL> _special_case_arith_ops_dict = dict ( zip ( _special_case_arith_ops_syms , <EOL> _special_case_arith_ops_funcs ) ) <EOL> _binary_ops_dict = { } <EOL> for d in ( _cmp_ops_dict , _bool_ops_dict , _arith_ops_dict ) : <EOL> _binary_ops_dict . update ( d ) <EOL> def _cast_inplace ( terms , dtype ) : <EOL> """<STR_LIT>""" <EOL> dt = np . dtype ( dtype ) <EOL> for term in terms : <EOL> try : <EOL> new_value = term . value . astype ( dt ) <EOL> except AttributeError : <EOL> new_value = dt . type ( term . value ) <EOL> term . update ( new_value ) <EOL> def is_term ( obj ) : <EOL> return isinstance ( obj , Term ) <EOL> class BinOp ( Op ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , op , lhs , rhs , ** kwargs ) : <EOL> super ( BinOp , self ) . __init__ ( op , ( lhs , rhs ) ) <EOL> self . lhs = lhs <EOL> self . rhs = rhs <EOL> self . _disallow_scalar_only_bool_ops ( ) <EOL> self . convert_values ( ) <EOL> try : <EOL> self . func = _binary_ops_dict [ op ] <EOL> except KeyError : <EOL> keys = list ( _binary_ops_dict . keys ( ) ) <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( op , keys ) ) <EOL> def __call__ ( self , env ) : <EOL> """<STR_LIT>""" <EOL> if self . op == '<STR_LIT:/>' and env . scope [ '<STR_LIT>' ] : <EOL> self . func = op . truediv <EOL> left = self . lhs ( env ) <EOL> right = self . rhs ( env ) <EOL> return self . func ( left , right ) <EOL> def evaluate ( self , env , engine , parser , term_type , eval_in_python ) : <EOL> """<STR_LIT>""" <EOL> if engine == '<STR_LIT>' : <EOL> res = self ( env ) <EOL> else : <EOL> left = self . lhs . evaluate ( env , engine = engine , parser = parser , <EOL> term_type = term_type , <EOL> eval_in_python = eval_in_python ) <EOL> right = self . rhs . evaluate ( env , engine = engine , parser = parser , <EOL> term_type = term_type , <EOL> eval_in_python = eval_in_python ) <EOL> if self . op in eval_in_python : <EOL> res = self . func ( left . value , right . value ) <EOL> else : <EOL> res = pd . eval ( self , local_dict = env , engine = engine , <EOL> parser = parser ) <EOL> name = env . add_tmp ( res ) <EOL> return term_type ( name , env = env ) <EOL> def convert_values ( self ) : <EOL> """<STR_LIT>""" <EOL> def stringify ( value ) : <EOL> if self . encoding is not None : <EOL> encoder = partial ( pprint_thing_encoded , <EOL> encoding = self . encoding ) <EOL> else : <EOL> encoder = pprint_thing <EOL> return encoder ( value ) <EOL> lhs , rhs = self . lhs , self . rhs <EOL> if is_term ( lhs ) and lhs . is_datetime and is_term ( rhs ) and rhs . isscalar : <EOL> v = rhs . value <EOL> if isinstance ( v , ( int , float ) ) : <EOL> v = stringify ( v ) <EOL> v = pd . Timestamp ( _ensure_decoded ( v ) ) <EOL> if v . tz is not None : <EOL> v = v . tz_convert ( '<STR_LIT>' ) <EOL> self . rhs . update ( v ) <EOL> if is_term ( rhs ) and rhs . is_datetime and is_term ( lhs ) and lhs . isscalar : <EOL> v = lhs . value <EOL> if isinstance ( v , ( int , float ) ) : <EOL> v = stringify ( v ) <EOL> v = pd . Timestamp ( _ensure_decoded ( v ) ) <EOL> if v . tz is not None : <EOL> v = v . tz_convert ( '<STR_LIT>' ) <EOL> self . lhs . update ( v ) <EOL> def _disallow_scalar_only_bool_ops ( self ) : <EOL> if ( ( self . lhs . isscalar or self . rhs . isscalar ) and <EOL> self . op in _bool_ops_dict and <EOL> ( not ( issubclass ( self . rhs . return_type , ( bool , np . bool_ ) ) and <EOL> issubclass ( self . lhs . return_type , ( bool , np . bool_ ) ) ) ) ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def isnumeric ( dtype ) : <EOL> return issubclass ( np . dtype ( dtype ) . type , np . number ) <EOL> class Div ( BinOp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lhs , rhs , truediv , * args , ** kwargs ) : <EOL> super ( Div , self ) . __init__ ( '<STR_LIT:/>' , lhs , rhs , * args , ** kwargs ) <EOL> if not isnumeric ( lhs . return_type ) or not isnumeric ( rhs . return_type ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . op , <EOL> lhs . return_type , <EOL> rhs . return_type ) ) <EOL> if truediv or PY3 : <EOL> _cast_inplace ( com . flatten ( self ) , np . float_ ) <EOL> _unary_ops_syms = '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT>' , '<STR_LIT>' <EOL> _unary_ops_funcs = op . pos , op . neg , op . invert , op . invert <EOL> _unary_ops_dict = dict ( zip ( _unary_ops_syms , _unary_ops_funcs ) ) <EOL> class UnaryOp ( Op ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , op , operand ) : <EOL> super ( UnaryOp , self ) . __init__ ( op , ( operand , ) ) <EOL> self . operand = operand <EOL> try : <EOL> self . func = _unary_ops_dict [ op ] <EOL> except KeyError : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( op , _unary_ops_syms ) ) <EOL> def __call__ ( self , env ) : <EOL> operand = self . operand ( env ) <EOL> return self . func ( operand ) <EOL> def __unicode__ ( self ) : <EOL> return pprint_thing ( '<STR_LIT>' . format ( self . op , self . operand ) ) <EOL> @ property <EOL> def return_type ( self ) : <EOL> operand = self . operand <EOL> if operand . return_type == np . dtype ( '<STR_LIT:bool>' ) : <EOL> return np . dtype ( '<STR_LIT:bool>' ) <EOL> if ( isinstance ( operand , Op ) and <EOL> ( operand . op in _cmp_ops_dict or operand . op in _bool_ops_dict ) ) : <EOL> return np . dtype ( '<STR_LIT:bool>' ) <EOL> return np . dtype ( '<STR_LIT:int>' ) <EOL> class MathCall ( Op ) : <EOL> def __init__ ( self , func , args ) : <EOL> super ( MathCall , self ) . __init__ ( func . name , args ) <EOL> self . func = func <EOL> def __call__ ( self , env ) : <EOL> operands = [ op ( env ) for op in self . operands ] <EOL> return self . func . func ( * operands ) <EOL> def __unicode__ ( self ) : <EOL> operands = map ( str , self . operands ) <EOL> return pprint_thing ( '<STR_LIT>' . format ( self . op , '<STR_LIT:U+002C>' . join ( operands ) ) ) <EOL> class FuncNode ( object ) : <EOL> def __init__ ( self , name ) : <EOL> if name not in _mathops : <EOL> raise ValueError ( <EOL> "<STR_LIT>" . format ( name ) ) <EOL> self . name = name <EOL> self . func = getattr ( np , name ) <EOL> def __call__ ( self , * args ) : <EOL> return MathCall ( self , args ) </s>
