o
    ŀg9                     @  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r]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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 qd2d%d&ZLG d'd( d(e)ZMd2d)d*ZNd+ZOejPejQejRejRfZSe4e5eOeSZTG d,d- d-e)ZUG d.d/ d/e)ZVG d0d1 d1ZWdS )3z
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tanharcsinarccosarctanarccosharcsinharctanhabslog10floorceil)arctan2__pd_eval_local_c                      s   e Zd ZU d' fdd	Zded< d'd(dd	Zed)ddZd)ddZdd Z	d*ddZ
dd Zd(ddZed+ddZedd ZeZed)ddZed+ddZedd  Zejd(d!d Zed"d# Ze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klasssupr_new	__class__ O/var/www/html/myenv/lib/python3.10/site-packages/pandas/core/computation/ops.pyr2   L   s   zTerm.__new__boolis_localreturnNonec                 C  sB   || _ || _|| _t|}|tp|tv | _|  | _	|| _
d S r-   )_namer5   r6   r/   
startswith	LOCAL_TAGr   r?   _resolve_name_valuer7   )selfr4   r5   r6   r7   tnamer<   r<   r=   __init__T   s   

zTerm.__init__r/   c                 C  s   | j tdS )N )r4   replacerD   rG   r<   r<   r=   
local_name^   s   zTerm.local_namec                 C  
   t | jS r-   )r   r4   rL   r<   r<   r=   __repr__b      
zTerm.__repr__c                 O     | j S r-   valuerG   argskwargsr<   r<   r=   __call__e      zTerm.__call__c                 O  s   | S r-   r<   rT   r<   r<   r=   evaluateh   s   zTerm.evaluatec                 C  sl   t | j}| j}|| jjv rt| jj| trd}| jj||d}| | t	|dr4|j
dkr4td|S )NF)r?   ndim   z?N-dimensional objects, where N > 2, are not supported with eval)r/   rM   r?   r5   scoper.   typeresolveupdatehasattrrZ   NotImplementedError)rG   rM   r?   resr<   r<   r=   rE   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)r4   r.   r/   r5   swapkeyrM   rS   )rG   rS   keyr<   r<   r=   r_   |   s   


zTerm.updatec                 C  rN   r-   )r
   rF   rL   r<   r<   r=   r
         
zTerm.is_scalarc                 C  sL   z| j jjW S  ty%   z| j jW  Y S  ty$   t| j  Y  Y S w w r-   )rF   valuesdtypeAttributeErrorr]   rL   r<   r<   r=   r]      s   z	Term.typec                 C  s$   t | j dt| j d| j  dS )Nz(name=z, type=))r]   __name__reprr4   rL   r<   r<   r=   raw   s   $zTerm.rawc                 C  s6   z| j j }W n ty   | j }Y nw t|ttjfS r-   )r]   ri   
issubclassr   np
datetime64rG   tr<   r<   r=   is_datetime      
zTerm.is_datetimec                 C  rQ   r-   rF   rL   r<   r<   r=   rS         z
Term.valuec                 C  s
   || _ d S r-   ru   )rG   rc   r<   r<   r=   rS      rf   c                 C  rQ   r-   rB   rL   r<   r<   r=   r4      rv   z	Term.nameintc                 C  s   | j jS r-   )rF   rZ   rL   r<   r<   r=   rZ      s   z	Term.ndim)NNr@   rA   r@   r/   )r@   r,   r@   r>   )r@   rx   )rk   
__module____qualname__r2   __annotations__rI   propertyrM   rO   rW   rY   rE   r_   r
   r]   return_typerm   rs   rS   setterr4   rZ   __classcell__r<   r<   r:   r=   r,   K   s8   
 






r,   c                   @  s*   e Zd Zdd Zedd Zd
ddZd	S )r0   c                 C  rQ   r-   rw   rL   r<   r<   r=   rE      rX   zConstant._resolve_namec                 C  rQ   r-   rR   rL   r<   r<   r=   r4      rv   zConstant.namer@   r/   c                 C  rN   r-   )rl   r4   rL   r<   r<   r=   rO      s   
zConstant.__repr__Nrz   )rk   r|   r}   rE   r   r4   rO   r<   r<   r<   r=   r0      s
    
r0   ~&|)notandorc                   @  s|   e Zd ZU dZded< ddd	d
ZdddZdddZedd Z	edddZ
edd ZedddZedddZdS ) Opz.
    Hold an operator of arbitrary arity.
    r/   opNoperandsIterable[Term | Op]r@   rA   c                 C  s   t ||| _|| _|| _d S r-   )_bool_op_mapgetr   r   r7   )rG   r   r   r7   r<   r<   r=   rI      s   
zOp.__init__r   c                 C  rN   r-   )iterr   rL   r<   r<   r=   __iter__   rP   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 )(rj   N)r   ).0oprr<   r<   r=   	<genexpr>   s    zOp.__repr__.<locals>.<genexpr> )r   r   r   join)rG   parenedr<   r<   r=   rO      s   zOp.__repr__c                 C  s,   | j tt v r
tjS tdd t| D  S )Nc                 s      | ]}|j V  qd S r-   r]   r   termr<   r<   r=   r          z!Op.return_type.<locals>.<genexpr>)r   CMP_OPS_SYMSBOOL_OPS_SYMSro   bool_r   comflattenrL   r<   r<   r=   r      s   zOp.return_typer>   c                 C  s(   | j }ttdg}| jtko|| S )Nobject)operand_types	frozensetro   rh   r   r   )rG   types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  r   r-   r   r   r<   r<   r=   r      r   z#Op.operand_types.<locals>.<genexpr>)r   r   r   rL   r<   r<   r=   r      s   zOp.operand_typesc                 C  s   t dd | jD S )Nc                 s  r   r-   )r
   )r   operandr<   r<   r=   r      r   zOp.is_scalar.<locals>.<genexpr>)allr   rL   r<   r<   r=   r
      s   zOp.is_scalarc                 C  s6   z| j j}W n ty   | j }Y nw t|ttjfS r-   )r   r]   ri   rn   r   ro   rp   rq   r<   r<   r=   rs      rt   zOp.is_datetimer-   )r   r/   r   r   r@   rA   )r@   r   rz   r{   )rk   r|   r}   __doc__r~   rI   r   rO   r   r   r   r   r
   rs   r<   r<   r<   r=   r      s    
 



r   c                 C  sT   z|  |W S  ty)   t| r#z| | W  Y S  ty"   Y nw | |v  Y S w )z`
    Compute the vectorized membership of ``x in y`` if possible, otherwise
    use Python.
    isinri   r	   xyr<   r<   r=   _in  s   r   c                 C  sX   z|  | W S  ty+   t| r%z	| |  W  Y S  ty$   Y nw | |v Y S w )zd
    Compute the vectorized membership of ``x not in y`` if possible,
    otherwise use Python.
    r   r   r<   r<   r=   _not_in  s   r   )><z>=z<=z==z!=inznot in)r   r   r   r   )+-*/**//%)r   r   r   r@   r>   c                 C  s
   t | tS r-   )r.   r,   )objr<   r<   r=   is_termO  rP   r   c                      sF   e Zd ZdZd fddZdd	 ZdddZ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   r/   r@   rA   c              
     s~   t  |||f || _|| _|   |   zt| | _W d S  ty> } zt	t
 }tdt| d| |d }~ww )NzInvalid binary operator , valid operators are )r1   rI   lhsrhs_disallow_scalar_only_bool_opsconvert_values_binary_ops_dictfuncKeyErrorlistkeys
ValueErrorrl   )rG   r   r   r   errr   r:   r<   r=   rI   ^  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   )rG   r5   leftrightr<   r<   r=   rW   p  s   

zBinOp.__call__enginec                 C  s   |dkr	| |}n2| j j|||||d}| jj|||||d}| j|v r-| |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)
local_dictr   r   r5   )	r   rY   r   r   r   rS   pandas.core.computation.evalr   add_tmp)rG   r5   r   r   r   r   rb   r   r   r   r4   r<   r<   r=   rY     s,   

	
zBinOp.evaluatec                   s    fdd} j  j}}t|r?|jr?t|r?|jr?|j}t|ttfr)||}t	t
|}|jdur9|d} j| t|rs|jrut|rw|jry|j}t|ttfr[||}t	t
|}|jdurk|d} j | dS dS dS dS dS )zK
        Convert datetimes to a comparable value in an expression.
        c                   s,    j d urtt j d}|| S t}|| S )N)r7   )r7   r   r   r   )rS   encoderrL   r<   r=   	stringify  s
   
z'BinOp.convert_values.<locals>.stringifyNUTC)r   r   r   rs   r
   rS   r.   rx   floatr   r   tz
tz_convertr_   )rG   r   r   r   vr<   rL   r=   r     s&   



zBinOp.convert_valuesc                 C  sz   | j }| j}|j}t|d|}|j}t|d|}|js|jr9| jtv r7t|tt	j
fr3t|tt	j
fs;tdd S d S d S )Nr]   z$cannot evaluate scalar only bool ops)r   r   r   getattrr
   r   _bool_ops_dictrn   r>   ro   r   ra   )rG   r   r   rhs_rtlhs_rtr<   r<   r=   r     s&   
z$BinOp._disallow_scalar_only_bool_ops)r   r/   r@   rA   )r   r/   ry   )
rk   r|   r}   r   rI   rW   rY   r   r   r   r<   r<   r:   r=   r   S  s    


1!r   c                 C  s   t t| jtjS r-   )rn   ro   rh   r]   number)rh   r<   r<   r=   	isnumeric  s   r   )r   r   r   r   c                      sD   e Zd ZdZd fddZdd	d
ZdddZe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   Literal['+', '-', '~', 'not']r@   rA   c              
     sZ   t  ||f || _zt| | _W d S  ty, } ztdt| dt |d }~ww )NzInvalid unary operator r   )	r1   rI   r   _unary_ops_dictr   r   r   rl   UNARY_OPS_SYMS)rG   r   r   r   r:   r<   r=   rI     s   zUnaryOp.__init__MathCallc                 C  s   |  |}| |S r-   )r   r   )rG   r5   r   r<   r<   r=   rW     s   

zUnaryOp.__call__r/   c                 C  s   t | j d| j dS )Nr   rj   )r   r   r   rL   r<   r<   r=   rO     s   zUnaryOp.__repr__np.dtypec                 C  sR   | j }|jtdkrtdS t|tr$|jtv s|jtv r$tdS tdS )Nr>   rx   )	r   r   ro   rh   r.   r   r   _cmp_ops_dictr   )rG   r   r<   r<   r=   r     s   



zUnaryOp.return_type)r   r   r@   rA   r@   r   rz   )r@   r   )
rk   r|   r}   r   rI   rW   rO   r   r   r   r<   r<   r:   r=   r     s    

r   c                      s0   e Zd Zd
 fddZdd Zddd	Z  ZS )r   r@   rA   c                   s   t  |j| || _d S r-   )r1   rI   r4   r   )rG   r   rU   r:   r<   r=   rI   &  s   
zMathCall.__init__c                   s     fdd| j D }| jj| S )Nc                   s   g | ]}| qS r<   r<   )r   r   r   r<   r=   
<listcomp>,  s    z%MathCall.__call__.<locals>.<listcomp>)r   r   )rG   r5   r   r<   r   r=   rW   *  s   zMathCall.__call__r/   c                 C  s(   t t| j}t| j dd| dS )Nr   ,rj   )mapr/   r   r   r   r   )rG   r   r<   r<   r=   rO   /  s   zMathCall.__repr__ry   rz   )rk   r|   r}   rI   rW   rO   r   r<   r<   r:   r=   r   %  s    r   c                   @  s    e Zd ZdddZddd	Zd
S )FuncNoder4   r/   r@   rA   c                 C  s.   |t vrtd| d|| _tt|| _d S )N"z" is not a supported function)MATHOPSr   r4   r   ro   r   )rG   r4   r<   r<   r=   rI   5  s   zFuncNode.__init__r   c                 G  s
   t | |S r-   )r   )rG   rU   r<   r<   r=   rW   ;  rP   zFuncNode.__call__N)r4   r/   r@   rA   r   )rk   r|   r}   rI   rW   r<   r<   r<   r=   r   4  s    
r   r{   )Xr   
__future__r   r   	functoolsr   operatortypingr   r   r   numpyro   pandas._libs.tslibsr   pandas.core.dtypes.commonr	   r
   pandas.core.commoncorecommonr   pandas.core.computation.commonr   r   pandas.core.computation.scoper   pandas.io.formats.printingr   r   collections.abcr   r   
REDUCTIONS_unary_math_ops_binary_math_opsr   rD   r,   r0   r   r   r   r   r   gtltgeleeqne_cmp_ops_funcsdictzipr   r   and_or__bool_ops_funcsr   ARITH_OPS_SYMSaddsubmultruedivpowfloordivmod_arith_ops_funcs_arith_ops_dictSPECIAL_CASE_ARITH_OPS_SYMS_special_case_arith_ops_funcs_special_case_arith_ops_dictr   dr_   r   r   r   r   posneginvert_unary_ops_funcsr   r   r   r   r<   r<   r<   r=   <module>   s    t6
	
 
1