U
    cF?                     @  sn  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	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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j/e'e(fZ0e1e2e)e0Z3dZ4ej5ej6ej5ej6fZ7e1e2e4e7Z8d Z9ej:ej;ej<ej=ej>ej?ej@fZAe1e2e9eAZBd!ZCej>ej?ej@fZDe1e2eCeDZEi ZFe3e8eBfD ]ZGeFHeG qd"d# ZId$d%d&d'ZJG d(d) d)e&ZKd$d%d*d+ZLG d,d- d-eKZMd.ZNejOejPejQejQfZRe1e2eNeRZSG d/d0 d0e&ZTG d1d2 d2e&ZUG d3d4 d4ZVdS )5z
Operator classes for eval.
    )annotations)datetime)partialN)CallableIterableLiteral)	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U d' fdd	Zded< d(dddd	Zed
dddZd
dddZdd Z	d dddZ
dd Zdd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)
isinstancestrConstantsuperr    __new__)clsnameenvsideencodingklassZsupr_new	__class__ ?/tmp/pip-unpacked-wheel-g7fro6k3/pandas/core/computation/ops.pyr&   F   s    zTerm.__new__boolis_localNonereturnc                 C  sB   || _ || _|| _t|}|tp*|tk| _|  | _	|| _
d S r!   )_namer)   r*   r#   
startswith	LOCAL_TAGr   r2   _resolve_name_valuer+   )selfr(   r)   r*   r+   Ztnamer/   r/   r0   __init__N   s    
zTerm.__init__r#   c                 C  s   | j tdS )N )r(   replacer8   r;   r/   r/   r0   
local_nameX   s    zTerm.local_namec                 C  s
   t | jS r!   )r   r(   r?   r/   r/   r0   __repr__\   s    zTerm.__repr__c                 O  s   | j S r!   valuer;   argskwargsr/   r/   r0   __call___   s    zTerm.__call__c                 O  s   | S r!   r/   rD   r/   r/   r0   evaluateb   s    zTerm.evaluatec                 C  sl   t | j}| j}|| jjkr2t| jj| tr2d}| jj||d}| | t	|drh|j
dkrhtd|S )NF)r2   ndim   z?N-dimensional objects, where N > 2, are not supported with eval)r#   r@   r2   r)   Zscoper"   typeresolveupdatehasattrrI   NotImplementedError)r;   r@   r2   resr/   r/   r0   r9   e   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(   r"   r#   r)   Zswapkeyr@   rC   )r;   rC   keyr/   r/   r0   rM   v   s    

zTerm.updatec                 C  s
   t | jS r!   )r
   r:   r?   r/   r/   r0   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 r!   )r:   valuesdtypeAttributeErrorrK   r?   r/   r/   r0   rK      s    z	Term.typec                 C  s$   t | j dt| j d| j  dS )Nz(name=z, type=))rK   __name__reprr(   r?   r/   r/   r0   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 r!   )rK   rU   
issubclassr   np
datetime64r;   tr/   r/   r0   is_datetime   s
    zTerm.is_datetimec                 C  s   | j S r!   r:   r?   r/   r/   r0   rC      s    z
Term.valuec                 C  s
   || _ d S r!   r`   )r;   rQ   r/   r/   r0   rC      s    c                 C  s   | j S r!   r6   r?   r/   r/   r0   r(      s    z	Term.nameintc                 C  s   | j jS r!   )r:   rI   r?   r/   r/   r0   rI      s    z	Term.ndim)NN)NN)rW   
__module____qualname__r&   __annotations__r<   propertyr@   rA   rG   rH   r9   rM   r
   rK   return_typerY   r_   rC   setterr(   rI   __classcell__r/   r/   r-   r0   r    E   s6   





r    c                      sF   e Zd Zddd fddZdd Zedd	 Zd
dddZ  ZS )r$   Nr3   r4   c                   s   t  j||||d d S )N)r*   r+   )r%   r<   )r;   rC   r)   r*   r+   r-   r/   r0   r<      s    zConstant.__init__c                 C  s   | j S r!   ra   r?   r/   r/   r0   r9      s    zConstant._resolve_namec                 C  s   | j S r!   rB   r?   r/   r/   r0   r(      s    zConstant.namer#   c                 C  s
   t | jS r!   )rX   r(   r?   r/   r/   r0   rA      s    zConstant.__repr__)NN)	rW   rc   rd   r<   r9   rf   r(   rA   ri   r/   r/   r-   r0   r$      s
   
r$   ~&|)notandorc                   @  s   e Zd ZU dZded< 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]r3   )rq   operandsr5   c                 C  s   t ||| _|| _|| _d S r!   )_bool_op_mapgetrq   rr   r+   )r;   rq   rr   r+   r/   r/   r0   r<      s    zOp.__init__c                 C  s
   t | jS r!   )iterrr   r?   r/   r/   r0   __iter__   s    zOp.__iter__r4   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/   r0   	<genexpr>   s     zOp.__repr__.<locals>.<genexpr> )rr   r   rq   join)r;   Zparenedr/   r/   r0   rA      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 r!   rK   rx   termr/   r/   r0   ry      s     z!Op.return_type.<locals>.<genexpr>)rq   CMP_OPS_SYMSBOOL_OPS_SYMSr[   bool_r   comflattenr?   r/   r/   r0   rg      s    zOp.return_typer1   c                 C  s(   | j }ttdg}| jtko&|| S )Nobject)operand_types	frozensetr[   rT   rg   r   )r;   typesZobj_dtype_setr/   r/   r0   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 r!   r|   r}   r/   r/   r0   ry      s     z#Op.operand_types.<locals>.<genexpr>)r   r   r   r?   r/   r/   r0   r      s    zOp.operand_typesc                 C  s   t dd | jD S )Nc                 s  s   | ]}|j V  qd S r!   )r
   )rx   operandr/   r/   r0   ry      s     zOp.is_scalar.<locals>.<genexpr>)allrr   r?   r/   r/   r0   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 r!   )rg   rK   rU   rZ   r   r[   r\   r]   r/   r/   r0   r_      s
    zOp.is_datetime)N)rW   rc   rd   __doc__re   r<   rv   rA   rf   rg   r   r   r
   r_   r/   r/   r/   r0   rp      s   


rp   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isinrU   r	   xyr/   r/   r0   _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/   r0   _not_in  s    r   )><z>=z<=z==z!=inznot in)rk   rl   rn   ro   )+-*/**//%)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[   rT   rK   rC   ZastyperU   rM   )Ztermsacceptable_dtypesrT   dtr~   rQ   r/   r/   r0   _cast_inplaceL  s    

r   r1   r4   c                 C  s
   t | tS r!   )r"   r    )objr/   r/   r0   is_terme  s    r   c                      sT   e Zd ZdZddd fddZdd Zdd	d
dZdd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#   r3   rq   r5   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
ValueErrorrX   )r;   rq   r   r   errr   r-   r/   r0   r<   t  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;   r)   leftrightr/   r/   r0   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   r)   )	r   rH   r   rq   r   rC   Zpandas.core.computation.evalr   Zadd_tmp)r;   r)   r   r   r   r   rP   r   r   r   r(   r/   r/   r0   rH     s,    
	

zBinOp.evaluater4   c                   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   )rC   encoderr?   r/   r0   	stringify  s    
z'BinOp.convert_values.<locals>.stringifyNUTC)r   r   r   r_   r
   rC   r"   rb   floatr   r   tzZ
tz_convertrM   )r;   r   r   r   vr/   r?   r0   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 )NrK   z$cannot evaluate scalar only bool ops)r   r   rg   getattrr
   rq   _bool_ops_dictrZ   r1   r[   r   rO   )r;   r   r   Zrhs_rtZlhs_rtr/   r/   r0   r     s"    
z$BinOp._disallow_scalar_only_bool_ops)
rW   rc   rd   r   r<   rG   rH   r   r   ri   r/   r/   r-   r0   r   i  s   
1!r   c                 C  s   t t| jtjS r!   )rZ   r[   rT   rK   number)rT   r/   r/   r0   	isnumeric  s    r   c                      s&   e Zd ZdZdd 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.
    r3   r4   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   rg   	TypeErrorrq   r[   Zfloat32Zfloat_r   r   r   )r;   r   r   r   r-   r/   r0   r<     s    zDiv.__init__)rW   rc   rd   r   r<   ri   r/   r/   r-   r0   r     s   	r   )r   r   rj   rm   c                      sV   e Zd ZdZddd fddZdd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.
    zLiteral[('+', '-', '~', 'not')]r3   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   rX   UNARY_OPS_SYMS)r;   rq   r   r   r-   r/   r0   r<   3  s    zUnaryOp.__init__MathCallr4   c                 C  s   |  |}| |S r!   )r   r   )r;   r)   r   r/   r/   r0   rG   ?  s    
zUnaryOp.__call__r#   c                 C  s   t | j d| j dS )Nrw   rV   )r   rq   r   r?   r/   r/   r0   rA   D  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 )Nr1   rb   )	r   rg   r[   rT   r"   rp   rq   _cmp_ops_dictr   )r;   r   r/   r/   r0   rg   G  s    


zUnaryOp.return_type)
rW   rc   rd   r   r<   rG   rA   rf   rg   ri   r/   r/   r-   r0   r   "  s   r   c                      s8   e Zd Zdd fddZdd Zdddd	Z  ZS )
r   r3   r4   c                   s   t  |j| || _d S r!   )r%   r<   r(   r   )r;   r   rE   r-   r/   r0   r<   T  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/   )rx   rq   r   r/   r0   
<listcomp>Z  s     z%MathCall.__call__.<locals>.<listcomp>ignore)r   )rr   r[   Zerrstater   )r;   r)   rr   r/   r   r0   rG   X  s    zMathCall.__call__r#   c                 C  s(   t t| j}t| j dd| dS )Nrw   ,rV   )mapr#   rr   r   rq   r{   )r;   rr   r/   r/   r0   rA   ^  s    zMathCall.__repr__)rW   rc   rd   r<   rG   rA   ri   r/   r/   r-   r0   r   S  s   r   c                   @  s$   e Zd ZdddddZdd ZdS )	FuncNoder#   r3   )r(   r5   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/   r0   r<   d  s    zFuncNode.__init__c                 G  s
   t | |S r!   )r   )r;   rE   r/   r/   r0   rG   j  s    zFuncNode.__call__N)rW   rc   rd   r<   rG   r/   r/   r/   r0   r   c  s   r   )Wr   
__future__r   r   	functoolsr   operatortypingr   r   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   r8   r    r$   rs   rp   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   drM   r   r   r   r   r   r   posneginvertZ_unary_ops_funcsr   r   r   r   r/   r/   r/   r0   <module>   s   t6
	 1