o
    5cF?                     @  sd  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# ZId5d&d'ZJG d(d) d)e&ZKd5d*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 )6z
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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  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__ Q/var/www/html/gps/gps/lib/python3.10/site-packages/pandas/core/computation/ops.pyr.   F   s   zTerm.__new__boolis_localreturnNonec                 C  sB   || _ || _|| _t|}|tp|tv | _|  | _	|| _
d S r)   )_namer1   r2   r+   
startswith	LOCAL_TAGr   r;   _resolve_name_valuer3   )selfr0   r1   r2   r3   tnamer8   r8   r9   __init__N   s   

zTerm.__init__r+   c                 C  s   | j tdS )N )r0   replacer@   rC   r8   r8   r9   
local_nameX   s   zTerm.local_namec                 C  
   t | jS r)   )r   r0   rH   r8   r8   r9   __repr__\      
zTerm.__repr__c                 O     | j S r)   valuerC   argskwargsr8   r8   r9   __call___      zTerm.__call__c                 O  s   | S r)   r8   rP   r8   r8   r9   evaluateb   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+   rI   r;   r1   scoper*   typeresolveupdatehasattrrV   NotImplementedError)rC   rI   r;   resr8   r8   r9   rA   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)r0   r*   r+   r1   swapkeyrI   rO   )rC   rO   keyr8   r8   r9   r[   v   s   


zTerm.updatec                 C  rJ   r)   )r
   rB   rH   r8   r8   r9   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)   )rB   valuesdtypeAttributeErrorrY   rH   r8   r8   r9   rY      s   z	Term.typec                 C  s$   t | j dt| j d| j  dS )Nz(name=z, type=))rY   __name__reprr0   rH   r8   r8   r9   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)   )rY   re   
issubclassr   np
datetime64rC   tr8   r8   r9   is_datetime      
zTerm.is_datetimec                 C  rM   r)   rB   rH   r8   r8   r9   rO         z
Term.valuec                 C  s
   || _ d S r)   rq   )rC   r_   r8   r8   r9   rO      rb   c                 C  rM   r)   r>   rH   r8   r8   r9   r0      rr   z	Term.nameintc                 C  s   | j jS r)   )rB   rV   rH   r8   r8   r9   rV      s   z	Term.ndimNNr<   r=   r<   r+   )r<   r(   r<   r:   )r<   rt   )rg   
__module____qualname__r.   __annotations__rE   propertyrI   rK   rS   rU   rA   r[   r
   rY   return_typeri   ro   rO   setterr0   rV   __classcell__r8   r8   r6   r9   r(   E   s8   
 







r(   c                      s>   e Zd Zdd fddZdd Zedd	 ZdddZ  ZS )r,   Nr<   r=   c                   s   t  j||||d d S )N)r2   r3   )r-   rE   )rC   rO   r1   r2   r3   r6   r8   r9   rE         zConstant.__init__c                 C  rM   r)   rs   rH   r8   r8   r9   rA      rT   zConstant._resolve_namec                 C  rM   r)   rN   rH   r8   r8   r9   r0      rr   zConstant.namer+   c                 C  rJ   r)   )rh   r0   rH   r8   r8   r9   rK      s   
zConstant.__repr__ru   rv   rw   )	rg   ry   rz   rE   rA   r|   r0   rK   r   r8   r8   r6   r9   r,      s    
r,   ~&|)notandorc                   @  sz   e Zd ZU dZded< ddd	d
Z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<   r=   c                 C  s   t ||| _|| _|| _d S r)   )_bool_op_mapgetr   r   r3   )rC   r   r   r3   r8   r8   r9   rE      s   
zOp.__init__c                 C  rJ   r)   )iterr   rH   r8   r8   r9   __iter__   rL   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 )(rf   N)r   ).0oprr8   r8   r9   	<genexpr>   s    zOp.__repr__.<locals>.<genexpr> )r   r   r   join)rC   parenedr8   r8   r9   rK      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)   rY   r   termr8   r8   r9   r          z!Op.return_type.<locals>.<genexpr>)r   CMP_OPS_SYMSBOOL_OPS_SYMSrk   bool_r   comflattenrH   r8   r8   r9   r}      s   zOp.return_typer:   c                 C  s(   | j }ttdg}| jtko|| S )Nobject)operand_types	frozensetrk   rd   r}   r   )rC   typesobj_dtype_setr8   r8   r9   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   r8   r8   r9   r      r   z#Op.operand_types.<locals>.<genexpr>)r   r   r   rH   r8   r8   r9   r      s   zOp.operand_typesc                 C  s   t dd | jD S )Nc                 s  r   r)   )r
   )r   operandr8   r8   r9   r      r   zOp.is_scalar.<locals>.<genexpr>)allr   rH   r8   r8   r9   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}   rY   re   rj   r   rk   rl   rm   r8   r8   r9   ro      rp   zOp.is_datetimer)   )r   r+   r   r   r<   r=   rw   rx   )rg   ry   rz   __doc__r{   rE   r   rK   r|   r}   r   r   r
   ro   r8   r8   r8   r9   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.
    isinre   r	   xyr8   r8   r9   _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   r8   r8   r9   _not_in  s   r   )><z>=z<=z==z!=inznot in)r   r   r   r   )+-*/**//%)r   r   r   c              	   C  s^   t |}| D ]%}|j|v rqz|j|}W n ty&   ||j}Y nw || 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)rk   rd   rY   rO   astypere   r[   )termsacceptable_dtypesrd   dtr   r_   r8   r8   r9   _cast_inplaceL  s   

r   r<   r:   c                 C  s
   t | tS r)   )r*   r(   )objr8   r8   r9   is_terme  rL   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<   r=   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 )r-   rE   lhsrhs_disallow_scalar_only_bool_opsconvert_values_binary_ops_dictfuncKeyErrorlistkeys
ValueErrorrh   )rC   r   r   r   errr   r6   r8   r9   rE   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   )rC   r1   leftrightr8   r8   r9   rS     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   r1   )	r   rU   r   r   r   rO   pandas.core.computation.evalr   add_tmp)rC   r1   r   r   r   r   r^   r   r   r   r0   r8   r8   r9   rU     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)r3   )r3   r   r   r   )rO   encoderrH   r8   r9   	stringify  s
   
z'BinOp.convert_values.<locals>.stringifyNUTC)r   r   r   ro   r
   rO   r*   rt   floatr   r   tz
tz_convertr[   )rC   r   r   r   vr8   rH   r9   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 )NrY   z$cannot evaluate scalar only bool ops)r   r   r}   getattrr
   r   _bool_ops_dictrj   r:   rk   r   r]   )rC   r   r   rhs_rtlhs_rtr8   r8   r9   r     s$   
z$BinOp._disallow_scalar_only_bool_ops)r   r+   r<   r=   )r   r+   rv   )
rg   ry   rz   r   rE   rS   rU   r   r   r   r8   r8   r6   r9   r   i  s    


1!r   c                 C  s   t t| jtjS r)   )rj   rk   rd   rY   number)rd   r8   r8   r9   	isnumeric  s   r   c                      s"   e Zd ZdZ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.
    r<   r=   c                   sj   t  d|| t|jrt|js#t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-   rE   r   r}   	TypeErrorr   rk   float32float_r   r   r   )rC   r   r   r   r6   r8   r9   rE     s   
zDiv.__init__rv   )rg   ry   rz   r   rE   r   r8   r8   r6   r9   r     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<   r=   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   )	r-   rE   r   _unary_ops_dictr   r   r   rh   UNARY_OPS_SYMS)rC   r   r   r   r6   r8   r9   rE   3  s   zUnaryOp.__init__MathCallc                 C  s   |  |}| |S r)   )r   r   )rC   r1   r   r8   r8   r9   rS   ?  s   

zUnaryOp.__call__r+   c                 C  s   t | j d| j dS )Nr   rf   )r   r   r   rH   r8   r8   r9   rK   D  r   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:   rt   )	r   r}   rk   rd   r*   r   r   _cmp_ops_dictr   )rC   r   r8   r8   r9   r}   G  s   



zUnaryOp.return_type)r   r   r<   r=   )r<   r   rw   )r<   r   )
rg   ry   rz   r   rE   rS   rK   r|   r}   r   r8   r8   r6   r9   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<   r=   c                   s   t  |j| || _d S r)   )r-   rE   r0   r   )rC   r   rQ   r6   r8   r9   rE   T  s   
zMathCall.__init__c                   sP    fdd| j D }tjdd | jj| W  d    S 1 s!w   Y  d S )Nc                   s   g | ]}| qS r8   r8   )r   r   r   r8   r9   
<listcomp>Z  s    z%MathCall.__call__.<locals>.<listcomp>ignore)r   )r   rk   errstater   )rC   r1   r   r8   r   r9   rS   X  s   
$zMathCall.__call__r+   c                 C  s(   t t| j}t| j dd| dS )Nr   ,rf   )mapr+   r   r   r   r   )rC   r   r8   r8   r9   rK   ^  s   zMathCall.__repr__rv   rw   )rg   ry   rz   rE   rS   rK   r   r8   r8   r6   r9   r   S  s    r   c                   @  s   e Zd Zd
ddZdd Zd	S )FuncNoder0   r+   r<   r=   c                 C  s.   |t vrtd| d|| _tt|| _d S )N"z" is not a supported function)MATHOPSr   r0   r   rk   r   )rC   r0   r8   r8   r9   rE   d  s   zFuncNode.__init__c                 G  s
   t | |S r)   )r   )rC   rQ   r8   r8   r9   rS   j  rL   zFuncNode.__call__N)r0   r+   r<   r=   )rg   ry   rz   rE   rS   r8   r8   r8   r9   r   c  s    
r   rx   )Wr   
__future__r   r   	functoolsr   operatortypingr   r   r   numpyrk   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   
REDUCTIONS_unary_math_ops_binary_math_opsr   r@   r(   r,   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   r   r   posneginvert_unary_ops_funcsr   r   r   r   r8   r8   r8   r9   <module>   s    t6
	
 
1