U
    f/e?                     @  sz  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 ZGe4e9eCfD ]ZHeGIeH qd$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__ ?/tmp/pip-unpacked-wheel-tiezk1ph/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$   r2   __new__)clsr!   envsideencodingklassZsupr_newr(   r*   r+   r6   S   s    zTerm.__new__boolr"   c                 C  sB   || _ || _|| _t|}|tp*|tk| _|  | _	|| _
d S r3   )_namer8   r9   r    
startswith	LOCAL_TAGr   r"   _resolve_name_valuer:   )r&   r!   r8   r9   r:   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 r3   )r   r!   rF   r*   r*   r+   __repr__i   s    zTerm.__repr__c                 O  s   | j S r3   valuer&   argskwargsr*   r*   r+   __call__l   s    zTerm.__call__c                 O  s   | S r3   r*   rK   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)r8   resolverG   r"   updatehasattrrP   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!   r4   r    r8   ZswapkeyrG   rJ   )r&   rJ   keyr*   r*   r+   rS   |   s    

zTerm.updatec                 C  s
   t | jS r3   )r	   rA   rF   r*   r*   r+   r	      s    zTerm.is_scalarc                 C  sX   z| j jjW S  tk
rR   z| j jW  Y S  tk
rL   t| j  Y  Y S X Y nX d S r3   )rA   valuesdtypeAttributeErrortyperF   r*   r*   r+   r\      s    z	Term.typec                 C  s$   t | j dt| j d| j  dS )Nz(name=z, type=))r\   r-   r#   r!   rF   r*   r*   r+   raw   s    zTerm.rawc                 C  s8   z| j j }W n tk
r&   | j }Y nX t|ttjfS r3   )r\   r[   
issubclassr   np
datetime64r&   tr*   r*   r+   is_datetime   s
    zTerm.is_datetimec                 C  s   | j S r3   rA   rF   r*   r*   r+   rJ      s    z
Term.valuec                 C  s
   || _ d S r3   re   )r&   rW   r*   r*   r+   rJ      s    c                 C  s   | j S r3   r=   rF   r*   r*   r+   r!      s    z	Term.nameintc                 C  s   | j jS r3   )rA   rP   rF   r*   r*   r+   rP      s    z	Term.ndim)NN)NN)r-   r.   r/   r6   __annotations__r%   propertyrG   rH   rN   rO   r@   rS   r	   r\   return_typer^   rd   rJ   setterr!   rP   r1   r*   r*   r(   r+   r2   R   s6   







r2   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 )r5   Nc                   s   t  j||||d d S )N)r9   r:   )r$   r%   )r&   rJ   r8   r9   r:   r(   r*   r+   r%      s    zConstant.__init__c                 C  s   | j S r3   rf   rF   r*   r*   r+   r@      s    zConstant._resolve_namec                 C  s   | j S r3   rI   rF   r*   r*   r+   r!      s    zConstant.namer    rB   c                 C  s
   t | jS r3   )r#   r!   rF   r*   r*   r+   rH      s    zConstant.__repr__)NN)	r-   r.   r/   r%   r@   ri   r!   rH   r1   r*   r*   r(   r+   r5      s
   
r5   ~&|)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])rs   operandsc                 C  s   t ||| _|| _|| _d S r3   )_bool_op_mapgetrs   rt   r:   )r&   rs   rt   r:   r*   r*   r+   r%      s    zOp.__init__c                 C  s
   t | jS r3   )iterrt   rF   r*   r*   r+   __iter__   s    zOp.__iter__rB   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 )(r]   N)r   ).0Zoprr*   r*   r+   	<genexpr>   s     zOp.__repr__.<locals>.<genexpr> )rt   r   rs   join)r&   Zparenedr*   r*   r+   rH      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 r3   r\   rz   termr*   r*   r+   r{      s     z!Op.return_type.<locals>.<genexpr>)rs   CMP_OPS_SYMSBOOL_OPS_SYMSr`   bool_r   comflattenrF   r*   r*   r+   rj      s    zOp.return_typer<   c                 C  s(   | j }ttdg}| jtko&|| S )Nobject)operand_types	frozensetr`   rZ   rj   r   )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 r3   r~   r   r*   r*   r+   r{      s     z#Op.operand_types.<locals>.<genexpr>)r   r   r   rF   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 r3   )r	   )rz   operandr*   r*   r+   r{      s     zOp.is_scalar.<locals>.<genexpr>)allrt   rF   r*   r*   r+   r	      s    zOp.is_scalarc                 C  s8   z| j j}W n tk
r&   | j }Y nX t|ttjfS r3   )rj   r\   r[   r_   r   r`   ra   rb   r*   r*   r+   rd      s
    zOp.is_datetime)N)r-   r.   r/   r0   rh   r%   rx   rH   ri   rj   r   r   r	   rd   r*   r*   r*   r+   rr      s   


rr   c                 C  s\   z|  |W S  tk
rV   t| rJz| | W  Y S  tk
rH   Y nX | |k Y S X dS )z`
    Compute the vectorized membership of ``x in y`` if possible, otherwise
    use Python.
    Nisinr[   r   xyr*   r*   r+   _in	  s    r   c                 C  s`   z|  | W S  tk
rZ   t| rNz| |  W  Y S  tk
rL   Y nX | |k Y S X dS )zd
    Compute the vectorized membership of ``x not in y`` if possible,
    otherwise use Python.
    Nr   r   r*   r*   r+   _not_in  s    r   )><z>=z<=z==z!=inznot in)rm   rn   rp   rq   )+-*/**//%)r   r   r   c              	   C  s`   t |}| D ]L}|j|krqz|j|}W n  tk
rN   ||j}Y nX || q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)r`   rZ   r\   rJ   Zastyper[   rS   )Ztermsacceptable_dtypesrZ   dtr   rW   r*   r*   r+   _cast_inplaceR  s    

r   r<   rB   c                 C  s
   t | tS r3   )r4   r2   )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    rs   c              
     s   t  |||f || _|| _|   |   zt| | _W nH tk
r } z*t	t
 }tdt| d| |W 5 d }~X Y nX d S )NzInvalid binary operator , valid operators are )r$   r%   lhsrhs_disallow_scalar_only_bool_opsconvert_values_binary_ops_dictfuncKeyErrorlistkeys
ValueErrorr#   )r&   rs   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&   r8   leftrightr*   r*   r+   rN     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   r8   )	r   rO   r   rs   r   rJ   Zpandas.core.computation.evalr   Zadd_tmp)r&   r8   r   r   r   r   rV   r   r   r   r!   r*   r*   r+   rO     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)r:   )r:   r   r   r   )rJ   encoderrF   r*   r+   	stringify  s    
z'BinOp.convert_values.<locals>.stringifyNUTC)r   r   r   rd   r	   rJ   r4   rg   floatr   r
   tzZ
tz_convertrS   )r&   r   r   r   vr*   rF   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 )Nr\   z$cannot evaluate scalar only bool ops)r   r   rj   getattrr	   rs   _bool_ops_dictr_   r<   r`   r   rU   )r&   r   r   Zrhs_rtZlhs_rtr*   r*   r+   r     s"    
z$BinOp._disallow_scalar_only_bool_ops)
r-   r.   r/   r0   r%   rN   rO   r   r   r1   r*   r*   r(   r+   r   o  s   
1!r   c                 C  s   t t| jtjS r3   )r_   r`   rZ   r\   number)rZ   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   rj   	TypeErrorrs   r`   Zfloat32Zfloat_r   r   r   )r&   r   r   r   r(   r*   r+   r%     s    zDiv.__init__r,   r*   r*   r(   r+   r     s   	r   )r   r   rl   ro   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    r   c              
     sf   t  ||f || _zt| | _W n< tk
r` } ztdt| dt |W 5 d }~X Y nX d S )NzInvalid unary operator r   )	r$   r%   r   _unary_ops_dictr   r   r   r#   UNARY_OPS_SYMS)r&   rs   r   r   r(   r*   r+   r%   9  s    zUnaryOp.__init__c                 C  s   |  |}| |S r3   )r   r   )r&   r8   r   r*   r*   r+   rN   E  s    
zUnaryOp.__call__rB   c                 C  s   t | j d| j dS )Nry   r]   )r   rs   r   rF   r*   r*   r+   rH   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 )Nr<   rg   )	r   rj   r`   rZ   r4   rr   rs   _cmp_ops_dictr   )r&   r   r*   r*   r+   rj   L  s    


zUnaryOp.return_type)
r-   r.   r/   r0   r%   rN   rH   ri   rj   r1   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 r3   )r$   r%   r!   r   )r&   r   rL   r(   r*   r+   r%   Y  s    zMathCall.__init__c              
     sD    fdd| j D }tjdd | jj| W  5 Q R  S Q R X d S )Nc                   s   g | ]}| qS r*   r*   )rz   rs   r   r*   r+   
<listcomp>_  s     z%MathCall.__call__.<locals>.<listcomp>ignore)r   )rt   r`   Zerrstater   )r&   r8   rt   r*   r   r+   rN   ]  s    zMathCall.__call__r    rB   c                 C  s(   t t| j}t| j dd| dS )Nry   ,r]   )mapr    rt   r   rs   r}   )r&   rt   r*   r*   r+   rH   c  s    zMathCall.__repr__)r-   r.   r/   r%   rN   rH   r1   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   r`   r   )r&   r!   r*   r*   r+   r%   i  s    zFuncNode.__init__c                 G  s
   t | |S r3   )r   )r&   rL   r*   r*   r+   rN   o  s    zFuncNode.__call__N)r-   r.   r/   r%   rN   r*   r*   r*   r+   r   h  s   r   )Xr0   
__future__r   r   	functoolsr   operatortypingr   r   Znumpyr`   Zpandas._libs.tslibsr   Zpandas.core.dtypes.commonr   r	   Zpandas.core.commoncorecommonr   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   r2   r5   ru   rr   r   r   r   gtltgeleeqneZ_cmp_ops_funcsdictzipr   r   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   drS   r   r   r   r   r   r   posneginvertZ_unary_ops_funcsr   r   r   r   r*   r*   r*   r+   <module>   s   m6
	 0