o
    թZhN                     @   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 ejddgddd Zejd	d
gddd ZG dd dZejde je jgdd Zdd Zdd Zejdejejgejdeg deg dgdd Zejdeg deg dfeg ddd eg dfeg ddd eg dfeg ddd eg dfeg ddd eg dfgejdejejgd!d" Zd#d$ Zd%d& Zejd'eejegd(d) Z ejdg d*ejd+ej!d,gd-d. Z"dS )/    N)SparseDtype)SparseArrayintegerblock)paramsc                 C      | j S )z!kind kwarg to pass to SparseArrayparamrequest r   b/var/www/html/lang_env/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_arithmetics.pykind   s   r   TFc                 C   r   )z}
    Fixture returning True or False, determining whether to operate
    op(sparse, dense) instead of op(sparse, sparse)
    r   r
   r   r   r   mix   s   r   c                   @   s  e Zd Zdd ZdefddZdd Zdd	 Zd
d Ze	j
dg de	j
dg ddd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Ze	j
dd$d%ejgd&d' Ze	j
dd$d%ejgd(d) Zd*d+ Zd,d- Zd.d/ Zd0S )1TestSparseArrayArithmeticsc                 C   s   t || d S N)tmassert_numpy_array_equal)selfabr   r   r   _assert   s   z"TestSparseArrayArithmetics._assertr   c           	      C   s   t |tjr|t||j}nt |tjr ||t|j}nttjddd |r4||| }n||| }W d    n1 sEw   Y  | 	|| d S )Nignore)invaliddivide)

isinstancenpndarraypdZSeriesvaluesNotImplementedErrorerrstateto_denser   )	r   r   r   a_denseb_denser   opexpectedresultr   r   r   _check_numeric_ops    s   z-TestSparseArrayArithmetics._check_numeric_opsc                 C   sD   t |tsJ t |jtsJ |jjtjksJ t |jts J d S r   )	r   r   dtyper   subtyper   bool_
fill_valuebool)r   resr   r   r   _check_bool_result2   s   z-TestSparseArrayArithmetics._check_bool_resultc                 C   s  t jdd | ||k | ||k ||k | ||k | ||k ||k | ||k | ||k ||k | ||k | ||k ||k | ||k | ||k ||k | ||k  | ||k  ||k  | ||k | ||k ||k | ||k | ||k ||k | ||k | ||k ||k | ||k | ||k ||k | ||k | ||k ||k | ||k  | ||k  ||k  W d    d S 1 sw   Y  d S )Nr   )r   )r   r!   r/   r   r"   r   r   r   r#   r$   r   r   r   _check_comparison_ops8   s4   "z0TestSparseArrayArithmetics._check_comparison_opsc                 C   s   |  ||@  | ||@  ||@  |  ||B  | ||B  ||B  |  ||@  | ||@  ||@  |  ||B  | ||B  ||B  d S r   )r/   r   r"   r0   r   r   r   _check_logical_opsc   s   z-TestSparseArrayArithmetics._check_logical_opsscalar)r         r,   )Nr      c           
      C   sN   |}t t jdddt jddddt jg
}t|||d}	| |	||||| d S )Nr4   r6   r   r   r,   r   arraynanr   r(   )
r   r   r   all_arithmetic_functionsr,   r3   r   r%   r   r   r   r   r   test_float_scalarq   s   $z,TestSparseArrayArithmetics.test_float_scalarc                 C   s   t t jdddt jddddt jg
}t||d}| |d|d | |d|d | |d|d t||dd}| |d|d | |d|d | |d|d t||dd}| |d|d | |d|d | |d|d d S )Nr4   r6   r   r   r5   r7   r   r9   r:   r   r1   )r   r   r   r   r   r   r   test_float_scalar_comparison{   s   $z7TestSparseArrayArithmetics.test_float_scalar_comparisonc           	      C   sT   |}t g d}t g d}t||dd}t||dd}| |||||| d S )N
              ?       @g      @rA   rA   rB   rC   rB   rA   
rA   rC         @g      @rA   rA   rB   rE   rC   rA   r   r7   )r   r9   r   r(   	r   r   r   r;   r%   r   rvaluesr   r   r   r   r   "test_float_same_index_without_nans   s   z=TestSparseArrayArithmetics.test_float_same_index_without_nansc           
      C   s|   |}t t jdddt jddddt jg
}t t jdddt jddddt jg
}t||d}t||d}	| ||	|||| d S )Nr4   r6   r   r5      r=   r8   )
r   r   r   r;   r   r%   r   rG   r   r   r   r   r   test_float_same_index_with_nans   s   $$z:TestSparseArrayArithmetics.test_float_same_index_with_nansc                 C   s   t t jdddt jddddt jg
}t t jdddt jddddt jg
}t||d}t||d}| |||| t g d}t g d}t||dd	}t||dd	}| |||| d S )
Nr4   r6   r   r5   rI   r=   r@   rD   r7   r>   r   r   r   rG   r   r   r   r   r    test_float_same_index_comparison   s   $$z;TestSparseArrayArithmetics.test_float_same_index_comparisonc           	      C   s&  |}t t jdddt jddddt jg
}t dt jddt jddddt jg
}t||d}t||d}| |||||| | ||d ||d || t||dd}t||d}| |||||| t||dd}t||dd}| |||||| t||dd}t||dd}| |||||| d S Nr4   r6   r   r5      r=   r7   r8   rF   r   r   r   test_float_array       $$z+TestSparseArrayArithmetics.test_float_arrayc                 C   s&  |}t t jdddt jddddt jg
}t dt jddt jddddt jg
}t|dd}t|dd}| |||||| | ||d ||d || t|ddd	}t|dd}| |||||| t|ddd	}t|ddd	}| |||||| t|ddd	}t|ddd	}| |||||| d S )
Nr4   r6   r   r5   rN   r   r=   r   r7   r8   )r   r   r;   r%   r   rG   r   r   r   r   r   test_float_array_different_kind   rP   z:TestSparseArrayArithmetics.test_float_array_different_kindc                 C   s  t t jdddt jddddt jg
}t dt jddt jddddt jg
}t||d}t||d}| |||| | ||d ||d  t||dd}t||d}| |||| t||dd}t||dd}| |||| t||dd}t||dd}| |||| d S rM   r>   rK   r   r   r   test_float_array_comparison   s   $$z6TestSparseArrayArithmetics.test_float_array_comparisonc           
      C   s  |}t j}t jg d|d}t jg d|d}t|||d}|jt|ks'J t|||d}	|	jt|ks7J | ||	|||| | ||	d ||d || t|d||d}|jt|ks`J t|||d}	|	jt|kspJ | ||	|||| t|d||d}|jt|ksJ t|d||d}	|	jt|ksJ | ||	|||| t|d||d}|jt|ddksJ t|d	||d}	|	jt|d	dksJ | ||	|||| d S )
N
r   r4   r6   r   r   r   r4   r6   r4   r   r)   
r6   r   r6   r5   r   r   r4   rN   r6   r   r)   r   r   )r,   r)   r   r4   r,   r6   )r   int64r9   r   r)   r   r(   )
r   r   r   r;   r%   r)   r   rG   r   r   r   r   r   test_int_array   s2   z)TestSparseArrayArithmetics.test_int_arrayc                 C   s   d}t jg d|d}t jg d|d}t|||d}t|||d}| |||| | ||d ||d  t|||dd}t|||d}| |||| t|||dd}t|||dd}| |||| t|||dd}t|||d	d}| |||| d S )
NrX   rS   rT   rU   rV   r   )r)   r   r,   r4   r6   )r   r9   r   r1   )r   r   r)   r   rG   r   r   r   r   r   test_int_array_comparison  s    z4TestSparseArrayArithmetics.test_int_array_comparisonTFc                 C   s`   t jg dt jd}t jg dt jd}t||t j|d}t||t j|d}| |||| d S )N)TFTTrT   r   r)   r,   r   r9   r+   r   r2   r   r   r,   r   rG   r   r   r   r   r   test_bool_same_index3  
   z/TestSparseArrayArithmetics.test_bool_same_indexc                 C   s`   t jg dt jd}t jg dt jd}t||t j|d}t||t j|d}| |||| d S )N)TFTFTTrT   )TFFTFTr[   r\   r]   r   r   r   test_bool_array_logical>  r_   z2TestSparseArrayArithmetics.test_bool_array_logicalc                 C   sd  |}d}t t jdddt jddddt jg
}t jg d|d}t||d}	t||d}
|
jt|ks4J | |	|
|||| | |	|
d ||d || t||dd}	t||d}
|
jt|ksbJ | |	|
|||| t||dd}	t||dd}
|
jt|ksJ | |	|
|||| t||dd}	t||dd}
|
jt|dd	ksJ | |	|
|||| d S 
NrX   r4   r6   r   rU   rT   r=   r7   rW   )r   r9   r:   r   r)   r   r(   )r   r   r   r;   r   r%   rdtyper   rG   r   r   r   r   r   test_mixed_array_float_intI  s*   $z5TestSparseArrayArithmetics.test_mixed_array_float_intc                 C   sL  d}t t jdddt jddddt jg
}t jg d|d}t||d}t||d}|jt|ks2J | |||| | ||d ||d  t||dd}t||d}|jt|ks\J | |||| t||dd}t||dd}|jt|ks{J | |||| t||dd}t||dd}|jt|dd	ksJ | |||| d S ra   )r   r9   r:   r   r)   r   r1   )r   r   rb   r   rG   r   r   r   r   r   test_mixed_array_comparisone  s(   $z6TestSparseArrayArithmetics.test_mixed_array_comparisonc                 C   s`   t g d}t g d}||A }tjjjdtjg ddd}t g d|d}t	|| d S )	N)TTFF)TFTFrI   r   r4   r6   Zint32rT   )FTT)Zsparse_index)
r   r   coreZarrayssparseZIntIndexr   r9   r   assert_sp_array_equal)r   str'   Zsp_indexr&   r   r   r   test_xor  s    z#TestSparseArrayArithmetics.test_xorN)__name__
__module____qualname__r   r-   r(   r/   r1   r2   pytestmarkparametrizer<   r?   rH   rJ   rL   rO   rQ   rR   rY   rZ   r   r:   r^   r`   rc   rd   rk   r   r   r   r   r      s2    +$



r   r%   c                 C   s@   t ddgdd}| |ddg}| |t ddg}t|| d S )Nr   r4   rW   )r   r   rh   )r%   arrr'   r&   r   r   r   test_with_list  s   rs   c                  C   s@   t ddgdd} tddgddgg}| |}|tu sJ d S )Nr   r4   rW   r6   r5   rI   )r   r   Z	DataFrame__add__NotImplemented)rr   dfr'   r   r   r   test_with_dataframe  s   
rw   c                  C   s6   t ddgdd} | td }| d }t|| d S )Nr   r4   rW   r6   r   r   r9   r   rh   )rr   r'   r&   r   r   r   test_with_zerodim_ndarray  s   ry   ufuncrr   )r   r   r4   )NNr{   r4   c                 C   s8   | |}| |j }t| t||d}t|| d S )NrW   )r,   r   r   asarrayr   rh   )rz   rr   r'   r,   r&   r   r   r   test_ufuncs  s   
r}   za, b)r   r   r   re   r4   rW   c                 C   sD   | ||}| t |t |}t|tsJ tt || d S r   )r   r|   r   r   r   r   )rz   r   r   r'   r&   r   r   r   test_binary_ufuncs  s   
r~   c                  C   s@   t g d} tg d}|| 7 }tg d}t|| d S )Nr   r6   r   r   r   r4   r6   r5   r   r5   r6   r5   )r   r   r9   r   r   Zsparrayr   r&   r   r   r   test_ndarray_inplace  s
   r   c                  C   sB   t g d} tg d}| |7 } t g ddd}t| | d S )Nr   r   r   r   rW   rx   r   r   r   r   test_sparray_inplace  s
   r   consc                 C   sT   t ddg}| g d}tjtdd ||@  W d    d S 1 s#w   Y  d S )NT)TTTzoperands have mismatched length)match)r   ro   Zraises
ValueError)r   leftrightr   r   r   test_mismatched_length_cmp_op  s
   
"r   )addsubmultruedivfloordivpowr,   r5   c              	   C   s  t t| } tjdd}tjdd}||d d d< ||d d d< t||d}t||d}tjdd | ||}t| | | |j	d}t
|tsRJ t| |  | || }t
|tsjJ t|| | | |}	t
|	ts~J t||	 | |d}
t
|
tsJ z| | d}| |j	d}W n	 ty   Y nw t|
j	| t|
 | W d    d S W d    d S 1 sw   Y  d S )Nr6      r5   rW   r   )allrI   )getattroperatorr   randomZdefault_rngZstandard_normalr   r!   r"   r,   r   r   Zassert_almost_equalrh   r   )r%   r,   Zdata1Zdata2firstsecondr.   expZres2Zres3Zres4Zexp_fvr   r   r   test_binary_operators  sB   


"r   )#r   numpyr   ro   Zpandasr   r   Zpandas._testingZ_testingr   Zpandas.core.arrays.sparser   Zfixturer   r   r   rp   rq   eqr   rs   rw   ry   absr   r}   r9   Zgreaterr~   r   r   listr   r:   r   r   r   r   r   <module>   sR    

  r
	

