o
    ҷh^6                     @   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	 d dl
mZmZmZ d dlmZ ejdd Zejdd Zejd	d
 Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejjej ddG dd dZ dS )    N)option_context)	DataFrameIndexSeries)expressionsc                   C       t tjddtdddS )N   i'     ABCDfloat64columnsdtyper   nprandomdefault_rngstandard_normallist r   r   P/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/test_expressions.py_frame   
   r   c                   C   r   )Nr   )d   r
   r   r   r   r   r   r   r   r   _frame2   r   r   c                 C   8   t | d  | d d| d d| d ddS 	NABfloat32Cint64Dint32)r   r   r!   r#   r   copyastyper   r   r   r   _mixed#      
r)   c                 C   r   r   r%   r   r   r   r   _mixed2/   r*   r,   c                   C   &   t tjdjddddtdddS )	Nr      r   r	   sizer   r"   r   r   r   r   r   integersr   r   r   r   r   _integer;   
   r3   c                 C   s"   | t jdjddt | d S )Nr   r   r/   )r   r   r   r2   shape)r3   r   r   r   _integer_integersD   s   "r6   c                   C   r-   )	Nr   r.   r   )e   r
   r/   r   r"   r   r1   r   r   r   r   	_integer2J   r4   r8   c                 C      | d j  S Nr   valuesr&   r(   r   r   r   _arrayS      r=   c                 C   r9   r:   r;   r+   r   r   r   _array2X   r>   r?   c                 C   r9   Nr#   r;   )r)   r   r   r   _array_mixed]   r>   rA   c                 C   r9   r@   r;   )r,   r   r   r   _array_mixed2b   r>   rB   znot using numexpr)reasonc                
   @   sL  e Zd Zejdddd ZededefddZ	ej
d	g d
ej
dddgej
dg ddd Zej
d	g d
ej
dddgdd Zdd Zej
dej
dg dej
dddgdd Zej
dddgdd Zej
dddgej
d	g d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TestExpressionsT)autousec                 c   s    t j}d V  |t _d S N)expr_MIN_ELEMENTS)selfmin_elementsr   r   r   save_min_elementsi   s   
z!TestExpressions.save_min_elementsflexopnamec                    sp   |r fdd} |_ ntt }tdd || |}W d    n1 s&w   Y  t  || |}||fS )Nc                    s   t |  |S rF   )getattr)xyrM   r   r   <lambda>r   s    z)TestExpressions.call_op.<locals>.<lambda>compute.use_numexprF)__name__rN   operatorr   rG   get_test_result)dfotherrL   rM   opexpectedresultr   rQ   r   call_opo   s   

zTestExpressions.call_opfixture)r3   r8   r6   r   r   r)   r,   F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 ],}| |jd d |f |jd d |f ||\}}|dkrV|jjdksVJ t|| q0d S )Nr   rc   c                 s   s    | ]}|j d kV  qdS )fN)kind).0rO   r   r   r   	<genexpr>   s    z6TestExpressions.test_run_arithmetic.<locals>.<genexpr>re   )getfixturevaluerG   rH   r\   alldtypesr<   tmassert_equalrangelenr   ilocr   rf   )	rI   requestr]   rL   r^   rW   r[   rZ   ir   r   r   test_run_arithmetic   s   
0z#TestExpressions.test_run_arithmeticc                 C   s   | |}|j}tdd | d }W d   n1 sw   Y  dt_td | ||||\}}	t }
|
s?J dt	
|	| tt|jD ]}|jdd|f d }| |jdd|f ||d qLd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.
        rS   Fr.   Nr   Tz Did not use numexpr as expected.r_   )ri   rT   r   r&   rG   rH   set_test_moder\   rV   rl   rm   rn   ro   r   rp   )rI   rq   r]   rL   comparison_oprW   r^   rX   r[   rZ   used_numexprrr   binary_compr   r   r   test_run_binary   s   

 zTestExpressions.test_run_binaryc                 C   s|   t jdd}t jdd}ttjd ||d}|r J ttjd||d}|r.J ttjd||d}|s<J d S )Nr   iAB r   evaluate+)r   r   r   r   rG   _can_use_numexprrU   r_   )rI   arrayarray2r[   r   r   r   test_invalid   s   zTestExpressions.test_invalidz2ignore:invalid value encountered in:RuntimeWarningzopname,op_str))r_   rz   )r`   -)ra   *)rc   /)pow**zleft_fix,right_fix)r=   r?   )rA   rB   c           	      C   s   | |}| |}dd }tdd ||||| W d    n1 s%w   Y  td ||||| t  ||||| d S )Nc                 S   sj   |dkr	t | } tt|}tj|| | dd}tj|| | dd}t|| t||||d}|r3J d S )Nr   Tuse_numexprFry   )	r   absrN   rU   rG   ry   rl   assert_numpy_array_equalr{   )leftrightrM   op_strrY   r[   rZ   r   r   r   testit   s   

z/TestExpressions.test_binary_ops.<locals>.testitrS   Fr.   ri   r   rG   set_numexpr_threads)	rI   rq   rM   r   left_fix	right_fixr   r   r   r   r   r   test_binary_ops   s   
	

zTestExpressions.test_binary_opsc                    sv   | || | fdd}tdd |  W d    n1 s%w   Y  td |  t  |  d S )Nc                     sb   d } d } }t j|| dd}t j|| dd}t|| t |||d}|r/J d S )Nr.   Tr   Fry   )rG   ry   rl   r   r{   )f12f22rY   r[   rZ   ru   r   r   r   r   r      s   z3TestExpressions.test_comparison_ops.<locals>.testitrS   Fr.   r   )rI   rq   ru   r   r   r   r   r   r   test_comparison_ops   s   



z#TestExpressions.test_comparison_opscond)r   r   r)   r,   c                    sj   | | fdd}tdd |  W d    n1 sw   Y  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   emptyr5   bool_fillrG   wherer<   rl   r   )cr[   rZ   r   rW   r   r   r     s
   
z*TestExpressions.test_where.<locals>.testitrS   Fr.   r   )rI   rq   r   r]   r   r   r   r   
test_where  s   


zTestExpressions.test_wherezop_str,opname))r   rc   )z//rd   )r   r   c                 C   s  t tjdddktjdddkd}d| d}tt|}t|}tj	t
|d ||| W d    n1 s@w   Y  tj	t
|d ||j|j W d    n1 s^w   Y  tj	t
|d ||jd W d    n1 s{w   Y  tj	t
|d |d	|j W d    n1 sw   Y  tj	t
|d |d	| W d    n1 sw   Y  tj	t
|d ||d W d    d S 1 sw   Y  d S )
Nr   
         ?abz
operator 'z!' not implemented for bool dtypes)matchTF)r   r   r   r   rN   rU   reescapepytestraisesNotImplementedErrorr   r   )rI   r   rM   rW   msgre   err_msgr   r   r   !test_bool_ops_raise_on_arithmetic%  s4   

"z1TestExpressions.test_bool_ops_raise_on_arithmetic))rz   r_   )r   ra   )r   r`   c              	   C   s~  d}t tjd|dktjd|dkd}dddd}d	d
dd}tt|}tt|||  }|dkr;d S tjddd t  |||}	|||}
t	|	|
 W d    n1 sbw   Y  t  ||j
|j}	||j
|j}
t|	|
 W d    n1 sw   Y  t  ||j
d}	||j
d}
t|	|
 W d    n1 sw   Y  t  |d|j
}	|d|j
}
t|	|
 W d    n1 sw   Y  t  |d|}	|d|}
t	|	|
 W d    n1 sw   Y  t  ||d}	||d}
t	|	|
 W d    n1 sw   Y  W d    d S W d    d S 1 s8w   Y  d S )Nr   r   r   r   |&^)rz   r   r   or_and_xor)r   r   r   r   T   )rJ   F)r   r   r   r   rN   rU   rl   r   assert_produces_warningassert_frame_equalr   r   assert_series_equal)rI   r   rM   nrW   subs	sub_funcsre   ferer   r   r    test_bool_ops_warn_on_arithmeticF  s\   












 $z0TestExpressions.test_bool_ops_warn_on_arithmeticztest_input,expected)r   r.   r   aa)r   r   r   r   )r   r   r   )r      r   r   )r   r
   r   r   )r   r.   r.   bbc                 C   s>   |j d d ddgf |j d d ddgf }t|| d S )Nr   r   )locnerl   r   )rI   
test_inputrZ   r[   r   r   r   test_bool_ops_column_name_dtype{  s   .z/TestExpressions.test_bool_ops_column_name_dtypeaxis)r   r.   c           	      C   s   |}|dkr|j dd d f }n	|j d d df }dt_t||}tdd |||d}W d    n1 s7w   Y  |||d}t|| d S )Nr.   r   rS   F)r   )rp   rG   rH   rN   r   rl   r   )	rI   r   r^   r   rW   rX   op_funcrZ   r[   r   r   r   test_frame_series_axis  s   
z&TestExpressions.test_frame_series_axisrY   )__mod____rmod____floordiv____rfloordiv__boxscalarr   c              	   C   s   dt _tdd}||}t||}||}tdd ||}W d    n1 s*w   Y  t|| t|D ]/\}	}
|t	krI|j
|	df }n||	 }ztt|
||}W n	 tya   Y q9w ||kshJ q9d S )Nr   i2   rS   F)rG   rH   r   arangerN   r   rl   rm   	enumerater   rp   intZeroDivisionError)rI   rY   r   r   dataobjmethodr[   rZ   rr   elemscalar_resultr   r   r   ,test_python_semantics_with_numexpr_installed  s(   

z<TestExpressions.test_python_semantics_with_numexpr_installedN)rT   
__module____qualname__r   r]   rK   staticmethodboolstrr\   markparametrizers   rx   r~   filterwarningsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rD   g   s    





2
	rD   )!rU   r   numpyr   r   pandasr   pandas._testing_testingrl   pandas.core.apir   r   r   pandas.core.computationr   rG   r]   r   r   r)   r,   r3   r6   r8   r=   r?   rA   rB   r   skipifUSE_NUMEXPRrD   r   r   r   r   <module>   s@    










