U
    f/e2                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZ d dlmZ e	ejddedddZe	ejd	dedddZe	ed
  ed ded ded ddZe	ed
  ed ded ded ddZe	ejjdd	ddedddZe	ejjdd	ddedddZed
 j Zed
 j Zed j Zed j Zej j!ej" ddG dd dZ#dS )    N)	DataFrameIndexSeries)expressions'     ABCDZfloat64)columnsdtyped   ABZfloat32CZint64DZint32)r   r   r   r      )r   r   size)e   r   znot using numexprreasonc                   @   s  e Zd Zdd Zdd ZeeedddZedd	d
Z	edddZ
edddZejdeeeejjddeed eeeegejdddgdd Zdd Zejddddddgejd eefeefgd!d" Zejdd#d$d%d&d'd(gejd eefeefgd)d* Z ejd+ddgejdeeeegd,d- Z!ejd.d/d0d1gd2d3 Z"ejd.d4d5d6gd7d8 Z#ejd9e$dd:dd;gdd:dd;ggd<d=d>d?gd@e$ddgddggd<d?gd@fe$ddAdd;gddBdd;gdd:d:dCggd<d=d>d?gd@e$ddgddgddggd<d?gd@fgdDdE Z%ejdFdGejdHdIdJdK Z&ejdLdMej'dNejj(dOdPdQdRdSgejdTe$e)e*gejdUdVdWgdXdY Z+dZS )[TestExpressionsc                 C   s4   t  | _t | _t | _t | _t	j
| _
d S N)_framecopyframe_frame2Zframe2_mixedmixed_mixed2Zmixed2expr_MIN_ELEMENTSselfmethod r$   A/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/test_expressions.pysetup_method0   s
    



zTestExpressions.setup_methodc                 C   s   | j t_ d S r   )r    r   r!   r$   r$   r%   teardown_method8   s    zTestExpressions.teardown_method)flexopnamec                    sZ   |r fdd} |_ n
tt }td || |}td t  || |}||fS )Nc                    s   t |  |S r   )getattr)xyr)   r$   r%   <lambda>>       z)TestExpressions.call_op.<locals>.<lambda>FT)__name__r*   operatorr   set_use_numexprget_test_result)dfotherr(   r)   opexpectedresultr$   r-   r%   call_op;   s    




zTestExpressions.call_op)r(   c                 C   s   dt _ddddddg}|D ]b}| ||||\}}|dkrp|jdkrV|jjd	ksptntd
d |jj	D sptt
|| qd S )Nr   addsubmulmodtruedivfloordivr   fc                 s   s   | ]}|j d kV  qdS )r@   N)kind).0r+   r$   r$   r%   	<genexpr>V   s     z1TestExpressions.run_arithmetic.<locals>.<genexpr>)r   r    r9   ndimr
   rA   AssertionErrorallZdtypesvaluestmassert_equal)r"   r4   r5   r(   
operationsarithr8   r7   r$   r$   r%   run_arithmeticL   s    
zTestExpressions.run_arithmeticc           	      C   sb   dt _t d ddddddg}|D ]8}| ||||\}}t  }|sPtd	t|| q$d
S )z
        tests solely that the result is the same whether or not numexpr is
        enabled.  Need to test whether the function does the correct thing
        elsewhere.
        r   Tgtltgeleeqnez Did not use numexpr as expected.N)r   r    Zset_test_moder9   r3   rE   rH   rI   )	r"   r4   r5   r(   rJ   rK   r8   r7   Zused_numexprr$   r$   r%   
run_binaryY   s    
zTestExpressions.run_binaryc                 C   sz   |  ||| td |d }td | ||| tt|jD ].}|  |jd d |f |jd d |f | qFd S )NFr   T)rL   r   r2   rS   rangelenr	   iloc)r"   r4   r5   r(   Zbinary_compir$   r$   r%   	run_framej   s    

zTestExpressions.run_framer4   r      r   r(   TFc                 C   s   |  ||| d S r   )rX   )r"   r4   r(   r$   r$   r%   test_arithmeticy   s    zTestExpressions.test_arithmeticc                 C   sp   t jd}t jd}ttjd ||d}|r4tttjd||d}|rPtttjd||d}|sltd S )NiAB r   evaluate+)nprandomrandnr   _can_use_numexprr1   r:   rE   )r"   arrayZarray2r8   r$   r$   r%   test_invalid   s    zTestExpressions.test_invalidzopname,op_str)r:   r\   )r;   -)r<   *)r>   /)pow**z
left,rightc                    sN    fdd}t d |  t d t d |  t   |  d S )Nc                     sd   dkrd S t t} tj|   dd}tj|   dd}t|| t| d}|r`td S )Nrf   Tuse_numexprFr[   r*   r1   r   r[   rH   assert_numpy_array_equalr`   rE   )r6   r8   r7   leftop_strr)   rightr$   r%   testit   s    
z/TestExpressions.test_binary_ops.<locals>.testitFTr   r   r2   Zset_numexpr_threadsr"   r)   rn   rm   ro   rp   r$   rl   r%   test_binary_ops   s    


zTestExpressions.test_binary_ops)rM   >)rN   <)rO   z>=)rP   z<=)rQ   z==)rR   z!=c                    sN    fdd}t d |  t d t d |  t   |  d S )Nc                     sh    d } d }t t}tj| | dd}tj| | dd}t|| t||d}|rdtd S )Nr   Trh   Fr[   rj   )Zf12Zf22r6   r8   r7   rl   r$   r%   rp      s    
z3TestExpressions.test_comparison_ops.<locals>.testitFTr   rq   rr   r$   rl   r%   test_comparison_ops   s    


z#TestExpressions.test_comparison_opscondc                    sJ    fdd}t d |  t d t d |  t   |  d S )Nc                     sX   t jjt jd} |   t| jjd }t | jjd }t	|| d S )N)r
   r   )
r]   emptyshapeZbool_fillr   whererG   rH   rk   )cr8   r7   rw   r4   r$   r%   rp      s
    
z*TestExpressions.test_where.<locals>.testitFTr   rq   )r"   rw   r4   rp   r$   r}   r%   
test_where   s    


zTestExpressions.test_wherezop_str,opname)re   r>   )z//r?   )rg   rf   c              	   C   s*  t tjddktjddkd}d| d}tt|}t|}tj	t
|d ||| W 5 Q R X tj	t
|d ||j|j W 5 Q R X tj	t
|d ||jd W 5 Q R X tj	t
|d |d|j W 5 Q R X tj	t
|d |d| W 5 Q R X tj	t
|d ||d W 5 Q R X d S )	N
         ?abz
operator 'z!' not implemented for bool dtypes)matchTF)r   r]   r^   randr*   r1   reescapepytestZraisesNotImplementedErrorr   r   )r"   rn   r)   r4   msgr@   err_msgr$   r$   r%   !test_bool_ops_raise_on_arithmetic   s     &

z1TestExpressions.test_bool_ops_raise_on_arithmetic)r\   r:   )rd   r<   )rc   r;   c              
   C   s  d}t tj|dktj|dkd}dddd}dd	d
d}tt|}tt|||  }|dkrjd S tjdddf tjdd& |||}	|||}
t	|	|
 W 5 Q R X tjdd. ||j
|j}	||j
|j}
t|	|
 W 5 Q R X tjdd* ||j
d}	||j
d}
t|	|
 W 5 Q R X tjdd* |d|j
}	|d|j
}
t|	|
 W 5 Q R X tjdd& |d|}	|d|}
t	|	|
 W 5 Q R X tjdd& ||d}	||d}
t	|	|
 W 5 Q R X W 5 Q R X d S )Nr   r   r   |&^)r\   rd   rc   or_and_xor)r   r   r   rc   T   )Zmin_elementsF)Zcheck_stacklevel)r   r]   r^   r   r*   r1   rH   ri   Zassert_produces_warningassert_frame_equalr   r   Zassert_series_equal)r"   rn   r)   nr4   subsZ	sub_funcsr@   Zferer$   r$   r%    test_bool_ops_warn_on_arithmetic  sB    &






z0TestExpressions.test_bool_ops_warn_on_arithmeticztest_input,expectedr   Zaar   r   r|   r
   )r	      r   Zbbc                 C   s>   |j d d ddgf |j d d ddgf }t|| d S )Nr   r
   )locrR   rH   r   )r"   Z
test_inputr7   r8   r$   r$   r%   test_bool_ops_column_name_dtype;  s    .z/TestExpressions.test_bool_ops_column_name_dtyperK   )r:   r;   r<   r=   r>   r?   axis)r   r   c                 C   s   | j }|dkr$| j jdd d f }n| j jd d df }dt_t||}td |||d}td |||d}t|| d S )Nr   r   F)r   T)r   rV   r   r    r*   r2   rH   r   )r"   r   rK   r4   r5   Zop_funcr7   r8   r$   r$   r%   test_frame_series_axisU  s    


z&TestExpressions.test_frame_series_axisr6   __mod____rmod__zGH-36552r   )Zmarks__floordiv____rfloordiv__boxscalarr   c              	   C   s   dt _tdd}||}t||}||}t d ||}t d t|| t|D ]`\}	}
|t	kr||j
|	df }n||	 }ztt|
||}W n tk
r   Y q\X ||ks\tq\d S )Nr   i2   FT)r   r    r]   Zaranger*   r2   rH   rI   	enumerater   rV   intZeroDivisionErrorrE   )r"   r6   r   r   dataobjr#   r8   r7   rW   elemZscalar_resultr$   r$   r%   ,test_python_semantics_with_numexpr_installedm  s$    


z<TestExpressions.test_python_semantics_with_numexpr_installedN),r0   
__module____qualname__r&   r'   staticmethodboolstrr9   rL   rS   rX   r   markZparametrize_integer	_integer2r]   r^   randintry   r   r   r   r   rZ   rb   _array_array2_array_mixed_array_mixed2rs   rv   r~   r   r   r   r   r   paramZxfailr   r   r   r$   r$   r$   r%   r   .   s      
 
- 
 

 	r   )$r1   r   Znumpyr]   r   Zpandas._testingZ_testingrH   Zpandas.core.apir   r   r   Zpandas.core.computationr   r   r^   r_   listr   r   r   Zastyper   r   r   r   r   rG   r   r   r   r   r   ZskipifZUSE_NUMEXPRr   r$   r$   r$   r%   <module>   sL   

    