U
    f/esR                     @   s  d dl Z d dlZd dlZd dlm  mZ d dlm	  m
Z d dlmZ d dlmZ d dlmZmZmZ dZd ddgdddgd	d
dgd	ddgd	d
dgdddgdZd dgddgdgdgdgdgdZd gdgd dgddgd dgddgdZdgdgd dgddgdgdgdZd dgddgddgdd	gg g dZdd ZG dd dZG dd dZG dd dZG dd dZG dd dZ G dd  d Z!dS )!    N)Series)
BlockIndexIntIndexmake_sparse_index                  	            )xlocxlenylocylenintersect_locintersect_len
            c                    s`    fdd}|t  |t |t |t |t  dgdgg g g g   g g g g g g  d S )Nc                    s.    | d | d | d | d | d | d  d S )Nr   r   r   r   r   r    )case_check_caser   M/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/arrays/sparse/test_libsparse.py_check_case_dict?   s    z%check_cases.<locals>._check_case_dictr   r
   )
plain_casedelete_blockssplit_blocks
skip_blockno_intersect)r   r   r   r   r   check_cases>   s    
r%   c                   @   s   e Zd Zdd Zdd ZdS )TestSparseIndexUnionc                 C   s  dd }dg}dg}dg}dg}dg}dg}||||||| ddg}ddg}dd	g}ddg}ddd	g}d
ddg}||||||| dg}dg}dg}dg}dg}d
g}||||||| ddg}ddg}dg}dg}dg}dg}||||||| ddg}ddg}dg}d
g}dg}dg}||||||| ddg}ddg}ddg}ddg}dg}dg}||||||| dg}dg}dddg}dddg}dg}dg}||||||| ddg}ddg}ddg}ddg}ddddg}ddddg}||||||| d S )Nc                 S   s   t t| |}t t||}||}t|t s0tt|jtj	|tj
d t|jtj	|tj
d | }	| }
|	|
}t|tstt|j| j d S )NZdtype)r   TEST_LENGTH
make_union
isinstanceAssertionErrortmassert_numpy_array_equalblocsnparrayint32blengthsto_int_indexr   indices)r   r   r   r   elocelenxindexyindexZbresultZixindexZiyindexZiresultr   r   r   r   V   s    
 
z?TestSparseIndexUnion.test_index_make_union.<locals>._check_caser   r
   r   r   r   r   r   r   r   r	      r      r   r   r   )selfr   r   r   r   r   r5   r6   r   r   r   test_index_make_unionU   sr    



z*TestSparseIndexUnion.test_index_make_unionc              	   C   s  t dtjdddgtjd}t dtjddgtjd}||}t dtddddgtj}||sjtt dtjg tjd}t dtjddgtjd}||}t dtddgtj}||stt dtjg tjd}t dtjg tjd}||}t dtg tj}||s$tt dtjdddddgtjd}t dtjdddddgtjd}||}t dtdddddgtj}||stt dtjddgtjd}t dtjddgtjd}d}tjt	|d	 || W 5 Q R X d S )
Nr
   r   r	   r   r'   r   r   -Indices must reference same underlying lengthmatch)
r   r/   r0   r1   r)   equalsr+   pytestraises
ValueError)r;   abresexpmsgr   r   r   test_int_index_make_union   s2    


  
z.TestSparseIndexUnion.test_int_index_make_unionN)__name__
__module____qualname__r<   rI   r   r   r   r   r&   T   s   pr&   c                   @   s*   e Zd Zejdd Zdd Zdd ZdS )TestSparseIndexIntersectc                    s*   dd  dd  fdd}t | d S )Nc                 S   s   |  |}||std S N)	intersectr@   r+   )rD   rE   expectedresultr   r   r   _check_correct   s    
z?TestSparseIndexIntersect.test_intersect.<locals>._check_correctc              	   S   s,   d}t jt|d | | W 5 Q R X d S )Nr=   r>   )rA   rB   	ExceptionrO   )rD   ZlongerrH   r   r   r   _check_length_exc   s    zBTestSparseIndexIntersect.test_intersect.<locals>._check_length_excc           
         sx   t t| |}t t||}t t||}t td ||}	 |||  | | |  ||	 | |	  d S )Nr   )r   r(   r3   )
r   r   r   r   r5   r6   r7   r8   rP   Zlonger_indexrR   rT   r   r   r      s      
z<TestSparseIndexIntersect.test_intersect.<locals>._check_caser%   r;   r   r   rU   r   test_intersect   s    z'TestSparseIndexIntersect.test_intersectc                 C   s   t dtjg tjd}t dtjddgtjd}|||sDt|||sXt| }| }|||s|t|||std S )Nr   r'   r   r	   r   r/   r0   r1   rO   r@   r+   to_block_index)r;   r7   r8   r   r   r   test_intersect_empty  s    z-TestSparseIndexIntersect.test_intersect_emptyc              	   C   s   t dtjddgtjdt dtjdddgtjdt dtjg tjdt dtjg tjdg}|D ]4}|||szt| }|||sbtqbd S )Nr
   r   r   r'   r   r   rY   )r;   Zcasesr   r   r   r   test_intersect_identical  s    z1TestSparseIndexIntersect.test_intersect_identicalN)rJ   rK   rL   tdZskip_if_windowsrX   r[   r\   r   r   r   r   rM      s   
rM   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestSparseIndexCommonc                 C   s  t dtjddgtjddd}t|ts,t|jdks:tt	|j
tjddgtjd t dtjg tjddd}t|tst|jdkstt	|j
tjg tjd t dtjddddgtjddd}t|tst|jdkstt	|j
tjddddgtjd d S 	Nr   r   r	   r'   integerkindr   r   r   r/   r0   r1   r*   r   r+   npointsr,   r-   r4   r;   idxr   r   r   test_int_internal  s       z'TestSparseIndexCommon.test_int_internalc                 C   s  t dtjddgtjddd}t|ts,t|jdks:tt	|j
tjdgtjd t	|jtjdgtjd t dtjg tjddd}t|tst|jdkstt	|j
tjg tjd t	|jtjg tjd t dtjddddgtjddd}t|tst|jdkstt	|j
tjdgtjd t	|jtjdgtjd t dtjdddgtjddd}t|tst|jdkstt	|j
tjddgtjd t	|jtjddgtjd d S 	Nr   r   r	   r'   blockra   r   r   r   r/   r0   r1   r*   r   r+   rd   r,   r-   r.   r2   re   r   r   r   test_block_internal.  s(    " z)TestSparseIndexCommon.test_block_internalc                 C   s  dD ]}t dtjddgtjd|d}|ddks:t|ddksLt|d	dks^t|ddkspt|dd	kst|ddkstt dtjg tjd|d}tdd
D ]}||dkstqt dtjdd	ddgtjd|d}|ddkst|ddkst|d	d	ks.t|ddksBt|ddksVt|ddksjtt dtjdddgtjd|d}|ddkst|ddkst|d	dkst|dd	kst|ddkst|ddkstqd S N)r`   ri   r   r   r	   r'   ra   r   r   r
   )r   r/   r0   r1   lookupr+   range)r;   rb   rf   ir   r   r   test_lookupG  s:    
   z!TestSparseIndexCommon.test_lookupc                 C   sD  dD ]8}t dtjddgtjd|d}|tjdddgtjd}tjdddgtjd}t|| |tjddd	dgtjd}tjdddd	gtjd}t|| t dtjg tjd|d}|tjddddgtjd}tjddddgtjd}t dtjdd	ddgtjd|d}|tjdddgtjd}tjdddgtjd}t|| |tjddd	dgtjd}tjddd	dgtjd}t|| t dtjdddgtjd|d}|tjdd	ddgtjd}tjd	dddgtjd}t|| |tjd	ddd
gtjd}tjddd	dgtjd}t|| qd S rl   )r   r/   r0   r1   Zlookup_arrayr,   r-   )r;   rb   rf   rF   rG   r   r   r   test_lookup_arrayh  s:    
   z'TestSparseIndexCommon.test_lookup_arrayc                 C   s8   dd }t dddgddg}| }|| || d S )Nc                 S   s   |  ddkst|  ddks$t|  ddks6t|  ddksHt|  ddksZt|  ddkslt|  d	dks~t|  d
dkst|  ddkst|  ddkstd S )Nr   rm   r
   r   r   r9   r   r      r   r	   r      )rn   r+   )indexr   r   r   _check  s    z8TestSparseIndexCommon.test_lookup_basics.<locals>._checkr   r
   r   r	   r   )r   r3   )r;   rv   ZbindexZiindexr   r   r   test_lookup_basics  s
    z(TestSparseIndexCommon.test_lookup_basicsN)rJ   rK   rL   rg   rk   rq   rr   rw   r   r   r   r   r^     s
   !$r^   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestBlockIndexc                 C   s  t dtjddgtjddd}t|ts,t|jdks:tt	|j
tjdgtjd t	|jtjdgtjd t dtjg tjddd}t|tst|jdkstt	|j
tjg tjd t	|jtjg tjd t dtjddddgtjddd}t|tst|jdkstt	|j
tjdgtjd t	|jtjdgtjd t dtjdddgtjddd}t|tst|jdkstt	|j
tjddgtjd t	|jtjddgtjd d S rh   rj   re   r   r   r   rk     s(    " z"TestBlockIndex.test_block_internalc              	   C   sl   dD ]b}t |tjd|dtjddd}tjd|dtjd}t|j| t|jtjt	|tjd qd S )N)r
   r   d   e   r   r   r'   ri   ra   )
r   r/   aranger1   r,   r-   r.   r2   Zoneslen)r;   rp   rf   rG   r   r   r   test_make_block_boundary  s
    z'TestBlockIndex.test_make_block_boundaryc                 C   sD   t dddgddg}||s"t|t dddgddgr@td S )Nr   r   r   r   r
   r   )r   r@   r+   r;   ru   r   r   r   test_equals  s    zTestBlockIndex.test_equalsc              	   C   s   g }g }t d||}t d||}d}tjt|d t ddgdg W 5 Q R X d}tjt|d t dddgdd	g W 5 Q R X d S )
Nr   r   zBlock 0 extends beyond endr>   r   r
   zBlock 0 overlapsr   r	   )r   rA   rB   rC   )r;   locslengthsru   rH   r   r   r   test_check_integrity  s    z#TestBlockIndex.test_check_integrityc              
   C   sZ   ddg}ddg}ddddddd	d
ddg
}t d||}| }t|jtj|tjd d S )Nr   r   r   r   r   r   r	   rs   r   r:   r   r   r   r'   )r   r3   r,   r-   r4   r/   r0   r1   )r;   r   r   Zexp_indsri   Zdenser   r   r   test_to_int_index  s    z TestBlockIndex.test_to_int_indexc                 C   s(   t dddgddg}| |ks$td S )Nr   r   r
   r   )r   rZ   r+   r~   r   r   r   test_to_block_index  s    z"TestBlockIndex.test_to_block_indexN)	rJ   rK   rL   rk   r}   r   r   r   r   r   r   r   r   rx     s   
rx   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestIntIndexc              	   C   sL  d}t jt|d tddddgd W 5 Q R X d}t jt|d tddd	dgd W 5 Q R X d}t jt|d tddd	dgd W 5 Q R X d
}t jt|d tddddgd W 5 Q R X t jt|d tddddgd W 5 Q R X d}t jt|d tddddgd W 5 Q R X t jt|d tddddgd W 5 Q R X d S )NzToo many indicesr>   r   r   r	   )lengthr4   zNo index can be less than zeror
   z(All indices must be less than the lengthr   z#Indices must be strictly increasing)rA   rB   rC   r   )r;   rH   r   r   r   r     s&    z!TestIntIndex.test_check_integrityc                 C   s  t dtjddgtjddd}t|ts,t|jdks:tt	|j
tjddgtjd t dtjg tjddd}t|tst|jdkstt	|j
tjg tjd t dtjddddgtjddd}t|tst|jdkstt	|j
tjddddgtjd d S r_   rc   re   r   r   r   rg     s       zTestIntIndex.test_int_internalc                 C   sB   t ddddddg}||s"t|t dddddgr>td S )Nr   r   r   r   r	   r   )r   r@   r+   r~   r   r   r   r   %  s    zTestIntIndex.test_equalsc                 C   s   dd }t | d S )Nc           
      S   s^   t t| |}t t||}|  }|  }	t|t s>t||sLt|	|sZtd S rN   )r   r(   r3   rZ   r*   r+   r@   )
r   r   r   r   r5   r6   r7   r8   ZxbindexZybindexr   r   r   r   +  s    z5TestIntIndex.test_to_block_index.<locals>._check_caserV   rW   r   r   r   r   *  s    z TestIntIndex.test_to_block_indexc                 C   s(   t ddddddg}| |ks$td S )Nr   r   r	   r   r
   r   )r   r3   r+   r~   r   r   r   r   8  s    zTestIntIndex.test_to_int_indexN)rJ   rK   rL   r   rg   r   r   r   r   r   r   r   r     s
   &r   c                   @   s4   e Zd Zdd Zejddddddgd	d
 ZdS )TestSparseOperatorsc                    s    fdd}t | d S )Nc                    s,  t t| |}t t||}| }| }	t|jd d }
t|jd d }d}d}|
|||||\}}}|
||||	|\}}}| |stt	|| ||kstt
|
|j}|tt|}t
||	j}|tt|} ||}||j}t	||j t	||j d S )Ng      $@r   g      Y@r   r   )r   r(   r3   r/   r{   rd   r@   r+   r,   r-   r   r4   ZreindexZfillnavalues)r   r   r   r   r5   r6   r7   r8   ZxdindexZydindexxyZxfillZyfillZresult_block_valsZrb_indexZbfillZresult_int_valsZri_indexZifillZxseriesZyseriesZseries_result	python_op	sparse_opr   r   r   ?  sF         
     

z2TestSparseOperators._op_tests.<locals>._check_caserV   )r;   r   r   r   r   r   r   	_op_tests>  s    %zTestSparseOperators._op_testsopnameaddsubmultruedivfloordivc                 C   s,   t td| d}t t|}| || d S )NZsparse_Z_float64)getattrspliboperatorr   )r;   r   r   r   r   r   r   test_opf  s    
zTestSparseOperators.test_opN)rJ   rK   rL   r   rA   markZparametrizer   r   r   r   r   r   =  s   (r   )"r   Znumpyr/   rA   Zpandas._libs.sparseZ_libssparser   Zpandas.util._test_decoratorsutilZ_test_decoratorsr]   Zpandasr   Zpandas._testingZ_testingr,   Zpandas.core.arrays.sparser   r   r   r(   r    r!   r"   r#   r$   r%   r&   rM   r^   rx   r   r   r   r   r   r   <module>   sh   			

 5 JQ