B
    W0d?                 @  s~  d Z ddlmZ ddlmZ ddlmZ ddlZddlmZm	Z	 ddl
ZddlmZ ddlmZmZ ddlm  mZ dd	lmZmZ dd
lmZ ddlmZmZ dZdZdZee Z dZ!G dd de"Z#G dd dZ$G dd de$Z%ddddZ&G dd dZ'dd Z(dd Z)d Z*ej+ej,ej-ej.ej/ej0e(e)fZ1e2e3e*e1Z4d!Z5ej6ej7ej6ej7fZ8e2e3e5e8Z9d"Z:ej;ej<ej=ej>ej?ej@ejAfZBe2e3e:eBZCd#ZDej?ej@ejAfZEe2e3eDeEZFi ZGxe4e9eCfD ]ZHeGIeH qW d$d% ZJd&d'd(d)ZKG d*d+ d+e'ZLd&d'd,d-ZMG d.d/ d/eLZNd0ZOejPejQejRejRfZSe2e3eOeSZTG d1d2 d2e'ZUG d3d4 d4e'ZVG d5d6 d6ZWdS )7z
Operator classes for eval.
    )annotations)datetime)partialN)CallableIterable)	Timestamp)is_list_like	is_scalar)ensure_decodedresult_type_many)DEFAULT_GLOBALS)pprint_thingpprint_thing_encoded)sumprod)sincosexplogexpm1log1psqrtsinhcoshtanhZarcsinZarccosZarctanZarccoshZarcsinhZarctanhabslog10floorceil)Zarctan2Z__pd_eval_local_c                  s*   e Zd ZdZdddd fddZ  ZS )	UndefinedVariableErrorz1
    NameError subclass for local variables.
    Nstrzbool | None)nameis_localc               s8   t | d}|rd| }n
d| }t | d S )Nz is not definedzlocal variable zname )reprsuper__init__)selfr!   r"   Zbase_msgmsg)	__class__ M/var/www/html/venv/lib/python3.7/site-packages/pandas/core/computation/ops.pyr%   I   s
    
zUndefinedVariableError.__init__)N)__name__
__module____qualname____doc__r%   __classcell__r)   r)   )r(   r*   r   D   s   r   c                  s   e Zd ZU d& fdd	Zded< d'ddZedd	d
dZdd	ddZdd Z	dd Z
dd Zdd Zedd	ddZedd ZeZedd	ddZedd	ddZedd Zejd d Zed!d" Zed#d	d$d%Z  ZS )(TermNc               s&   t |tstn| }tt|j}||S )N)
isinstancer    Constantr$   r0   __new__)clsr!   envsideencodingklassZsupr_new)r(   r)   r*   r3   S   s    zTerm.__new__boolr"   c             C  sB   || _ || _|| _t|}|tp*|tk| _|  | _	|| _
d S )N)_namer5   r6   r    
startswith	LOCAL_TAGr   r"   _resolve_name_valuer7   )r&   r!   r5   r6   r7   Ztnamer)   r)   r*   r%   [   s    
zTerm.__init__r    )returnc             C  s   | j tdS )N )r!   replacer<   )r&   r)   r)   r*   
local_namee   s    zTerm.local_namec             C  s
   t | jS )N)r   r!   )r&   r)   r)   r*   __repr__i   s    zTerm.__repr__c             O  s   | j S )N)value)r&   argskwargsr)   r)   r*   __call__l   s    zTerm.__call__c             O  s   | S )Nr)   )r&   rE   rF   r)   r)   r*   evaluateo   s    zTerm.evaluatec             C  s>   | j j| j| jd}| | t|dr:|jdkr:td|S )N)r"   ndim   z?N-dimensional objects, where N > 2, are not supported with eval)r5   resolverB   r"   updatehasattrrI   NotImplementedError)r&   resr)   r)   r*   r=   r   s    
zTerm._resolve_namec             C  s.   | j }t|tr$| jj| j||d || _dS )z
        search order for local (i.e., @variable) variables:

        scope, key_variable
        [('locals', 'local_name'),
         ('globals', 'local_name'),
         ('locals', 'key'),
         ('globals', 'key')]
        )	new_valueN)r!   r1   r    r5   ZswapkeyrB   rD   )r&   rD   keyr)   r)   r*   rL   |   s    

zTerm.updatec             C  s
   t | jS )N)r	   r>   )r&   r)   r)   r*   r	      s    zTerm.is_scalarc             C  sH   y
| j jjS  tk
rB   y| j jS  tk
r<   t| j S X Y nX d S )N)r>   valuesdtypeAttributeErrortype)r&   r)   r)   r*   rU      s    
z	Term.typec             C  s$   t | j dt| j d| j  dS )Nz(name=z, type=))rU   r+   r#   r!   )r&   r)   r)   r*   raw   s    zTerm.rawc             C  s8   y| j j }W n tk
r&   | j }Y nX t|ttjfS )N)rU   rT   
issubclassr   np
datetime64)r&   tr)   r)   r*   is_datetime   s
    zTerm.is_datetimec             C  s   | j S )N)r>   )r&   r)   r)   r*   rD      s    z
Term.valuec             C  s
   || _ d S )N)r>   )r&   rP   r)   r)   r*   rD      s    c             C  s   | j S )N)r:   )r&   r)   r)   r*   r!      s    z	Term.nameintc             C  s   | j jS )N)r>   rI   )r&   r)   r)   r*   rI      s    z	Term.ndim)NN)NN)r+   r,   r-   r3   __annotations__r%   propertyrB   rC   rG   rH   r=   rL   r	   rU   return_typerW   r\   rD   setterr!   rI   r/   r)   r)   )r(   r*   r0   R   s$   



	r0   c                  s@   e Zd Zd fdd	Zdd Zedd Zdd	d
dZ  ZS )r2   Nc               s   t  j||||d d S )N)r6   r7   )r$   r%   )r&   rD   r5   r6   r7   )r(   r)   r*   r%      s    zConstant.__init__c             C  s   | j S )N)r:   )r&   r)   r)   r*   r=      s    zConstant._resolve_namec             C  s   | j S )N)rD   )r&   r)   r)   r*   r!      s    zConstant.namer    )r?   c             C  s
   t | jS )N)r#   r!   )r&   r)   r)   r*   rC      s    zConstant.__repr__)NN)	r+   r,   r-   r%   r=   r_   r!   rC   r/   r)   r)   )r(   r*   r2      s   r2   ~&|)notandorc               @  s   e Zd ZU dZded< ddddddZd	d
 ZddddZedd Z	eddddZ
edd ZeddddZeddddZdS )Opz.
    Hold an operator of arbitrary arity.
    r    opNzIterable[Term | Op])ri   operandsc             C  s   t ||| _|| _|| _d S )N)_bool_op_mapgetri   rj   r7   )r&   ri   rj   r7   r)   r)   r*   r%      s    zOp.__init__c             C  s
   t | jS )N)iterrj   )r&   r)   r)   r*   __iter__   s    zOp.__iter__)r?   c             C  s(   dd | j D }td| j d|S )zW
        Print a generic n-ary operator and its operands using infix notation.
        c             s  s   | ]}d t | dV  qdS )(rV   N)r   ).0Zoprr)   r)   r*   	<genexpr>   s    zOp.__repr__.<locals>.<genexpr> )rj   r   ri   join)r&   Zparenedr)   r)   r*   rC      s    zOp.__repr__c             C  s,   | j tt krtjS tdd t| D  S )Nc             s  s   | ]}|j V  qd S )N)rU   )rp   termr)   r)   r*   rq      s    z!Op.return_type.<locals>.<genexpr>)ri   CMP_OPS_SYMSBOOL_OPS_SYMSrY   bool_r   comflatten)r&   r)   r)   r*   r`      s    zOp.return_typer9   c             C  s(   | j }ttdg}| jtko&|| S )Nobject)operand_types	frozensetrY   rS   r`   rz   )r&   typesZobj_dtype_setr)   r)   r*   has_invalid_return_type   s    zOp.has_invalid_return_typec             C  s   t dd t| D S )Nc             s  s   | ]}|j V  qd S )N)rU   )rp   rt   r)   r)   r*   rq      s    z#Op.operand_types.<locals>.<genexpr>)r|   rx   ry   )r&   r)   r)   r*   r{      s    zOp.operand_typesc             C  s   t dd | jD S )Nc             s  s   | ]}|j V  qd S )N)r	   )rp   operandr)   r)   r*   rq      s    zOp.is_scalar.<locals>.<genexpr>)allrj   )r&   r)   r)   r*   r	      s    zOp.is_scalarc             C  s8   y| j j}W n tk
r&   | j }Y nX t|ttjfS )N)r`   rU   rT   rX   r   rY   rZ   )r&   r[   r)   r)   r*   r\      s
    zOp.is_datetime)N)r+   r,   r-   r.   r^   r%   rn   rC   r_   r`   r~   r{   r	   r\   r)   r)   r)   r*   rh      s   
rh   c             C  sP   y
|  |S  tk
rJ   t| rBy
| | S  tk
r@   Y nX | |kS X dS )z`
    Compute the vectorized membership of ``x in y`` if possible, otherwise
    use Python.
    N)isinrT   r   )xyr)   r)   r*   _in	  s    

r   c             C  sT   y|  | S  tk
rN   t| rFy| |  S  tk
rD   Y nX | |kS X dS )zd
    Compute the vectorized membership of ``x not in y`` if possible,
    otherwise use Python.
    N)r   rT   r   )r   r   r)   r)   r*   _not_in  s    r   )><z>=z<=z==z!=inznot in)rc   rd   rf   rg   )+-*/z**z//%)z**z//r   c          	   C  sd   t |}xT| D ]L}|j|kr qy|j|}W n  tk
rP   ||j}Y nX || qW dS )a$  
    Cast an expression inplace.

    Parameters
    ----------
    terms : Op
        The expression that should cast.
    acceptable_dtypes : list of acceptable numpy.dtype
        Will not cast if term's dtype in this list.
    dtype : str or numpy.dtype
        The dtype to cast to.
    N)rY   rS   rU   rD   ZastyperT   rL   )Ztermsacceptable_dtypesrS   dtrt   rP   r)   r)   r*   _cast_inplaceR  s    


r   r9   )r?   c             C  s
   t | tS )N)r1   r0   )objr)   r)   r*   is_termk  s    r   c                  sL   e Zd ZdZdd fddZdd Zddd	d
Zdd Zdd Z  Z	S )BinOpz
    Hold a binary operator and its operands.

    Parameters
    ----------
    op : str
    lhs : Term or Op
    rhs : Term or Op
    r    )ri   c          
     s   t  |||f || _|| _|   |   yt| | _W nH tk
r } z*t	t
 }tdt| d| |W d d }~X Y nX d S )NzInvalid binary operator z, valid operators are )r$   r%   lhsrhs_disallow_scalar_only_bool_opsconvert_values_binary_ops_dictfuncKeyErrorlistkeys
ValueErrorr#   )r&   ri   r   r   errr   )r(   r)   r*   r%   z  s    zBinOp.__init__c             C  s    |  |}| |}| ||S )z
        Recursively evaluate an expression in Python space.

        Parameters
        ----------
        env : Scope

        Returns
        -------
        object
            The result of an evaluated expression.
        )r   r   r   )r&   r5   leftrightr)   r)   r*   rG     s    

zBinOp.__call__)enginec             C  s   |dkr| |}nd| j j|||||d}| jj|||||d}| j|krZ| |j|j}nddlm}	 |	| |||d}||}
||
|dS )al  
        Evaluate a binary operation *before* being passed to the engine.

        Parameters
        ----------
        env : Scope
        engine : str
        parser : str
        term_type : type
        eval_in_python : list

        Returns
        -------
        term_type
            The "pre-evaluated" expression as an instance of ``term_type``
        python)r   parser	term_typeeval_in_pythonr   )eval)Z
local_dictr   r   )r5   )	r   rH   r   ri   r   rD   Zpandas.core.computation.evalr   Zadd_tmp)r&   r5   r   r   r   r   rO   r   r   r   r!   r)   r)   r*   rH     s(    


zBinOp.evaluatec               s    fdd} j  j }}t|r~|jr~t|r~|jr~|j}t|ttfrR||}t	t
|}|jdk	rr|d} j| t|r|jrt|r|jr|j}t|ttfr||}t	t
|}|jdk	r|d} j | dS )zK
        Convert datetimes to a comparable value in an expression.
        c               s&    j d k	rtt j d}nt}|| S )N)r7   )r7   r   r   r   )rD   encoder)r&   r)   r*   	stringify  s    
z'BinOp.convert_values.<locals>.stringifyNUTC)r   r   r   r\   r	   rD   r1   r]   floatr   r
   tzZ
tz_convertrL   )r&   r   r   r   vr)   )r&   r*   r     s$    



zBinOp.convert_valuesc             C  sr   | j }| j}|j}t|d|}|j}t|d|}|js<|jrn| jtkrnt|tt	j
frft|tt	j
fsntdd S )NrU   z$cannot evaluate scalar only bool ops)r   r   r`   getattrr	   ri   _bool_ops_dictrX   r9   rY   rw   rN   )r&   r   r   Zrhs_rtZlhs_rtr)   r)   r*   r     s    
z$BinOp._disallow_scalar_only_bool_ops)
r+   r,   r-   r.   r%   rG   rH   r   r   r/   r)   r)   )r(   r*   r   o  s   	1!r   c             C  s   t t| jtjS )N)rX   rY   rS   rU   number)rS   r)   r)   r*   	isnumeric  s    r   c                  s    e Zd ZdZ fddZ  ZS )Divz
    Div operator to special case casting.

    Parameters
    ----------
    lhs, rhs : Term or Op
        The Terms or Ops in the ``/`` expression.
    c               sj   t  d|| t|jr$t|jsFtd| j d|j d|j dtjtjg}t	t
| |tj d S )Nr   z unsupported operand type(s) for z: 'z' and '')r$   r%   r   r`   	TypeErrorri   rY   Zfloat32Zfloat_r   rx   ry   )r&   r   r   r   )r(   r)   r*   r%     s     zDiv.__init__)r+   r,   r-   r.   r%   r/   r)   r)   )r(   r*   r     s   r   )r   r   rb   re   c                  sN   e Zd ZdZdd fddZdd Zddd	d
ZeddddZ  Z	S )UnaryOpaK  
    Hold a unary operator and its operands.

    Parameters
    ----------
    op : str
        The token used to represent the operator.
    operand : Term or Op
        The Term or Op operand to the operator.

    Raises
    ------
    ValueError
        * If no function associated with the passed operator token is found.
    r    )ri   c          
     sf   t  ||f || _yt| | _W n< tk
r` } ztdt| dt |W d d }~X Y nX d S )NzInvalid unary operator z, valid operators are )	r$   r%   r   _unary_ops_dictr   r   r   r#   UNARY_OPS_SYMS)r&   ri   r   r   )r(   r)   r*   r%   9  s    zUnaryOp.__init__c             C  s   |  |}| |S )N)r   r   )r&   r5   r   r)   r)   r*   rG   E  s    
zUnaryOp.__call__)r?   c             C  s   t | j d| j dS )Nro   rV   )r   ri   r   )r&   r)   r)   r*   rC   I  s    zUnaryOp.__repr__znp.dtypec             C  sR   | j }|jtdkr tdS t|trH|jtks>|jtkrHtdS tdS )Nr9   r]   )	r   r`   rY   rS   r1   rh   ri   _cmp_ops_dictr   )r&   r   r)   r)   r*   r`   L  s    


zUnaryOp.return_type)
r+   r,   r-   r.   r%   rG   rC   r_   r`   r/   r)   r)   )r(   r*   r   (  s
   r   c                  s2   e Zd Z fddZdd ZddddZ  ZS )	MathCallc               s   t  |j| || _d S )N)r$   r%   r!   r   )r&   r   rE   )r(   r)   r*   r%   Y  s    zMathCall.__init__c          	     s8    fdd| j D }tjdd | jj| S Q R X d S )Nc               s   g | ]}| qS r)   r)   )rp   ri   )r5   r)   r*   
<listcomp>_  s    z%MathCall.__call__.<locals>.<listcomp>ignore)r   )rj   rY   Zerrstater   )r&   r5   rj   r)   )r5   r*   rG   ]  s    zMathCall.__call__r    )r?   c             C  s(   t t| j}t| j dd| dS )Nro   ,rV   )mapr    rj   r   ri   rs   )r&   rj   r)   r)   r*   rC   c  s    zMathCall.__repr__)r+   r,   r-   r%   rG   rC   r/   r)   r)   )r(   r*   r   X  s   r   c               @  s"   e Zd ZddddZdd ZdS )FuncNoder    )r!   c             C  s.   |t krtd| d|| _tt|| _d S )N"z" is not a supported function)MATHOPSr   r!   r   rY   r   )r&   r!   r)   r)   r*   r%   i  s    zFuncNode.__init__c             G  s
   t | |S )N)r   )r&   rE   r)   r)   r*   rG   o  s    zFuncNode.__call__N)r+   r,   r-   r%   rG   r)   r)   r)   r*   r   h  s   r   )Xr.   
__future__r   r   	functoolsr   operatortypingr   r   numpyrY   Zpandas._libs.tslibsr   Zpandas.core.dtypes.commonr   r	   Zpandas.core.commoncorecommonrx   Zpandas.core.computation.commonr
   r   Zpandas.core.computation.scoper   Zpandas.io.formats.printingr   r   Z
REDUCTIONSZ_unary_math_opsZ_binary_math_opsr   r<   	NameErrorr   r0   r2   rk   rh   r   r   ru   gtltgeleeqneZ_cmp_ops_funcsdictzipr   rv   and_or_Z_bool_ops_funcsr   ZARITH_OPS_SYMSaddsubmultruedivpowfloordivmodZ_arith_ops_funcsZ_arith_ops_dictZSPECIAL_CASE_ARITH_OPS_SYMSZ_special_case_arith_ops_funcsZ_special_case_arith_ops_dictr   drL   r   r   r   r   r   r   posneginvertZ_unary_ops_funcsr   r   r   r   r)   r)   r)   r*   <module>   s   m6 0