
    z-e^6                     (   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             Zej        d             Zej        d             Zej        d             Zej        d	             Zej        d
             Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Zej                            ej         d           G d d                      Z dS )    N)option_context)	DataFrameIndexSeries)expressionsc                      t          t          j                            d                              d          t          d          d          S )N   i'     ABCDfloat64columnsdtyper   nprandomdefault_rngstandard_normallist     =lib/python3.11/site-packages/pandas/tests/test_expressions.py_framer      sF    
	a  00<<V   r   c                      t          t          j                            d                              d          t          d          d          S )Nr	   )d   r   r   r   r   r   r   r   r   _frame2r      sF    
	a  00::V   r   c                     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   s    r   _mixedr+   #   sp    !!####I..##G,,##G,,		
 	
  r   c                     t          | d                                         | d                             d          | d                             d          | d                             d          d          S r   r'   r   s    r   _mixed2r.   /   sp    ""$$$$Y//$$W--$$W--		
 	
  r   c                      t          t          j                            d                              ddd          t          d          d          S )	Nr	      r   r
   sizer   r$   r   r   r   r   r   integersr   r   r   r   _integerr5   ;   sM    
	a  ))!Sz)BBV   r   c                     | t           j                            d                              ddt          j        |                     z  S )Nr	   r   r1   )r   r   r   r4   shape)r5   s    r   _integer_integersr8   D   s>     bi++A..77128HCUCU7VVVVr   c                      t          t          j                            d                              ddd          t          d          d          S )	Nr	   r0   r   )e   r   r1   r   r$   r   r3   r   r   r   	_integer2r;   J   sM    
	a  ))!Sx)@@V   r   c                 @    | d         j                                         S Nr    valuesr(   r*   s    r   _arrayr@   S       #;""$$$r   c                 @    | d         j                                         S r=   r>   r-   s    r   _array2rC   X       3<##%%%r   c                 @    | d         j                                         S Nr%   r>   )r+   s    r   _array_mixedrG   ]   rA   r   c                 @    | d         j                                         S rF   r>   )r.   s    r   _array_mixed2rI   b   rD   r   znot using numexpr)reasonc                   v   e Zd Z ej        d          d             Zededefd            Z	ej
                            dg d          ej
                            ddd	g          ej
                            d
g d          d                                     Zej
                            dg d          ej
                            ddd	g          d                         Zd Zej
                            d          ej
                            dg d          ej
                            dddg          d                                     Zej
                            dddg          d             Zej
                            ddd	g          ej
                            dg d          d                         Zej
                            dg d          d             Zej
                            dg d          d             Zej
                            d eg d g d gg d!"           ed	d	gd	d	ggd#d$g"          f eg d%g d&g d'gg d!"           ed	d	gd	d	gd	d	ggd#d$g"          fg          d(             Zej
                            d
d          ej
                            d)d*          d+                         Zej
                            d,g d-          ej
                            d.eeeg          ej
                            d/d0d1g          d2                                     Zd3S )4TestExpressionsT)autousec              #   B   K   t           j        }d V  |t           _        d S N)expr_MIN_ELEMENTS)selfmin_elementss     r   save_min_elementsz!TestExpressions.save_min_elementsi   s%      ))r   flexopnamec                     |rfd}|_         nt          t                    }t          dd          5   || |          }d d d            n# 1 swxY w Y   t	          j                      || |          }||fS )Nc                 6     t          |           |          S rO   )getattr)xyrV   s     r   <lambda>z)TestExpressions.call_op.<locals>.<lambda>r   s    0ga0033 r   compute.use_numexprF)__name__rY   operatorr   rP   get_test_result)dfotherrU   rV   opexpectedresults      `   r   call_opzTestExpressions.call_opo   s     	+3333B BKK6**B1599 	% 	%r"e}}H	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	Bxs   AAAfixture)r5   r;   r8   r   r   r+   r.   Farith)addsubmulmodtruedivfloordivc                    |                     |          }dt          _        |                     ||||          \  }}|dk    r%t	          d |j        j        D                       sJ t          j        ||           t          t          |j                            D ]h}|                     |j        d d |f         |j        d d |f         ||          \  }}|dk    r|j        j        dk    sJ t          j        ||           id S )Nr   rm   c              3   ,   K   | ]}|j         d k    V  dS )fN)kind).0rZ   s     r   	<genexpr>z6TestExpressions.test_run_arithmetic.<locals>.<genexpr>   s(      EEqv}EEEEEEr   rq   )getfixturevaluerP   rQ   rf   alldtypesr?   tmassert_equalrangelenr   ilocr   rr   )	rR   requestrg   rU   rh   ra   re   rd   is	            r   test_run_arithmeticz#TestExpressions.test_run_arithmetic   s   " $$W--<<Be<<IEEho.DEEEEEEEE
&)))s2:'' 	. 	.A#||BGAAAqDM27111a4=$PUVVFH	!!~*c1111OHf----		. 	.r   c                 X   |                     |          }|j        }t          dd          5  |                                dz   }ddd           n# 1 swxY w Y   dt          _        t	          j        d           |                     ||||          \  }}	t	          j                    }
|
s
J d            t          j
        |	|           t          t          |j                            D ]=}|j        dd|f         dz   }|                     |j        dd|f         ||d           >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]   Fr0   Nr   Tz Did not use numexpr as expected.ri   )ru   r^   r   r(   rP   rQ   set_test_moderf   r`   rx   ry   rz   r{   r   r|   )rR   r}   rg   rU   comparison_opra   rh   rb   re   rd   used_numexprr~   binary_comps                r   test_run_binaryzTestExpressions.test_run_binary   sq   & $$W--&1599 	" 	"GGIIME	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 4   <<E4??+--??????
&)))s2:'' 	B 	BA*QQQT*Q.KLLAT5AAAA	B 	Bs   AAAc                    t           j                            d                              d          }t           j                            d                              d          }t	          j        t          j        d ||d          }|rJ t	          j        t          j        d||d          }|rJ t	          j        t          j        d||d          }|sJ d S )Nr	   iAB r   evaluate+)r   r   r   r   rP   _can_use_numexprr_   ri   )rR   arrayarray2re   s       r   test_invalidzTestExpressions.test_invalid   s    	%%a((88CC&&q))99#>> &x|T5%TT &x|S&&*UU &x|S%TTr   z2ignore:invalid value encountered in:RuntimeWarningzopname,op_str))ri   r   )rj   -)rk   *)rm   /)pow**zleft_fix,right_fix)r@   rC   )rG   rI   c                 R   |                     |          }|                     |          }d }t          dd          5   |||||           d d d            n# 1 swxY w Y   t          j        d            |||||           t          j                      |||||           d S )Nc                 &   |dk    rt          j        |           } t          t          |          }t	          j        || | d          }t	          j        || | d          }t          j        ||           t	          j        ||||d          }|rJ d S )Nr   Tuse_numexprFr   )	r   absrY   r_   rP   r   rx   assert_numpy_array_equalr   )leftrightrV   op_strrc   re   rd   s          r   testitz/TestExpressions.test_binary_ops.<locals>.testit   s    vd||6**B ]2tTtDDDF}RtGGGH'999*2vueZPPFr   r]   Fr0   ru   r   rP   set_numexpr_threads)	rR   r}   rV   r   left_fix	right_fixr   r   r   s	            r   test_binary_opszTestExpressions.test_binary_ops   s    &&x00''	22	 	 	 1599 	0 	0F4///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	 ###tUFF+++ """tUFF+++++s   AA Ac                 H   |                     |          |                     |          fd}t          dd          5   |             d d d            n# 1 swxY w Y   t          j        d            |             t          j                      |             d S )Nc                      dz   } dz   }}t          j        || d          }t          j        || d          }t          j        ||           t          j        |||d          }|rJ d S )Nr0   Tr   Fr   )rP   r   rx   r   r   )f12f22rc   re   rd   r   r   r   s        r   r   z3TestExpressions.test_comparison_ops.<locals>.testit   s    (C!)CB]2tSdCCCF}RsFFFH'999*2r5#zJJFr   r]   Fr0   r   )rR   r}   r   r   r   r   r   r   s     `   @@r   test_comparison_opsz#TestExpressions.test_comparison_ops   s     &&x00''	22	 	 	 	 	 	 	 1599 	 	FHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ### """s   AA #A cond)r   r   r+   r.   c                    |                     |          fd}t          dd          5   |             d d d            n# 1 swxY w Y   t          j        d            |             t          j                      |             d S )Nc                  2   t          j        j        t           j                  } |                                t          j        | j        j        dz             }t          j        | j        j        dz             }t          j	        ||           d S )N)r   r0   )
r   emptyr7   bool_fillrP   wherer?   rx   r   )cre   rd   r   ra   s      r   r   z*TestExpressions.test_where.<locals>.testit  sy    222AFF4LLLZ29bi!m<<Fx29bi!m<<H'99999r   r]   Fr0   r   )rR   r}   r   rg   r   ra   s     `  @r   
test_wherezTestExpressions.test_where  s     $$W--	: 	: 	: 	: 	: 	: 1599 	 	FHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ### """s   AA	A	zop_str,opname))r   rm   )z//rn   )r   r   c                 v   t          t          j                            d                              d          dk    t          j                            d                              d          dk    d          }d| d}t	          t
          |          }t          j        |          }t          j	        t          |          5   |||           d d d            n# 1 swxY w Y   t          j	        t          |          5   ||j        |j                   d d d            n# 1 swxY w Y   t          j	        t          |          5   ||j        d           d d d            n# 1 swxY w Y   t          j	        t          |          5   |d	|j                   d d d            n# 1 swxY w Y   t          j	        t          |          5   |d	|           d d d            n# 1 swxY w Y   t          j	        t          |          5   ||d           d d d            d S # 1 swxY w Y   d S )
Nr	   
         ?abz
operator 'z!' not implemented for bool dtypes)matchTF)r   r   r   r   rY   r_   reescapepytestraisesNotImplementedErrorr   r   )rR   r   rV   ra   msgrq   err_msgs          r   !test_bool_ops_raise_on_arithmeticz1TestExpressions.test_bool_ops_raise_on_arithmetic%  s    Y**1--44R883>Y**1--44R883> 
 
 E6DDDHf%%)C..].g>>> 	 	Ab"III	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ].g>>> 	 	AbdBDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ].g>>> 	 	AbdDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ].g>>> 	 	AeRTNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ].g>>> 	 	AeRLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ].g>>> 	 	Ab$KKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sl   CC"%C"D''D+.D+E++E/2E/F//F36F3G..G25G2H..H25H2))r   ri   )r   rk   )r   rj   c                 `   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          5  t          j                    5   |||          }	 |||          }
t          j	        |	|
           d d d            n# 1 swxY w Y   t          j                    5   ||j
        |j                  }	 ||j
        |j                  }
t          j        |	|
           d d d            n# 1 swxY w Y   t          j                    5   ||j
        d          }	 ||j
        d          }
t          j        |	|
           d d d            n# 1 swxY w Y   t          j                    5   |d|j
                  }	 |d|j
                  }
t          j        |	|
           d d d            n# 1 swxY w Y   t          j                    5   |d|          }	 |d|          }
t          j	        |	|
           d d d            n# 1 swxY w Y   t          j                    5   ||d          }	 ||d          }
t          j	        |	|
           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   r	   r   r   |&^)r   r   r   or_and_xor)r   r   r   r   T   )rS   F)r   r   r   r   rY   r_   rx   r   assert_produces_warningassert_frame_equalr   r   assert_series_equal)rR   r   rV   nra   subs	sub_funcsrq   feres              r    test_bool_ops_warn_on_arithmeticz0TestExpressions.test_bool_ops_warn_on_arithmeticF  s    Y**1--44Q77#=Y**1--44Q77#= 
 
 s--f599	Hf%%Xyf677S==F^Dq111 	, 	,+-- , ,Ab"IIBr2JJ%a+++, , , , , , , , , , , , , , ,
 +-- - -AbdBDMMBrtRTNN&q!,,,- - - - - - - - - - - - - - -
 +-- - -AbdDMMBrtTNN&q!,,,- - - - - - - - - - - - - - -
 +-- - -AeRTNNBubdOO&q!,,,- - - - - - - - - - - - - - -
 +-- , ,AeRLLBubMM%a+++, , , , , , , , , , , , , , ,
 +-- , ,Ab$KKBr4LL%a+++, , , , , , , , , , , , , , ,5	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   L#1.D+L#+D/	/L#2D/	3L#	AFL#F	L#F	L#58G9-L#9G=	=L# G=	L#8IL#I	L#"I	#L#9.J3'L#3J7	7L#:J7	;L#.L?L#L	L#L	L##L'*L'ztest_input,expected)r   r0   r	   aa)r   r   r   r   )r   r   r   )r      r	   r   )r   r   r	   r   )r   r0   r0   bbc                     |j         d d ddgf                             |j         d d ddgf                   }t          j        ||           d S )Nr   r   )locnerx   r   )rR   
test_inputrd   re   s       r   test_bool_ops_column_name_dtypez/TestExpressions.test_bool_ops_column_name_dtype{  sX    . C> 1255jnQQQgEV6WXX
fh/////r   axis)r   r0   c                 B   |}|dk    r|j         dd d f         }n|j         d d df         }dt          _        t          ||          }t	          dd          5   |||          }d d d            n# 1 swxY w Y    |||          }t          j        ||           d S )Nr0   r   r]   F)r   )r|   rP   rQ   rY   r   rx   r   )	rR   r   rh   r   ra   rb   op_funcrd   re   s	            r   test_frame_series_axisz&TestExpressions.test_frame_series_axis  s     199GAqqqDMEEGAAAqDME"e$$1599 	1 	1wu4000H	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 T***
h/////s   A22A69A6rc   )__mod____rmod____floordiv____rfloordiv__boxscalarr   c                    dt           _        t          j        dd          } ||          }t	          ||          } ||          }t          dd          5   ||          }d d d            n# 1 swxY w Y   t          j        ||           t          |          D ]g\  }	}
|t          k    r|j
        |	df         }n||	         }	  t	          t          |
          |          |          }||k    sJ X# t          $ r Y dw xY wd S )Nr   i2   r]   F)rP   rQ   r   arangerY   r   rx   ry   	enumerater   r|   intZeroDivisionError)rR   rc   r   r   dataobjmethodre   rd   r~   elemscalar_results               r   ,test_python_semantics_with_numexpr_installedz<TestExpressions.test_python_semantics_with_numexpr_installed  sv    yb!!c$iib!! 1599 	& 	&vf~~H	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	))) ! 
	1 
	1GAti &AqD 1 &q	1173t99b11&99 %00000 %   
	1 
	1s$   A00A47A4&C77
DDN)r^   
__module____qualname__r   rg   rT   staticmethodboolstrrf   markparametrizer   r   r   filterwarningsr   r   r   r   r   r   r   r   r   r   r   r   r   r   rL   rL   g   s       V^D!!!* * "!*
    s       \  [	
 	
 	
  [VdE]33[DDD . .  43  . [	
 	
 	
  [VdE]33B B 43 B0    [ TUU[SSS  [46WX , , 	  VU,4 [46WX   2 [VdE]33[Y(R(R(RSS  TS 43$ [NNN   < [CCC 0, 0, 0,d [ 	$__ooo6@X@X@X   	E5>E5>:S'NSSS	 	$__oooG444   	U^eU^eU^D 'N  		
 *0 0+ *0
 [D  [VV,,0 0 -, 0& [	
 	
 	
  [UY$>??[XAw//1 1 0/ @? 1 1 1r   rL   )!r_   r   numpyr   r   pandasr   pandas._testing_testingrx   pandas.core.apir   r   r   pandas.core.computationr   rP   rg   r   r   r+   r.   r5   r8   r;   r@   rC   rG   rI   r   skipifUSE_NUMEXPRrL   r   r   r   <module>r      s>    				      ! ! ! ! ! !               
 8 7 7 7 7 7                W W W
    % % % & & & % % % & & & ((1DEEh1 h1 h1 h1 h1 h1 h1 FEh1 h1 h1r   