U
    c                  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 ddlmZ errd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ddZdJddd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j0d,ej1d-ej2d.ej3d/ej4d/ej5d0ej6d0ej7d1ej8d1e9dej:diZ;d2d3 Z<d4d5 Z=eed6 d7d8 Z>d0d/d1d9Z?d:d; Z@dKdd<d=d>ZAdLd?d@ZBdMdddAdBdCZCdddDdEdFZDdddGdHZEdS )NzL
Expressions
-----------

Offer fast expression evaluation through numexpr

    )annotationsN)
get_option)FuncType)find_stack_level)NUMEXPR_INSTALLED)	roperatorzbool | None
_TEST_MODEz
list[bool]_TEST_RESULTzFuncType | None	_evaluate_whereZint64Zint32Zfloat64Zfloat32bool)evaluatewherei@B TNone)returnc                 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-g7fro6k3/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_threads7   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_numexprI   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_reversedr0   r1   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_valuer0   r1   r2   r3   )r-   r   r   r   )rE   r"   r#   r:   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
isinstancerD   Zbool_)xr   r   r   _has_bool_dtype   s    rI   )r;   r<   r=   c                 C  sJ   t |rFt |rF| tkrFtjdt|  dtt|   d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	 instead.)
stacklevelTF)rI   _BOOL_OP_UNSUPPORTEDwarningswarnreprr   )r!   r"   r#   r   r   r   r9      s    r9   )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#   rO   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   )rE   r"   r#   rO   r   r   r   r      s    r   )r   r   c                 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    rR   )used_numexprr   c                 C  s   | rt |  d S r   )r	   append)rS   r   r   r   r     s    r   c                  C  s   t } g a | S )z1
    Get test result and reset test_results.
    )r	   )resr   r   r   get_test_result  s    rV   )T)N)T)T)T)F__doc__
__future__r   operatorrL   ZnumpyrD   Zpandas._configr   Zpandas._typingr   Zpandas.util._exceptionsr   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rdivmodrP   r   r   rI   rK   r9   r   r   rR   r   rV   r   r   r   r   <module>   s   		*                            $
