3
UdQ              	   @   s  d dl Z d dlZd dlZd dljjZd dlj	j
Zd dlmZ d dljZd dlmZmZmZ dZe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ed dgddgdgdgdgdgdZed gdgd dgddgd dgddgdZedgdgd dgddgdgdgdZe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_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   ]/var/www/html/virt/lib64/python3.6/site-packages/pandas/tests/arrays/sparse/test_libsparse.py_check_case_dict6   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_cases5   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||}|j|}t|t s0ttj|jtj	|tj
d tj|jtj	|tj
d |j }	|j }
|	j|
}t|tsttj|j|j j d S )N)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   M   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   r4   r5   r   r   r   test_index_make_unionL   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}|j|}t dtjddddgtj}|j|sjtt dtjg tjd}t dtjddgtjd}|j|}t dtjddgtj}|j|stt dtjg tjd}t dtjg tjd}|j|}t dtjg tj}|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}|j|}t dtjdddddgtj}|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	 |j| W d Q R X d S )
Nr
   r   r	   r   )r&   r   r   z-Indices must reference same underlying length)match)
r   r.   r/   r0   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;   rF   r   r   r   r   r%   K   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   | j |}|j|std S )N)	intersectr=   r*   )rA   rB   expectedresultr   r   r   _check_correct   s    
z?TestSparseIndexIntersect.test_intersect.<locals>._check_correctc          	   S   s,   d}t jt|d | j| W d Q R X d S )Nz-Indices must reference same underlying length)r<   )r>   r?   	ExceptionrK   )rA   ZlongerrE   r   r   r   _check_length_exc   s    zBTestSparseIndexIntersect.test_intersect.<locals>._check_length_excc       
         sx   t t| |}t t||}t t||}t td ||}	 |||  |j |j |j  ||	 |j |	j  d S )Nr   )r   r'   r2   )
r   r   r   r   r4   r5   r6   r7   rL   Zlonger_index)rN   rP   r   r   r      s    
z<TestSparseIndexIntersect.test_intersect.<locals>._check_case)r$   )r:   r   r   )rN   rP   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}|j|j|sDt|j|j|sXt|j }|j }|j|j|s|t|j|j|std S )Nr   )r&   r   r	   )r   r.   r/   r0   rK   r=   r*   to_block_index)r:   r6   r7   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}x<|D ]4}|j|j|s|t|j }|j|j|sdtqdW d S )Nr
   r   r   )r&   r   r   )r   r.   r/   r0   rK   r=   r*   rR   )r:   Zcasesr   r   r   r   test_intersect_identical  s    
z1TestSparseIndexIntersect.test_intersect_identicalN)rG   rH   rI   tdZskip_if_windowsrQ   rS   rT   r   r   r   r   rJ      s   rJ   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	|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	|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	|j
tjddddgtjd d S )	Nr   r   r	   )r&   integer)kindr   r   )r   r.   r/   r0   r)   r   r*   npointsr+   r,   r3   )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	|j
tjdgtjd tj	|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	|j
tjg tjd tj	|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	|j
tjdgtjd tj	|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	|j
tjddgtjd tj	|jtjddgtjd d S )	Nr   r   r	   )r&   block)rX   r   r   )r   r.   r/   r0   r)   r   r*   rY   r+   r,   r-   r1   )r:   rZ   r   r   r   test_block_internal#  s(    " z)TestSparseIndexCommon.test_block_internalc             C   s  xdD ]}t dtjddgtjd|d}|jddks>t|jd	dksPt|jddksbt|jdd	kstt|jddkst|jddkstt dtjg tjd|d}x$tdd
D ]}|j|dkstqW t dtjd	dddgtjd|d}|jddkst|jd	d	ks"t|jddks6t|jddksJt|jddks^t|jddksrtt dtjd	ddgtjd|d}|jddkst|jd	d	kst|jddkst|jddkst|jddkst|jddkstqW d S )NrW   r\   r   r   r	   )r&   )rX   r   r   r
   )rW   r\   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   )r   r.   r/   r0   lookupr*   range)r:   rX   rZ   ir   r   r   test_lookup<  s2    " z!TestSparseIndexCommon.test_lookupc             C   sJ  xBdD ]8}t dtjddgtjd|d}|jtjdd	dgtjd}tjddd	gtjd}tj|| |jtjddddgtjd}tjdd	ddgtjd}tj|| t dtjg tjd|d}|j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}|jtjdd	dgtjd}tjdd	dgtjd}tj|| |jtjddddgtjd}tjddddgtjd}tj|| t dtjd	ddgtjd|d}|jtjdddd	gtjd}tjdddd	gtjd}tj|| |jtjdddd
gtjd}tjddddgtjd}tj|| qW d S )NrW   r\   r   r   r	   )r&   )rX   r   r   r
   )rW   r\   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   r^   )r   r.   r/   r0   Zlookup_arrayr+   r,   )r:   rX   rZ   rC   rD   r   r   r   test_lookup_array[  s2    " z'TestSparseIndexCommon.test_lookup_arrayc             C   s8   dd }t dddgddg}|j }|| || d S )Nc             S   s   | j ddkst| j ddks$t| j ddks6t| j ddksHt| j ddksZt| j ddkslt| j d	dks~t| j d
dkst| j ddkst| j ddkstd S )Nr   r   r
   r   r   r8   r   r      r   r	   r      r^   r^   r^   r^   r^   r^   )r_   r*   )indexr   r   r   _check~  s    z8TestSparseIndexCommon.test_lookup_basics.<locals>._checkr   r
   r   r	   r   )r   r2   )r:   rg   ZbindexZiindexr   r   r   test_lookup_basics}  s
    z(TestSparseIndexCommon.test_lookup_basicsN)rG   rH   rI   r[   r]   rb   rc   rh   r   r   r   r   rV     s
   "rV   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	|j
tjdgtjd tj	|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	|j
tjg tjd tj	|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	|j
tjdgtjd tj	|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	|j
tjddgtjd tj	|jtjddgtjd d S )	Nr   r   r	   )r&   r\   )rX   r   r   )r   r.   r/   r0   r)   r   r*   rY   r+   r,   r-   r1   )r:   rZ   r   r   r   r]     s(    " z"TestBlockIndex.test_block_internalc          	   C   sp   xjd
D ]b}t |tjd|dtjddd	}tjd|dtjd}tj|j| tj|jtjt	|tjd qW d S )Nr
   r   d   e   r   r   )r&   r\   )rX   )r
   r   rj   rk   )
r   r.   aranger0   r+   r,   r-   r1   Zoneslen)r:   ra   rZ   rD   r   r   r   test_make_block_boundary  s
    
z'TestBlockIndex.test_make_block_boundaryc             C   sF   t dddgddg}|j|s"t|jt dddgddg sBtd S )Nr   r   r   r   r
   r   )r   r=   r*   )r:   rf   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 d Q R X d}tjt|d t dddgdd	g W d Q R X d S )
Nr   r   zBlock 0 extends beyond end)r<   r   r
   zBlock 0 overlapsr   r	   )r   r>   r?   r@   )r:   locslengthsrf   rE   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||}|j }tj|jtj|tjd d S )Nr   r   r   r   r   r   r	   rd   r   r9   r   r   r   )r&   )r   r2   r+   r,   r3   r.   r/   r0   )r:   rp   rq   Zexp_indsr\   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}|j |ks$td S )Nr   r   r
   r   )r   rR   r*   )r:   rf   r   r   r   test_to_block_index  s    z"TestBlockIndex.test_to_block_indexN)	rG   rH   rI   r]   rn   ro   rr   rs   rt   r   r   r   r   ri     s   
ri   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestIntIndexc          6   C   sL  d}t jt|d tddddgd W d Q R X d}t jt|d tddddgd W d Q R X d}t jt|d tddddgd W d Q R X d	}t jt|d tddddgd W d Q R X t jt|d tdddd
gd W d Q R X d}t jt|d tddddgd W d Q R X t jt|d tddddgd W d Q R X d S )NzToo many indices)r<   r   r   r	   )lengthr3   zNo index can be less than zeror
   z(All indices must be less than the lengthr   z#Indices must be strictly increasingrw   )r>   r?   r@   r   )r:   rE   r   r   r   rr     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	|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	|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	|j
tjddddgtjd d S )	Nr   r   r	   )r&   rW   )rX   r   r   )r   r.   r/   r0   r)   r   r*   rY   r+   r,   r3   )r:   rZ   r   r   r   r[     s    "zTestIntIndex.test_int_internalc             C   sD   t ddddddg}|j|s"t|jt dddddg s@td S )Nr   r   r   r   r	   r   )r   r=   r*   )r:   rf   r   r   r   ro     s    zTestIntIndex.test_equalsc             C   s   dd }t | d S )Nc       
      S   s^   t t| |}t t||}|j j }|j j }	t|t s>t|j|sLt|	j|sZtd S )N)r   r'   r2   rR   r)   r*   r=   )
r   r   r   r   r4   r5   r6   r7   ZxbindexZybindexr   r   r   r     s    z5TestIntIndex.test_to_block_index.<locals>._check_case)r$   )r:   r   r   r   r   rt     s    z TestIntIndex.test_to_block_indexc             C   s(   t ddddddg}|j |ks$td S )Nr   r   r	   r   r
   r   )r   r2   r*   )r:   rf   r   r   r   rs   '  s    zTestIntIndex.test_to_int_indexN)rG   rH   rI   rr   r[   ro   rt   rs   r   r   r   r   ru     s
   &ru   c               @   s4   e Zd Zdd Zej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||}|j }|j }	tj|jd d }
tj|jd d }d}d}|
|||||\}}}|
||||	|\}}}|j j|sttj	|| ||kstt
|
|j}|jtjtj|}t
||	j}|jtjtj|} ||}|j|j}tj	||j tj	||j d S )Ng      $@r   g      Y@r   r   )r   r'   r2   r.   rl   rY   r=   r*   r+   r,   r   r3   ZreindexZfillnavalues)r   r   r   r   r4   r5   r6   r7   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   .  s.    
z2TestSparseOperators._op_tests.<locals>._check_case)r$   )r:   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|}| j|| d S )NZsparse_Z_float64)getattrspliboperatorr~   )r:   r   r}   r|   r   r   r   test_opU  s    
zTestSparseOperators.test_opN)rG   rH   rI   r~   r>   markZparametrizer   r   r   r   r   rx   ,  s   (rx   )#r   Znumpyr.   r>   Zpandas._libs.sparseZ_libssparser   Zpandas.util._test_decoratorsutilZ_test_decoratorsrU   Zpandasr   Zpandas._testingZ_testingr+   Zpandas.core.arrays.sparser   r   r   r'   dictr   r    r!   r"   r#   r$   r%   rJ   rV   ri   ru   rx   r   r   r   r   <module>   s^   
"
 5 JO