U
    f/e                  8   @  s  U d Z ddlmZ ddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ erfddlZdaded	< g ad
ed< eadaded< daded< dddddhdddhdZdZdIddZdJddZdd Zdd Zdd  Zejd!ejd!ej d"ej!d"ej"d#ej#d#ej$d$ej%d$ej&dej'dej(dej)d%ej*d&ej+d&ej,d'ejd(ej-d)ej.d*ej/d+ej0d,ej1d-ej2d-ej3d.ej4d.ej5d/ej6d/e7dej8diZ9d0d1 Z:d2d3 Z;eed4 d5d6 Z<d.d-d/d7Z=d8d9 Z>dKdd:d;d<Z?dLd=d>Z@dMdd?d@dAdBZAdd?dCdDdEZBd
dFdGdHZCdS )NzL
Expressions
-----------

Offer fast expression evaluation through numexpr

    )annotationsN)
get_option)FuncType)NUMEXPR_INSTALLED)	roperatorzbool | None
_TEST_MODEz
list[bool]_TEST_RESULTzFuncType | None	_evaluate_whereZint64Zint32Zfloat64Zfloat32bool)evaluatewherei@B Tc                 C  s$   t r| atrtntatrtntad S N)r   USE_NUMEXPR_evaluate_numexpr_evaluate_standardr	   _where_numexpr_where_standardr
   v r   G/tmp/pip-unpacked-wheel-tiezk1ph/pandas/core/computation/expressions.pyset_use_numexpr)   s    r   c                 C  s&   t r"tr"| d krt } t|  d S r   )r   r   neZdetect_number_of_coresZset_num_threads)nr   r   r   set_numexpr_threads6   s    r   c                 C  s   t rtd | ||S )z
    Standard evaluation.
    F)r   _store_test_result)opop_strabr   r   r   r   ?   s    r   c                 C  sZ   |dk	rV|j tkrVt }||fD ]}t|dr ||jjhO }q t|rRt| |krVdS dS )z,return a boolean if we WILL be using numexprNdtypeTF)size_MIN_ELEMENTSsethasattrr!   namelen_ALLOWED_DTYPES)r   r   r   r    Zdtype_checkZdtypesor   r   r   _can_use_numexprH   s    

r*   c                 C  s   d }t | |||dr| jdd}|r4|| }}|}|}z"tjd| d||ddd}W n6 tk
rr   Y n$ tk
r   t|||rn Y nX |r|| }}t	rt
|d k	 |d krt| |||}|S )	Nr   _rza_value z b_value)a_valueb_valuesafeZ
local_dictZcasting)r*   __name__strip
startswithr   r   	TypeErrorNotImplementedError_bool_arith_fallbackr   r   r   )r   r   r   r    resultZis_reversedr-   r.   r   r   r   r   \   s4    



r   +*-/%z**z==z!=z<=<z>=>&|^c                 C  s   t | ||S r   )npr   )condr   r    r   r   r   r      s    r   c                 C  sD   d }t d d||dr,tjd| ||ddd}|d kr@t| ||}|S )Nr   z#where(cond_value, a_value, b_value))Z
cond_valuer-   r.   r/   r0   )r*   r   r   r   )rC   r   r    r7   r   r   r   r      s    
r   zcompute.use_numexprc                 C  s6   z| j tkW S  tk
r0   t| ttjf Y S X d S r   )r!   r   AttributeError
isinstancerB   Zbool_)xr   r   r   _has_bool_dtype   s    rG   )r8   r9   r:   c                 C  sD   t |r@t |r@| tkr@tdt|  dtt|   d dS dS )z
    Check if we should fallback to the python `_evaluate_standard` in case
    of an unsupported operation by numexpr, which is the case for some
    boolean ops.
    z'evaluating in Python space because the z> operator is not supported by numexpr for the bool dtype, use z insteadTF)rG   _BOOL_OP_UNSUPPORTEDwarningswarnrepr)r   r   r    r   r   r   r6      s    r6   )use_numexprc                 C  s0   t |  }|dk	r"|r"t| |||S t| |||S )z
    Evaluate and return the expression of the op on a and b.

    Parameters
    ----------
    op : the actual operand
    a : left operand
    b : right operand
    use_numexpr : bool, default True
        Whether to try to use numexpr.
    N)_op_str_mappingr	   r   )r   r   r    rL   r   r   r   r   r      s
    r   c                 C  s(   t dk	st|rt | ||S t| ||S )z
    Evaluate the where condition cond on a and b.

    Parameters
    ----------
    cond : np.ndarray[bool]
    a : return if cond is True
    b : return if cond is False
    use_numexpr : bool, default True
        Whether to try to use numexpr.
    N)r
   AssertionErrorr   )rC   r   r    rL   r   r   r   r      s    r   None)r   returnc                 C  s   | a g adS )z
    Keeps track of whether numexpr was used.

    Stores an additional ``True`` for every successful use of evaluate with
    numexpr since the last ``get_test_result``.
    N)r   r   r   r   r   r   set_test_mode  s    rQ   )used_numexprrP   c                 C  s   | rt |  d S r   )r   append)rR   r   r   r   r     s    r   )rP   c                  C  s   t } g a | S )z1
    Get test result and reset test_results.
    )r   )resr   r   r   get_test_result  s    rU   )T)N)T)T)T)D__doc__
__future__r   operatorrI   ZnumpyrB   Zpandas._configr   Zpandas._typingr   Zpandas.core.computation.checkr   Zpandas.core.opsr   Znumexprr   r   __annotations__r   r   r	   r
   r(   r#   r   r   r   r*   r   addZraddmulZrmulsubZrsubtruedivZrtruedivfloordivZ	rfloordivmodZrmodpowZrpoweqleltgegtand_Zrand_or_Zror_xorZrxordivmodZrdivmodrM   r   r   rG   rH   r6   r   r   rQ   r   rU   r   r   r   r   <module>   s   

		*                            $
