o
    1فiN                     @   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   l/home/test-dt/pcm-url-check/venv/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pd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"   r0   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   )r0   r   r#   r1   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 )Nr5   r7   r   r   r-   r   arraynanr   r)   )
r   r   r   all_arithmetic_functionsr-   r4   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 )Nr5   r7   r   r   r6   r8   r   r:   r;   r   r2   )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      @rB   rB   rC   rD   rC   rB   
rB   rD         @g      @rB   rB   rC   rF   rD   rB   r   r8   )r   r:   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 )Nr5   r7   r   r6      r>   r9   )
r   r   r   r<   r   r&   r    rH   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 )
Nr5   r7   r   r6   rJ   r>   rA   rE   r8   r?   r   r   r    rH   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 Nr5   r7   r   r6      r>   r8   r9   rG   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 )
Nr5   r7   r   r6   rO   r   r>   r   r8   r9   )r   r   r<   r&   r    rH   r   r   r   r   r   test_float_array_different_kind   rQ   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 rN   r?   rL   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   r5   r7   r   r   r   r5   r7   r5   r   r*   
r7   r   r7   r6   r   r   r5   rO   r7   r   r*   r   r   )r-   r*   r   r5   r-   r7   )r   int64r:   r   r*   r   r)   )
r   r   r   r<   r&   r*   r    rH   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 )
NrY   rT   rU   rV   rW   r   )r*   r   r-   r5   r7   )r   r:   r   r2   )r   r   r*   r    rH   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)TFTTrU   r   r*   r-   r   r:   r,   r   r3   r   r   r-   r    rH   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)TFTFTTrU   )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 
NrY   r5   r7   r   rV   rU   r>   r8   rX   )r   r:   r;   r   r*   r   r)   )r   r   r   r<   r   r&   rdtyper    rH   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 rb   )r   r:   r;   r   r*   r   r2   )r   r   rc   r    rH   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)TFTFrJ   r   r5   r7   int32rU   )FTT)sparse_index)
r   r   corearrayssparseIntIndexr   r:   r   assert_sp_array_equal)r   str(   sp_indexr'   r   r   r   test_xor  s    z#TestSparseArrayArithmetics.test_xorN)__name__
__module____qualname__r   r.   r)   r0   r2   r3   pytestmarkparametrizer=   r@   rI   rK   rM   rP   rR   rS   rZ   r[   r   r;   r_   ra   rd   re   rq   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   r5   rX   )r   r   rm   )r&   arrr(   r'   r   r   r   test_with_list  s   ry   c                  C   s@   t ddgdd} tddgddgg}| |}|tu sJ d S )Nr   r5   rX   r7   r6   rJ   )r   r   	DataFrame__add__NotImplemented)rx   dfr(   r   r   r   test_with_dataframe  s   
r~   c                  C   s6   t ddgdd} | td }| d }t|| d S )Nr   r5   rX   r7   r   r   r:   r   rm   )rx   r(   r'   r   r   r   test_with_zerodim_ndarray  s   r   ufuncrx   )r   r   r5   )NNr   r5   c                 C   s8   | |}| |j }t| t||d}t|| d S )NrX   )r-   r   r   asarrayr   rm   )r   rx   r(   r-   r'   r   r   r   test_ufuncs  s   
r   za, b)r   r   r   rf   r5   rX   c                 C   sD   | ||}| t |t |}t|tsJ tt || d S r   )r   r   r   r   r   r   )r   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   r7   r   r   r   r5   r7   r6   r   r6   r7   r6   )r   r   r:   r   r   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   rX   r   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   ru   raises
ValueError)r   leftrightr   r   r   test_mismatched_length_cmp_op  s
   
"r   )addsubmultruedivfloordivpowr-   r6   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 )Nr7      r6   rX   r   )allrJ   )getattroperatorr   randomdefault_rngstandard_normalr   r"   r#   r-   r   r   assert_almost_equalrm   r   )r&   r-   data1data2firstsecondr/   expres2res3res4exp_fvr   r   r   test_binary_operators  sB   


"r   )#r   numpyr   ru   pandasr   r   pandas._testing_testingr   pandas.core.arrays.sparser   fixturer   r   r   rv   rw   eqr   ry   r~   r   absr   r   r:   greaterr   r   r   listr   r;   r   r   r   r   r   <module>   sR    

  r
	

