HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/computation/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/computation/__pycache__/expr.cpython-36.pyc
3

���hf^�@s�dZddlZddlmZmZddlmZddlZddlm	Z	m
Z
mZmZm
Z
mZddlZddljjZddlmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%ddl&m'Z'm(Z(ddl)m*Z*ddl+j,j-j.Z.ee/e0fee/e0fd	�d
d�Z1ee/e0fee/e0fd	�dd
�Z2ee/e0fee/e0fd	�dd�Z3dd�Z4dd�Z5e5e3e2e1e'�fe0e0d�dd�Z6dd�Z7e7e8�Z9e7e0�Z:e;e<dd�dd�e=e�D���Z>e>fdd�Z?e;e@dd�e>��ZAe?ejB�ZCe?ejD�ZEe?ejF�ZGe?ejH�ZIe?ejJ�ZKe?ejL�ZMe?ejN�ZOe?ejP�ZQe?ejR�ZSe?ejT�ZUe?ejV�ZWe?ejX�ZYe?ej�ZZe?ej[�Z\e;d d!d"g�Z]e;d#d$d%d&d'd(d)d*d+d,d-g�Z^eEeCBeWBeYBeZBe\BeIBe^Be]Z_eAe_e]BZ`e_e`@Zad.ea��Zbea�s�tceb��e0e	dLd0�d1d2�Zded3d4d5�Zeee0e	e
eege
eefd6�d7d8�Zfd9d:�Zgeed;�Zhd<d=�Ziefe_�eieh�Gd>d4�d4ejj���Zke;d?d@dAdBg�Zle;e#e"�Zmefe_elBeMe;d@dCdAdBdDg�B�GdEdF�dFek��Znefe_elBe;dGg�B�GdHdI�dIek��ZoGdJd"�d"�ZpeoendK�ZqdS)Mz
:func:`~pandas.eval` parsers.
�N)�partial�reduce)�	iskeyword)�Callable�Optional�Set�Tuple�Type�TypeVar)�
_LOCAL_TAG�BinOp�Constant�Div�FuncNode�Op�Term�UnaryOp�UndefinedVariableError�_arith_ops_syms�_bool_ops_syms�
_cmp_ops_syms�_mathops�_reductions�_unary_ops_syms�is_term)�clean_backtick_quoted_toks�tokenize_string)�Scope)�tok�returncCs|\}}||dkrdn|fS)a`
    Rewrite the assignment operator for PyTables expressions that use ``=``
    as a substitute for ``==``.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    tuple of int, str
        Either the input or token or the replacement values
    �=z==�)r�toknum�tokvalr!r!�>/tmp/pip-build-5_djhm0z/pandas/pandas/core/computation/expr.py�_rewrite_assign&sr%cCsF|\}}|tjkr>|dkr$tjdfS|dkr6tjdfS||fS||fS)aj
    Replace ``&`` with ``and`` and ``|`` with ``or`` so that bitwise
    precedence is changed to boolean precedence.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    tuple of int, str
        Either the input or token or the replacement values
    �&�and�|�or)�tokenize�OP�NAME)rr"r#r!r!r$�_replace_booleans9s


r-cCs,|\}}|tjkr$|dkr$tjtfS||fS)a(
    Replace local variables with a syntactically valid name.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    tuple of int, str
        Either the input or token or the replacement values

    Notes
    -----
    This is somewhat of a hack in that we rewrite a string such as ``'@a'`` as
    ``'__pd_eval_local_a'`` by telling the tokenizer that ``__pd_eval_local_``
    is a ``tokenize.OP`` and to replace the ``'@'`` symbol with it.
    �@)r*r+r)rr"r#r!r!r$�_replace_localsRs
r/cs��fdd�S)z
    Compose 2 callables.
    cs��||��S)Nr!)�args�kwargs)�f�gr!r$�<lambda>psz_compose2.<locals>.<lambda>r!)r2r3r!)r2r3r$�	_compose2lsr5cGst|�dkstd��tt|�S)z&
    Compose 2 or more callables.
    �z.At least 2 callables must be passed to compose)�len�AssertionErrorrr5)�funcsr!r!r$�_composessr:)�sourcercs,t��std��tj�fdd�t|�D��S)a�
    Compose a collection of tokenization functions.

    Parameters
    ----------
    source : str
        A Python source code string
    f : callable
        This takes a tuple of (toknum, tokval) as its argument and returns a
        tuple with the same structure but possibly different elements. Defaults
        to the composition of ``_rewrite_assign``, ``_replace_booleans``, and
        ``_replace_locals``.

    Returns
    -------
    str
        Valid Python source code

    Notes
    -----
    The `f` parameter can be any callable that takes *and* returns input of the
    form ``(toknum, tokval)``, where ``toknum`` is one of the constants from
    the ``tokenize`` module and ``tokval`` is a string.
    zf must be callablec3s|]}�|�VqdS)Nr!)�.0�x)r2r!r$�	<genexpr>�sz_preparse.<locals>.<genexpr>)�callabler8r*�
untokenizer)r;r2r!)r2r$�	_preparse{srAcs�fdd�S)zO
    Factory for a type checking function of type ``t`` or tuple of types.
    cst|j��S)N)�
isinstance�value)r=)�tr!r$r4�sz_is_type.<locals>.<lambda>r!)rDr!)rDr$�_is_type�srEcCst|t�ot|tj�S)N)rB�type�
issubclass�ast�AST)r=r!r!r$r4�sr4ccs|]}tt|�VqdS)N)�getattrrH)r<�noder!r!r$r>�sr>cs�fdd�|D�}t|�S)zE
    Filter out AST nodes that are subclasses of ``superclass``.
    c3s|]}t|��r|jVqdS)N)rG�__name__)r<rK)�
superclassr!r$r>�sz _filter_nodes.<locals>.<genexpr>)�	frozenset)rMZ	all_nodesZ
node_namesr!)rMr$�
_filter_nodes�srOcCs|jS)N)rL)r=r!r!r$r4�sZAssignZModule�ExprZYieldZGeneratorExpZIfExpZDictCompZSetComp�Repr�LambdarrIZIsZIsNotz$cannot both support and not support .)�	node_namercs�fdd�}|S)zV
    Return a function that raises a NotImplementedError with a passed node name.
    cstd��d���dS)N�'z' nodes are not implemented)�NotImplementedError)�selfr0r1)rSr!r$r2�sz _node_not_implemented.<locals>.fr!)rSr2r!)rSr$�_node_not_implemented�srW�_T�BaseExprVisitor)Zbound)�nodesrcs ttttd��fdd�}|S)z�
    Decorator to disallow certain nodes from parsing. Raises a
    NotImplementedError instead.

    Returns
    -------
    callable
    )�clsrcsFf|_x:�D]2}t|�}d|��}|j|f7_t|||�qW|S)N�visit_)�unsupported_nodesrW�setattr)r[rKZ
new_method�name)rZr!r$�
disalloweds

zdisallow.<locals>.disallowed)r	rX)rZr`r!)rZr$�disallows
	racs��fdd�}|S)zw
    Return a function to create an op class with its symbol already passed.

    Returns
    -------
    callable
    cst��f|�|�S)z�
        Return a partial function with an Op subclass with an operator already passed.

        Returns
        -------
        callable
        )r)rVrKr0r1)�op_class�	op_symbolr!r$r2!sz_op_maker.<locals>.fr!)rbrcr2r!)rbrcr$�	_op_makers	
rd)�binaryZunarycs�fdd�}|S)z9
    Decorator to add default implementation of ops.
    cstxn�j�D]b\}}t||�d��}t||�d��}x8|D]0}||}|dk	r8t||�}t|d|��|�q8Wq
W|S)NZ_opsZ
_op_nodes_mapr\)�itemsrJrdr^)r[Zop_attr_namerb�opsZops_map�opZop_nodeZmade_op)�
op_classesr!r$r26s

zadd_ops.<locals>.fr!)rir2r!)rir$�add_ops1srjc@steZdZUdZeZeeeZe	e
eZdZZ
eeee
��ZeZd[Zeeee��ZejejejejejejejejiZeedfefdd�Zdd�Z dd�Z!d d!�Z"d"d#�Z#d\d$d%�Z$d&d'�Z%d(d)�Z&d_d2d3�Z'd4d5�Z(d6d7�Z)d8d9�Z*d:d;�Z+d<d=�Z,d>d?�Z-d@dA�Z.dBdC�Z/dDdE�Z0e0Z1dFdG�Z2dHdI�Z3dJdK�Z4dLdM�Z5dNdO�Z6d`dPdQ�Z7dRdS�Z8dTdU�Z9dVdW�Z:dXdY�Z;dS)arYz�
    Custom ast walker. Parsers of other engines should subclass this class
    if necessary.

    Parameters
    ----------
    env : Scope
    engine : str
    parser : str
    preparser : callable
    �Gt�Lt�GtE�LtE�Eq�NotEq�In�NotIn�BitAnd�BitOr�And�Or�Add�Sub�MultN�Pow�FloorDiv�Mod�UAdd�USub�Invert�Not.cCs"||_||_||_||_d|_dS)N)�env�engine�parser�	preparser�assigner)rVr�r�r�r�r!r!r$�__init__{s
zBaseExprVisitor.__init__cKs�t|t�rl|j|�}ytjtj|��}WnBtk
rj}z&tdd�|j�D��rVd|_	|�WYdd}~XnXdt
|�j}t||�}||f|�S)Ncss|]}t|�VqdS)N)r)r<r=r!r!r$r>�sz(BaseExprVisitor.visit.<locals>.<genexpr>z4Python keyword not valid identifier in numexpr queryr\)
rB�strr�rHZfix_missing_locations�parse�SyntaxError�any�split�msgrFrLrJ)rVrKr1�clean�e�method�visitorr!r!r$�visit�s


zBaseExprVisitor.visitcKs.t|j�dkrtd��|jd}|j|f|�S)Nr6z#only a single expression is allowedr)r7�bodyr�r�)rVrKr1�exprr!r!r$�visit_Module�s
zBaseExprVisitor.visit_ModulecKs|j|jf|�S)N)r�rC)rVrKr1r!r!r$�
visit_Expr�szBaseExprVisitor.visit_ExprcCs�|j}t|�}t|�r�t|�r�||jkr�tt||f�\}}tt||f�\}}	|s\|s\|s\|	rh|j|�}|	r�|jj|j	g�}
|j
|
|j�}|r�|jj|j	g�}
|j
|
|j�}|j|�}||||fS)N)rhrFr�rewrite_map�map�_is_list�_is_strr��add_tmprC�	term_typer�)rVrK�left�rightZop_instanceZop_typeZ	left_listZ
right_listZleft_strZ	right_strr_rhr!r!r$�_rewrite_membership_op�s
z&BaseExprVisitor._rewrite_membership_opcCsR|dkr|j|jdd�}|dkr0|j|jdd�}|j|||�\}}}}||||fS)Nr�)�sider�)r�r�r�r�)rVrKr�r�rhrbr!r!r$�_maybe_transform_eq_ne�sz&BaseExprVisitor._maybe_transform_eq_necCs�tjtj�}|jrPt|d�rP|jrP|j|krP|jjtj|j��}|j	||j�}|jr�t|d�r�|jr�|j|kr�|jjtj|j��}|j	||j�}||fS)NrC)
�npZdtypeZfloat32Z	is_scalar�hasattr�return_typer�r�rCr�)rVr�r�Zf32r_r!r!r$�_maybe_downcast_constants�s



z)BaseExprVisitor._maybe_downcast_constantscCs|j|j|j|j|j|�S)N)�evaluater�r�r�r�)rV�binop�eval_in_pythonr!r!r$�_maybe_eval�szBaseExprVisitor._maybe_eval�in�not in�==�!=�<�>�<=�>=cCs�|||�}|jr2td|j�d|j�d|j�d���|jdkrl|jtkrRt|dd�s^t|dd�rl|j||j�S|j|kr�|j||�S|jdkr�t|dd�t	ks�t|dd�t	kr�|j|||�S|S)	Nz unsupported operand type(s) for z: 'z' and 'rTZpytablesZis_datetimeFr�)
Zhas_invalid_return_type�	TypeErrorrhrFr�rrJr��
binary_ops�object)rVrhrb�lhs�rhsr�Zmaybe_eval_in_python�resr!r!r$�_maybe_evaluate_binop�s 	
 



z%BaseExprVisitor._maybe_evaluate_binopcKs2|j|�\}}}}|j||�\}}|j||||�S)N)r�r�r�)rVrKr1rhrbr�r�r!r!r$�visit_BinOpszBaseExprVisitor.visit_BinOpcKsdd�S)NcSs
t||�S)N)r)r�r�r!r!r$r4
sz+BaseExprVisitor.visit_Div.<locals>.<lambda>r!)rVrKr1r!r!r$�	visit_DivszBaseExprVisitor.visit_DivcKs |j|j�}|j|j�}||�S)N)r�rh�operand)rVrKr1rhr�r!r!r$�
visit_UnaryOpszBaseExprVisitor.visit_UnaryOpcKs|j|j|jf|�S)N)r��idr�)rVrKr1r!r!r$�
visit_NameszBaseExprVisitor.visit_NamecKs|j|j|j�S)N)�
const_typerCr�)rVrKr1r!r!r$�visit_NameConstantsz"BaseExprVisitor.visit_NameConstantcKs|j|j|j�S)N)r��nr�)rVrKr1r!r!r$�	visit_NumszBaseExprVisitor.visit_NumcKs|j|j|j�S)N)r�r�r�)rVrKr1r!r!r$�visit_ConstantszBaseExprVisitor.visit_ConstantcKs|jj|j�}|j||j�S)N)r�r��sr�)rVrKr1r_r!r!r$�	visit_Str szBaseExprVisitor.visit_Strcs*�jj�fdd�|jD��}�j|�j�S)Ncsg|]}�j|��j��qSr!)r�r�)r<r�)rVr!r$�
<listcomp>%sz.BaseExprVisitor.visit_List.<locals>.<listcomp>)r�r�Zeltsr�)rVrKr1r_r!)rVr$�
visit_List$szBaseExprVisitor.visit_ListcKs|j|j�S)z
 df.index[4] )r�rC)rVrKr1r!r!r$�visit_Index*szBaseExprVisitor.visit_Indexc
Ks�ddl}|j|j�}|j|j�}|j||j|j|jd�}y|j|}Wn4tk
rz|j||j|j|jd�}||}YnX|jj	|�}	|j
|	|jd�S)Nr)Z
local_dictr�r�)r�)�pandasr�rC�slice�evalr�r�r��AttributeErrorr�r�)
rVrKr1�pdrCZslobj�result�vr�r_r!r!r$�visit_Subscript.szBaseExprVisitor.visit_SubscriptcKsZ|j}|dk	r|j|�j}|j}|dk	r4|j|�j}|j}|dk	rN|j|�j}t|||�S)z df.index[slice(4,6)] N)�lowerr�rC�upper�stepr�)rVrKr1r�r�r�r!r!r$�visit_SliceBszBaseExprVisitor.visit_SlicecKs�t|j�dkrtd��t|jdtj�s0td��|jjdkrDtd��y|j	|jdf|�}Wn t
k
r||jdj}YnXt|d|�|_
|j
dkr�td��|j	|jf|�S)	z�
        support a single assignment node, like

        c = a + b

        set the assigner at the top level, must be a Name node which
        might or might not exist in the resolvers

        r6z#can only assign a single expressionrz5left hand side of an assignment must be a single nameNz%cannot assign without a target objectr_z@left hand side of an assignment must be a single resolvable name)r7�targetsr�rBrH�Namer��target�
ValueErrorr�rr�rJr�rC)rVrKr1r�r!r!r$�visit_AssignPs

zBaseExprVisitor.visit_Assignc	Ks�|j}|j}|j}t|tj�r�|j|�j}y$t||�}|jj	|�}|j
||j�Stk
r~t|tj�rx|j
|krx|S�YnXtdt|�j����dS)NzInvalid Attribute context )�attrrC�ctxrBrHZLoadr�rJr�r�r�r�r�r�r�rFrL)	rVrKr1r�rCr��resolvedr�r_r!r!r$�visit_Attributens
zBaseExprVisitor.visit_Attributecsrt|jtj�r(|jjdkr(�j|j�}nft|jtj�s@td��nNy�j|j�}Wn<t	k
r�yt
|jj�}Wntk
r��YnXYnX|dkr�td|jj����t
|d�r�|j}t|t
�r��fdd�|jD�}|jr�td|j�d���||�S�fd	d�|jD�}xN|jD]D}t|tj��s8td
|jj�d���|j�r�j|j�j||j<�qW�j|||��j�SdS)N�__call__z"Only named functions are supportedzInvalid function call rCcsg|]}�j|��qSr!)r�)r<�arg)rVr!r$r��sz.BaseExprVisitor.visit_Call.<locals>.<listcomp>z
Function "z$" does not support keyword argumentscsg|]}�j|�j�qSr!)r�rC)r<r�)rVr!r$r��sz keyword error in function call 'rT)rB�funcrH�	Attributer�r�r�r�r�rrr�r�r�rCr0�keywordsr_�keywordr�r�r�)rVrKr�r1r�Znew_args�keyr!)rVr$�
visit_Call�s8


zBaseExprVisitor.visit_CallcCs|S)Nr!)rVrhr!r!r$�translate_In�szBaseExprVisitor.translate_IncKs�|j}|j}t|�dkrF|j|d�}tj||j|dd�}|j|�S|j}g}xDt||�D]6\}}	|jtj	|	g||j|�gd��}
|	}|j
|
�q\W|jtjtj�|d��S)Nr6r)rhr�r�)�comparatorsr�rg)rh�values)
rgr�r7r�rHrr�r��zipZCompare�append�BoolOpru)rVrKr1rg�compsrhr�r�r��compZnew_noder!r!r$�
visit_Compare�s
zBaseExprVisitor.visit_ComparecCst|ttf�r|S|j|�S)N)rBrrr�)rVZbopr!r!r$�_try_visit_binop�sz BaseExprVisitor._try_visit_binopcs��fdd�}�j}t||�S)Ncs<�j|�}�j|�}�j�||�\}}}}�j|�j||�S)N)r�r�r�rh)r=�yr�r�rhrb)rKrVr!r$r��s

z-BaseExprVisitor.visit_BoolOp.<locals>.visitor)r�r)rVrKr1r�Zoperandsr!)rKrVr$�visit_BoolOp�szBaseExprVisitor.visit_BoolOp)rkrlrmrnrorprqrrrsrtrurvrwrxryNrzr{r|)r}r~rr�)NN�r�r��r�r�r�r�r�r�)r�r�)N)<rL�
__module__�__qualname__�__doc__r
r�r	rr�rrrr�Zbinary_op_nodes�dictr�Zbinary_op_nodes_maprZ	unary_opsZunary_op_nodesZunary_op_nodes_maprHrorqrprrr�rr�r]rAr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�Zvisit_Tupler�r�r�r�r�r�r�r�r�r�r!r!r!r$rYDs|



!
.�Dictr�rqrrr�rcs0eZdZeeeeee�d�f�fdd�	Z	�Z
S)�PandasExprVisitor)r2cst�j||||�dS)N)�superr�)rVr�r�r�r�)�	__class__r!r$r��s
zPandasExprVisitor.__init__)rLr�r�rrAr:r/r-rr��
__classcell__r!r!)r�r$r��s
r�r�cs$eZdZdd�f�fdd�	Z�ZS)�PythonExprVisitorcCs|S)Nr!)r=r!r!r$r4�szPythonExprVisitor.<lambda>cst�j||||d�dS)N)r�)r�r�)rVr�r�r�r�)r�r!r$r��szPythonExprVisitor.__init__)rLr�r�r�r�r!r!)r�r$r��sr�c@s|eZdZUdZeeedeee	ee
d�dd�Zed	d
��Z
dd�Zed
�dd�Ze
d
�dd�Zdd�Zedd��ZdS)rPa
    Object encapsulating an expression.

    Parameters
    ----------
    expr : str
    engine : str, optional, default 'numexpr'
    parser : str, optional, default 'pandas'
    env : Scope, optional, default None
    level : int, optional, default 2
    �numexprr�Nr)r�r�r��levelcCsL||_|pt|dd�|_||_||_t||j|j|j�|_|j�|_dS)Nr6)r�)	r�rr�r�r��_parsers�_visitorr��terms)rVr�r�r�r�r�r!r!r$r�sz
Expr.__init__cCst|jdd�S)Nr�)rJr)rVr!r!r$r�sz
Expr.assignercCs|j|j�S)N)rr�)rVr!r!r$r�sz
Expr.__call__)rcCstj|j�S)N)�printingZpprint_thingr)rVr!r!r$�__repr__sz
Expr.__repr__cCs
t|j�S)N)r7r�)rVr!r!r$�__len__szExpr.__len__cCs|jj|j�S)z&
        Parse an expression.
        )rr�r�)rVr!r!r$r�"sz
Expr.parsecCs2t|j�rt|jjg�Stdd�tj|j�D��S)z1
        Get the names in an expression.
        css|]}|jVqdS)N)r_)r<Ztermr!r!r$r>/szExpr.names.<locals>.<genexpr>)rrrNr_�com�flatten)rVr!r!r$�names(s
z
Expr.names)r�r�Nr)rLr�r�r�rr�r�r�r�r�intr��propertyr�r�rrr�rr!r!r!r$rP�s
	)�pythonr�).N)rr�rH�	functoolsrrr�rr*Ztypingrrrrr	r
Znumpyr�Zpandas.core.common�core�commonrZpandas.core.computation.opsrrr
rrrrrrrrrrrrrZpandas.core.computation.parsingrrZpandas.core.computation.scoperZpandas.io.formats.printing�io�formatsrrr�r%r-r/r5r:rArE�listr�r�rN�filter�dirZ
_all_nodesrOr�Z_all_node_names�modZ
_mod_nodesZstmtZ_stmt_nodesr�Z_expr_nodesZexpr_contextZ_expr_context_nodesr�Z_slice_nodesZboolopZ
_boolop_nodes�operatorZ_operator_nodesZunaryopZ_unary_op_nodesZcmpopZ
_cmp_op_nodesZ
comprehensionZ_comprehension_nodesZ
excepthandlerZ_handler_nodesZ	argumentsZ_arguments_nodesZ_keyword_nodes�aliasZ_alias_nodesZ
_hacked_nodesZ_unsupported_expr_nodesZ_unsupported_nodesZ_base_supported_nodes�intersection�_msgr8rWrXrardZ_op_classesrjZNodeVisitorrYZ_python_not_supportedZ_numexpr_supported_callsr�r�rPr�r!r!r!r$�<module>s� H   
















&
"=