a
    PfN                     @   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   s   | j S )z!kind kwarg to pass to SparseArrayparamrequest r   d/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/tests/arrays/sparse/test_arithmetics.pykind   s    r   TFc                 C   s   | j S )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ed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._assert)r   c           	      C   s   t |tjr |t||j}n$t |tjr@||t|j}nttjddd2 |rh||| }n||| }W d    n1 s0    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s0J 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    n1 s0    Y  d S )Nr   )r   )r   r    r.   r   r!   r   r   r   r"   r#   r   r   r   _check_comparison_ops8   s2    z0TestSparseArrayArithmetics._check_comparison_opsc                 C   s   |  ||@  | ||@  ||@  |  ||B  | ||B  ||B  |  ||@  | ||@  ||@  |  ||B  | ||B  ||B  d S r   )r.   r   r!   r/   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 )Nr3   r5   r   r   r+   r   arraynanr   r'   )
r   r   r   all_arithmetic_functionsr+   r2   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 )Nr3   r5   r   r   r4   r6   r   r8   r9   r   r0   )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      @r@   r@   rA   rB   rA   r@   
r@   rB         @g      @r@   r@   rA   rD   rB   r@   r   r6   )r   r8   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 )Nr3   r5   r   r4      r<   r7   )
r   r   r   r:   r
   r$   r   rF   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 )
Nr3   r5   r   r4   rH   r<   r?   rC   r6   r=   r   r   r   rF   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 Nr3   r5   r   r4      r<   r6   r7   rE   r   r   r   test_float_array   s     $$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 )
Nr3   r5   r   r4   rM   r   r<   r   r6   r7   )r   r   r:   r$   r   rF   r   r   r   r   r   test_float_array_different_kind   s     $$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 rL   r=   rJ   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sNJ t|||d}	|	jt|ksnJ | ||	|||| | ||	d ||d || t|d||d}|jt|ksJ t|||d}	|	jt|ksJ | ||	|||| t|d||d}|jt|ksJ t|d||d}	|	jt|ks<J | ||	|||| t|d||d}|jt|ddksxJ t|d	||d}	|	jt|d	dksJ | ||	|||| d S )
N
r   r3   r5   r   r   r   r3   r5   r3   r   r(   
r5   r   r5   r4   r   r   r3   rM   r5   r   r(   r   r   )r+   r(   r   r3   r+   r5   )r   int64r8   r   r(   r   r'   )
r   r   r   r:   r$   r(   r   rF   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 )
NrV   rQ   rR   rS   rT   r   )r(   r   r+   r3   r5   )r   r8   r   r0   )r   r   r(   r   rF   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)TFTTrR   r   r(   r+   r   r8   r*   r   r1   r   r   r+   r   rF   r   r   r   r   r   test_bool_same_index3  s
    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)TFTFTTrR   )TFFTFTrY   rZ   r[   r   r   r   test_bool_array_logical>  s
    z2TestSparseArrayArithmetics.test_bool_array_logicalc                 C   sh  |}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shJ | |	|
|||| | |	|
d ||d || t||dd}	t||d}
|
jt|ksJ | |	|
|||| t||dd}	t||dd}
|
jt|ksJ | |	|
|||| t||dd}	t||dd}
|
jt|dd	ksPJ | |	|
|||| d S 
NrV   r3   r5   r   rS   rR   r<   r6   rU   )r   r8   r9   r   r(   r   r'   )r   r   r   r:   r
   r$   rdtyper   rF   r   r   r   r   r   test_mixed_array_float_intI  s*    $z5TestSparseArrayArithmetics.test_mixed_array_float_intc                 C   sN  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sdJ | |||| | ||d ||d  t||dd}t||d}|jt|ksJ | |||| t||dd}t||dd}|jt|ksJ | |||| t||dd}t||dd}|jt|dd	ks:J | |||| d S r^   )r   r8   r9   r   r(   r   r0   )r   r   r_   r   rF   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)TFTFrH   r   r3   r5   Zint32rR   )FTT)Zsparse_index)
r   r   coreZarrayssparseZIntIndexr   r8   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.   r0   r1   pytestmarkparametrizer;   r>   rG   rI   rK   rN   rO   rP   rW   rX   r   r9   r\   r]   r`   ra   rh   r   r   r   r   r      s0   +$



r   r$   c                 C   s@   t ddgdd}| |ddg}| |t ddg}t|| d S )Nr   r3   rU   )r   r   re   )r$   arrr&   r%   r   r   r   test_with_list  s    rp   c                  C   s@   t ddgdd} tddgddgg}| |}|tu s<J d S )Nr   r3   rU   r5   r4   rH   )r   r   Z	DataFrame__add__NotImplemented)ro   Zdfr&   r   r   r   test_with_dataframe  s    
rs   c                  C   s6   t ddgdd} | td }| d }t|| d S )Nr   r3   rU   r5   r   r   r8   r   re   )ro   r&   r%   r   r   r   test_with_zerodim_ndarray  s    ru   ufuncro   )r   r   r3   )NNrw   r3   c                 C   s8   | |}| |j }t| t||d}t|| d S )NrU   )r+   r   r   asarrayr   re   )rv   ro   r&   r+   r%   r   r   r   test_ufuncs  s    
ry   za, b)r   r   r   rb   r3   rU   c                 C   sD   | ||}| t |t |}t|ts.J tt || d S r   )r   rx   r   r   r   r   )rv   r   r   r&   r%   r   r   r   test_binary_ufuncs  s    
rz   c                  C   s@   t g d} tg d}|| 7 }tg d}t|| d S )Nr   r5   r   r   r   r3   r5   r4   r   r4   r5   r4   )r   r   r8   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   rU   rt   r~   r   r   r   test_sparray_inplace  s
    r   consc                 C   sR   t ddg}| g d}tjtdd ||@  W d    n1 sD0    Y  d S )NT)TTTzoperands have mismatched length)match)r   rl   Zraises
ValueError)r   leftrightr   r   r   test_mismatched_length_cmp_op  s    r   )addsubmultruedivfloordivpowr+   r4   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sJ t| |  | || }t
|tsJ t|| | | |}	t
|	tsJ t||	 | |d}
t
|
ts$J z| | d}| |j	d}W n tyV   Y n 0 t|
j	| t|
 | W d    n1 s0    Y  d S )Nr5      r4   rU   r   )allrH   )getattroperatorr   randomZdefault_rngZstandard_normalr   r    r!   r+   r   r   Zassert_almost_equalre   r   )r$   r+   Zdata1Zdata2firstsecondr-   expZres2Zres3Zres4Zexp_fvr   r   r   test_binary_operators  s:    


r   )#r   numpyr   rl   Zpandasr   r   Zpandas._testingZ_testingr   Zpandas.core.arrays.sparser   Zfixturer   r   r   rm   rn   eqr   rp   rs   ru   absr   ry   r8   Zgreaterrz   r   r   listr   r9   r   r   r   r   r   <module>   sP   

  r
	

