a
    Of,?                     @  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	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 erd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j0ej1ej2e*e+fZ3e4e5e,e3Z6d Z7ej8ej9ej8ej9fZ:e4e5e7e:Z;d!Z<ej=ej>ej?ej@ejAejBejCfZDe4e5e<eDZEd"ZFejAejBejCfZGe4e5eFeGZHi ZIe6e;eEfD ]ZJeIKeJ qd#d$d%d&ZLd'd$d(d)ZMG d*d+ d+e)ZNd'd$d,d-ZOG d.d/ d/eNZPd0ZQejRejSejTejTfZUe4e5eQeUZVG d1d2 d2e)ZWG d3d4 d4e)ZXG d5d6 d6ZYdS )7z
Operator classes for eval.
    )annotations)datetime)partialN)TYPE_CHECKINGCallableLiteral)	Timestamp)is_list_like	is_scalar)ensure_decodedresult_type_many)DEFAULT_GLOBALS)pprint_thingpprint_thing_encoded)IterableIterator)sumprodminmax)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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__ T/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/core/computation/ops.pyr*   L   s    zTerm.__new__boolis_localNonereturnc                 C  sB   || _ || _|| _t|}|tp*|tv | _|  | _	|| _
d S r%   )_namer-   r.   r'   
startswith	LOCAL_TAGr   r6   _resolve_name_valuer/   )selfr,   r-   r.   r/   Ztnamer3   r3   r4   __init__T   s    
zTerm.__init__r'   c                 C  s   | j tdS )N )r,   replacer<   r?   r3   r3   r4   
local_name^   s    zTerm.local_namec                 C  s
   t | jS r%   )r   r,   rC   r3   r3   r4   __repr__b   s    zTerm.__repr__c                 O  s   | j S r%   valuer?   argskwargsr3   r3   r4   __call__e   s    zTerm.__call__c                 O  s   | S r%   r3   rH   r3   r3   r4   evaluateh   s    zTerm.evaluatec                 C  sl   t | j}| j}|| jjv r2t| jj| tr2d}| jj||d}| | t	|drh|j
dkrhtd|S )NF)r6   ndim   z?N-dimensional objects, where N > 2, are not supported with eval)r'   rD   r6   r-   scoper&   typeresolveupdatehasattrrM   NotImplementedError)r?   rD   r6   resr3   r3   r4   r=   k   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swapkeyrD   rG   )r?   rG   keyr3   r3   r4   rR   |   s    

zTerm.updatec                 C  s
   t | jS r%   )r
   r>   rC   r3   r3   r4   r
      s    zTerm.is_scalarc                 C  sT   z| j jjW S  tyN   z| j jW  Y S  tyH   t| j  Y  Y S 0 Y n0 d S r%   )r>   valuesdtypeAttributeErrorrP   rC   r3   r3   r4   rP      s    z	Term.typec                 C  s$   t | j dt| j d| j  dS )Nz(name=z, type=))rP   __name__reprr,   rC   r3   r3   r4   raw   s    zTerm.rawc                 C  s6   z| j j }W n ty$   | j }Y n0 t|ttjfS r%   )rP   rZ   
issubclassr   np
datetime64r?   tr3   r3   r4   is_datetime   s
    zTerm.is_datetimec                 C  s   | j S r%   r>   rC   r3   r3   r4   rG      s    z
Term.valuec                 C  s
   || _ d S r%   re   )r?   rV   r3   r3   r4   rG      s    c                 C  s   | j S r%   r:   rC   r3   r3   r4   r,      s    z	Term.nameintc                 C  s   | j jS r%   )r>   rM   rC   r3   r3   r4   rM      s    z	Term.ndim)NN)NN)r\   
__module____qualname__r*   __annotations__r@   propertyrD   rE   rK   rL   r=   rR   r
   rP   return_typer^   rd   rG   setterr,   rM   __classcell__r3   r3   r1   r4   r$   K   s6   




r$   c                   @  s.   e Zd Zdd Zedd ZddddZd	S )
r(   c                 C  s   | j S r%   rf   rC   r3   r3   r4   r=      s    zConstant._resolve_namec                 C  s   | j S r%   rF   rC   r3   r3   r4   r,      s    zConstant.namer'   r8   c                 C  s
   t | jS r%   )r]   r,   rC   r3   r3   r4   rE      s    zConstant.__repr__N)r\   rh   ri   r=   rk   r,   rE   r3   r3   r3   r4   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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]r7   )rv   operandsr9   c                 C  s   t ||| _|| _|| _d S r%   )_bool_op_mapgetrv   rw   r/   )r?   rv   rw   r/   r3   r3   r4   r@      s    zOp.__init__r   r8   c                 C  s
   t | jS r%   )iterrw   rC   r3   r3   r4   __iter__   s    zOp.__iter__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oprr3   r3   r4   	<genexpr>       zOp.__repr__.<locals>.<genexpr> )rw   r   rv   join)r?   Zparenedr3   r3   r4   rE      s    zOp.__repr__c                 C  s,   | j tt v rtjS tdd t| D  S )Nc                 s  s   | ]}|j V  qd S r%   rP   r}   termr3   r3   r4   r~      r   z!Op.return_type.<locals>.<genexpr>)rv   CMP_OPS_SYMSBOOL_OPS_SYMSr`   bool_r   comflattenrC   r3   r3   r4   rl      s    zOp.return_typer5   c                 C  s(   | j }ttdg}| jtko&|| S )Nobject)operand_types	frozensetr`   rY   rl   r   )r?   typesZobj_dtype_setr3   r3   r4   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   r3   r3   r4   r~      r   z#Op.operand_types.<locals>.<genexpr>)r   r   r   rC   r3   r3   r4   r      s    zOp.operand_typesc                 C  s   t dd | jD S )Nc                 s  s   | ]}|j V  qd S r%   )r
   )r}   operandr3   r3   r4   r~      r   zOp.is_scalar.<locals>.<genexpr>)allrw   rC   r3   r3   r4   r
      s    zOp.is_scalarc                 C  s6   z| j j}W n ty$   | j }Y n0 t|ttjfS r%   )rl   rP   rZ   r_   r   r`   ra   rb   r3   r3   r4   rd      s
    zOp.is_datetime)N)r\   rh   ri   __doc__rj   r@   r{   rE   rk   rl   r   r   r
   rd   r3   r3   r3   r4   ru      s   


ru   c                 C  sX   z|  |W S  tyR   t| rFz| | W  Y S  tyD   Y n0 | |v  Y S 0 dS )z`
    Compute the vectorized membership of ``x in y`` if possible, otherwise
    use Python.
    NisinrZ   r	   xyr3   r3   r4   _in  s    r   c                 C  s\   z|  | W S  tyV   t| rJz| |  W  Y S  tyH   Y n0 | |v Y S 0 dS )zd
    Compute the vectorized membership of ``x not in y`` if possible,
    otherwise use Python.
    Nr   r   r3   r3   r4   _not_in  s    r   )><z>=z<=z==z!=inznot in)rp   rq   rs   rt   )+-*/**//%)r   r   r   r7   r8   c              	   C  s^   t |}| D ]J}|j|v rqz|j|}W n tyL   ||j}Y n0 || 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`   rY   rP   rG   ZastyperZ   rR   )Ztermsacceptable_dtypesrY   dtr   rV   r3   r3   r4   _cast_inplaceO  s    

r   r5   c                 C  s
   t | tS r%   )r&   r$   )objr3   r3   r4   is_termh  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'   r7   rv   r9   c              
     s   t  |||f || _|| _|   |   zt| | _W nJ ty } z2t	t
 }tdt| d| |W Y d }~n
d }~0 0 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?   rv   r   r   errr   r1   r3   r4   r@   w  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rightr3   r3   r4   rK     s    

zBinOp.__call__)enginec                 C  s   |dkr| |}nd| j j|||||d}| jj|||||d}| j|v 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   rL   r   rv   r   rG   Zpandas.core.computation.evalr   Zadd_tmp)r?   r-   r   r   r   r   rU   r   r   r   r,   r3   r3   r4   rL     s,    
	

zBinOp.evaluater8   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urr|d} j| t|r|jrt|r|jr|j}t|ttfr||}t	t
|}|jdur|d} j | dS )zK
        Convert datetimes to a comparable value in an expression.
        c                   s&    j d urtt j d}nt}|| S )N)r/   )r/   r   r   r   )rG   encoderrC   r3   r4   	stringify  s    
z'BinOp.convert_values.<locals>.stringifyNUTC)r   r   r   rd   r
   rG   r&   rg   floatr   r   tzZ
tz_convertrR   )r?   r   r   r   vr3   rC   r4   r     s$    



zBinOp.convert_valuesc                 C  sr   | j }| j}|j}t|d|}|j}t|d|}|js<|jrn| jtv rnt|tt	j
frft|tt	j
fsntdd S )NrP   z$cannot evaluate scalar only bool ops)r   r   rl   getattrr
   rv   _bool_ops_dictr_   r5   r`   r   rT   )r?   r   r   Zrhs_rtZlhs_rtr3   r3   r4   r     s"    
z$BinOp._disallow_scalar_only_bool_ops)
r\   rh   ri   r   r@   rK   rL   r   r   rn   r3   r3   r1   r4   r   l  s   
1!r   c                 C  s   t t| jtjS r%   )r_   r`   rY   rP   number)rY   r3   r3   r4   	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.
    r7   r8   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   rl   	TypeErrorrv   r`   Zfloat32Zfloat64r   r   r   )r?   r   r   r   r1   r3   r4   r@     s    
zDiv.__init__)r\   rh   ri   r   r@   rn   r3   r3   r1   r4   r     s   	r   )r   r   ro   rr   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']r7   r   c              
     sh   t  ||f || _zt| | _W n> tyb } z&tdt| dt |W Y d }~n
d }~0 0 d S )NzInvalid unary operator r   )	r)   r@   r   _unary_ops_dictr   r   r   r]   UNARY_OPS_SYMS)r?   rv   r   r   r1   r3   r4   r@   6  s    zUnaryOp.__init__MathCallr8   c                 C  s   |  |}| |S r%   )r   r   )r?   r-   r   r3   r3   r4   rK   B  s    
zUnaryOp.__call__r'   c                 C  s   t | j d| j dS )Nr|   r[   )r   rv   r   rC   r3   r3   r4   rE   G  s    zUnaryOp.__repr__znp.dtypec                 C  sR   | j }|jtdkr tdS t|trH|jtv s>|jtv rHtdS tdS )Nr5   rg   )	r   rl   r`   rY   r&   ru   rv   _cmp_ops_dictr   )r?   r   r3   r3   r4   rl   J  s    


zUnaryOp.return_type)
r\   rh   ri   r   r@   rK   rE   rk   rl   rn   r3   r3   r1   r4   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   r7   r8   c                   s   t  |j| || _d S r%   )r)   r@   r,   r   )r?   r   rI   r1   r3   r4   r@   W  s    zMathCall.__init__c                   s     fdd| j D }| jj| S )Nc                   s   g | ]}| qS r3   r3   )r}   rv   r   r3   r4   
<listcomp>]  r   z%MathCall.__call__.<locals>.<listcomp>)rw   r   )r?   r-   rw   r3   r   r4   rK   [  s    zMathCall.__call__r'   c                 C  s(   t t| j}t| j dd| dS )Nr|   ,r[   )mapr'   rw   r   rv   r   )r?   rw   r3   r3   r4   rE   `  s    zMathCall.__repr__)r\   rh   ri   r@   rK   rE   rn   r3   r3   r1   r4   r   V  s   r   c                   @  s*   e Zd ZdddddZdddd	Zd
S )FuncNoder'   r7   )r,   r9   c                 C  s.   |t vrtd| d|| _tt|| _d S )N"z" is not a supported function)MATHOPSr   r,   r   r`   r   )r?   r,   r3   r3   r4   r@   f  s    zFuncNode.__init__r   r8   c                 G  s
   t | |S r%   )r   )r?   rI   r3   r3   r4   rK   l  s    zFuncNode.__call__N)r\   rh   ri   r@   rK   r3   r3   r3   r4   r   e  s   r   )Zr   
__future__r   r   	functoolsr   operatortypingr   r   r   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   collections.abcr   r   Z
REDUCTIONSZ_unary_math_opsZ_binary_math_opsr   r<   r$   r(   rx   ru   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   drR   r   r   r   r   r   r   posneginvertZ_unary_ops_funcsr   r   r   r   r3   r3   r3   r4   <module>   s   t6
	 1