a
    GGbl3                     @   s  d dl Z 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
 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)
set_option)	DataFrameIndexSeries)expressions'     Z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	j
deeeejjd	d
eed eeeege	j
dddge	j
dg 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g de	j
deefeefgdd Ze	j
dg d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"g d#d$d% Z e	j
d"g d&d'd( Z!e	j
d)e"g d*g d*gg d+d,e"ddgddggd-d.gd,fe"g d/g d0g d1gg d+d,e"ddgddgddggd-d.gd,fgd2d3 Z#e	j
dde	j
d4d5d6d7 Z$e	j
d8g d9e	j
d:e"e%e&ge	j
d;d<d=gd>d? Z'd@S )ATestExpressionsc                 C   s   t j| _d S N)expr_MIN_ELEMENTSselfmethod r   <lib/python3.9/site-packages/pandas/tests/test_expressions.pysetup_method2   s    zTestExpressions.setup_methodc                 C   s   | j t_ d S r   )r   r   r   r   r   r   teardown_method5   s    zTestExpressions.teardown_method)flexopnamec                    sZ   |r fdd} |_ n
tt }tdd || |}tdd t  || |}||fS )Nc                    s   t |  |S r   )getattr)xyr!   r   r   <lambda>;       z)TestExpressions.call_op.<locals>.<lambda>compute.use_numexprFT)__name__r"   operatorr   r   get_test_result)dfotherr    r!   opexpectedresultr   r%   r   call_op8   s    




zTestExpressions.call_opr,   r      r   r    TFarith)addsubmulmodtruedivfloordivc                 C   s   dt _| ||||\}}|dkr<tdd |jjD s<J t|| tt	|j
D ]X}| |jd d |f |jd d |f ||\}}|dkr|jjdksJ t|| qVd S )Nr   r8   c                 s   s   | ]}|j d kV  qdS )fN)kind).0r#   r   r   r   	<genexpr>_   r'   z6TestExpressions.test_run_arithmetic.<locals>.<genexpr>r:   )r   r   r1   allZdtypesvaluestmassert_equalrangelenr	   ilocr
   r;   )r   r,   r    r3   r0   r/   ir   r   r   test_run_arithmeticI   s    0z#TestExpressions.test_run_arithmeticc           	      C   sn   |j }tdd | d }tdd dt_td | ||||\}}t }|s^J dt	|| 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(   Fr   Tr   z Did not use numexpr as expected.N)
r)   r   copyr   r   Zset_test_moder1   r+   r@   rA   )	r   r,   r    Zcomparison_opr3   r-   r0   r/   Zused_numexprr   r   r   test_run_binaryh   s    


zTestExpressions.test_run_binaryc                 C   sp   t jd}t jd}ttjd ||d}|r4J ttjd||d}|rPJ ttjd||d}|slJ d S )NiAB r   evaluate+)nprandomrandnr   _can_use_numexprr*   r4   )r   ZarrayZarray2r0   r   r   r   test_invalid   s    zTestExpressions.test_invalidzopname,op_str))r4   rJ   )r5   -)r6   *)r8   /)pow**z
left,rightc                    sN    fdd}t dd |  t dd td |  t  |  d S )Nc                     s   dkrd S t t} t F d}td|t tj|   dd}tj|   dd}W d    n1 sj0    Y  t	|| t
| d}|rJ d S )NrS   z(invalid value encountered in true_divideignoreTuse_numexprFrI   )r"   r*   warningscatch_warningsfilterwarningsRuntimeWarningr   rI   r@   assert_numpy_array_equalrN   )r.   msgr0   r/   leftop_strr!   rightr   r   testit   s    

0z/TestExpressions.test_binary_ops.<locals>.testitr(   FTr   r   r   Zset_numexpr_threadsr   r!   r`   r_   ra   rb   r   r^   r   test_binary_ops   s    


zTestExpressions.test_binary_ops))gt>)lt<)gez>=)lez<=)eqz==)nez!=c                    sN    fdd}t dd |  t dd td |  t  |  d S )Nc                     sh    d } d }t t}tj| | dd}tj| | dd}t|| t||d}|rdJ d S )Nr   TrV   FrI   )r"   r*   r   rI   r@   r\   rN   )Zf12Zf22r.   r0   r/   r^   r   r   rb      s    
z3TestExpressions.test_comparison_ops.<locals>.testitr(   FTr   rc   rd   r   r^   r   test_comparison_ops   s    


z#TestExpressions.test_comparison_opscondc                    sJ    fdd}t dd |  t d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   )
rK   emptyshapeZbool_Zfillr   wherer?   r@   r\   )cr0   r/   ro   r,   r   r   rb      s
    
z*TestExpressions.test_where.<locals>.testitr(   FTr   rc   )r   ro   r,   rb   r   rt   r   
test_where   s    


zTestExpressions.test_wherezop_str,opname))rR   r8   )z//r9   )rT   rS   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 d    n1 st0    Y  tj	t
|d ||j|j W d    n1 s0    Y  tj	t
|d ||jd W d    n1 s0    Y  tj	t
|d |d|j W d    n1 s&0    Y  tj	t
|d |d| W d    n1 s`0    Y  tj	t
|d ||d W d    n1 s0    Y  d S )	N
         ?abz
operator 'z!' not implemented for bool dtypes)matchTF)r   rK   rL   randr"   r*   reescapepytestZraisesNotImplementedErrorry   rz   )r   r`   r!   r,   r]   r:   err_msgr   r   r   !test_bool_ops_raise_on_arithmetic   s     &

(,*,*z1TestExpressions.test_bool_ops_raise_on_arithmetic))rJ   r4   )rQ   r6   )rP   r5   c              	   C   sj  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 t 0 |||}	|||}
t	|	|
 W d    n1 s0    Y  t 8 ||j
|j}	||j
|j}
t|	|
 W d    n1 s0    Y  t 4 ||j
d}	||j
d}
t|	|
 W d    n1 sZ0    Y  t 4 |d|j
}	|d|j
}
t|	|
 W d    n1 s0    Y  t 0 |d|}	|d|}
t	|	|
 W d    n1 s0    Y  t 0 ||d}	||d}
t	|	|
 W d    n1 s<0    Y  W d    n1 s\0    Y  d S )Nrv   rw   rx   |&^)rJ   rQ   rP   or_and_xor)r   r   r   rP   T   )Zmin_elementsF)r   rK   rL   r|   r"   r*   r@   rW   Zassert_produces_warningassert_frame_equalry   rz   Zassert_series_equal)r   r`   r!   nr,   Z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,expected)r   r   r2   aa)ry   rz   rs   r
   )r	   ry   r
   )r      r2   r   )r   r   r2   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 )Nry   r
   )Zlocrm   r@   r   )r   Z
test_inputr/   r0   r   r   r   test_bool_ops_column_name_dtypeE  s    .z/TestExpressions.test_bool_ops_column_name_dtypeaxis)r   r   c                 C   s~   t }|dkr |jdd d f }n|jd d df }dt_t||}tdd |||d}tdd |||d}t|| d S )Nr   r   r(   F)r   T)_framerD   r   r   r"   r   r@   r   )r   r   r3   r,   r-   Zop_funcr/   r0   r   r   r   test_frame_series_axis_  s    


z&TestExpressions.test_frame_series_axisr.   )__mod____rmod____floordiv____rfloordiv__boxscalarr   c              	   C   s   dt _tdd}||}t||}||}tdd ||}tdd t|| t|D ]^\}	}
|t	kr||j
|	df }n||	 }ztt|
||}W n ty   Y q\0 ||ks\J q\d S )Nr   i2   r(   FT)r   r   rK   Zaranger"   r   r@   rA   	enumerater   rD   intZeroDivisionError)r   r.   r   r   dataobjr   r0   r/   rE   elemZscalar_resultr   r   r   ,test_python_semantics_with_numexpr_installedw  s$    


z<TestExpressions.test_python_semantics_with_numexpr_installedN)(r)   
__module____qualname__r   r   staticmethodboolstrr1   r   markZparametrize_integer	_integer2rK   rL   randintrq   r   _frame2_mixed_mixed2rF   rH   rO   _array_array2_array_mixed_array_mixed2re   rn   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   0   s   

-
	r   )'r*   r}   rX   ZnumpyrK   r   Zpandasr   Zpandas._testingZ_testingr@   Zpandas.core.apir   r   r   Zpandas.core.computationr   r   rL   rM   listr   r   rG   Zastyper   r   r   r   r   r?   r   r   r   r   r   ZskipifZUSE_NUMEXPRr   r   r   r   r   <module>   sH   

