U
    sVcOJ                     @   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gZd dgddgdgdgdgdggZd gdgd dgddgd dgddggZdgdgd dgddgdgdggZd dgddgddgdd	gg g gZd gdgg g g g gZg g g g g g gZeeeeeegZddddddg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                  	            
            
plain_casedelete_blockssplit_blocks
skip_blockno_intersect	one_emptyc                   @   s6  e Zd Zejddgdgdgdgdgdggddgddgddgddgdddgd	ddggd
gdgdgdgd
gd	ggddgddgdgdgdgdggddgddgdgd	gdgdggddgddgddgddgdgdggdgdgdddgdddgdgdggddgddgddgddgddddgddddgggdd Zdd ZdS )TestSparseIndexUnion"xloc, xlen, yloc, ylen, eloc, elenr   r
   r   r   r   r   r   r   r   r	      r      r   r   c                 C   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)selfxlocxlenylocylenelocelenxindexyindexZbresultZixindexZiyindexZiresult r5   M/tmp/pip-unpacked-wheel-xj8nt62q/pandas/tests/arrays/sparse/test_libsparse.pytest_index_make_union[   s    ,

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&   r'   r(   r    equalsr"   pytestraises
ValueError)r,   abresexpmsgr5   r5   r6   test_int_index_make_union   s2    


  
z.TestSparseIndexUnion.test_int_index_make_unionN)__name__
__module____qualname__r<   markparametrizer7   rD   r5   r5   r5   r6   r   Z   s   *"".
,r   c                   @   s   e Zd Zejejjdee	ddd Z
dd Zejdedejd	d
gejdedejdd
dgejdedejg ejdedejg ejdgdd ZdS )TestSparseIndexIntersectr   idsc              	   C   s   t t||}t t||}t t||}	t td ||}
||}||	sLt| | }||	 sptd}tjt|d ||
 W 5 Q R X tjt|d | |
  W 5 Q R X d S )Nr   r8   r9   )	r   r   	intersectr;   r"   r*   r<   r=   	Exception)r,   r-   r.   r/   r0   r1   r2   r3   r4   expectedZlonger_indexresultrC   r5   r5   r6   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&   r'   r(   rM   r;   r"   to_block_index)r,   r3   r4   r5   r5   r6   test_intersect_empty   s    z-TestSparseIndexIntersect.test_intersect_emptycaser
   r   r   r   r   r   c                 C   s4   | ||st| }| ||s0td S N)rM   r;   r"   rR   )r,   rT   r5   r5   r6   test_intersect_identical   s    
z1TestSparseIndexIntersect.test_intersect_identicalN)rE   rF   rG   tdZskip_if_windowsr<   rH   rI   CASESIDSrQ   rS   r   r&   r'   r(   rV   r5   r5   r5   r6   rJ      s   	rJ   c                   @   s   e Zd Zdd Zdd Zejdddgdd	 Zejdddgd
d Z	ejdddgddgddgddgddgddgddgddgddgddgg
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&   r'   r(   r!   r   r"   npointsr#   r$   r+   r,   idxr5   r5   r6   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   blockr]   r   r   r   r&   r'   r(   r!   r   r"   r`   r#   r$   r%   r)   ra   r5   r5   r6   test_block_internal   s(    " z)TestSparseIndexCommon.test_block_internalr^   r\   re   c                 C   s  t dtjddgtjd|d}|ddks0t|ddksBt|ddksTt|ddksft|ddksxt|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st|ddkst|ddks"t|ddks6t|ddksJt|ddks^t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std S 
Nr   r   r	   r   r]   r   r   r
   )r   r&   r'   r(   lookupr"   range)r,   r^   rb   ir5   r5   r6   test_lookup  s0    " z!TestSparseIndexCommon.test_lookupc                 C   sD  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|| 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|| d S rh   )r   r&   r'   r(   Zlookup_arrayr#   r$   )r,   r^   rb   rA   rB   r5   r5   r6   test_lookup_array.  s2    " z'TestSparseIndexCommon.test_lookup_arrayzidx, expectedr   ri   r
   r   r   r   r   r      r   r	   r      c                 C   sD   t dddgddg}|||ks&t| }|||ks@td S )Nr   r
   r   r	   r   )r   rj   r"   r*   )r,   rb   rO   ZbindexZiindexr5   r5   r6   test_lookup_basicsQ  s    z(TestSparseIndexCommon.test_lookup_basicsN)rE   rF   rG   rc   rg   r<   rH   rI   rm   rn   rq   r5   r5   r5   r6   rZ      s*   

"rZ   c                   @   sR   e Zd Zdd Zejdddddg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 rd   rf   ra   r5   r5   r6   rg   i  s(    " z"TestBlockIndex.test_block_internalrl   r
   r   d   e   c                 C   sb   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 d S )Nr   r   r   re   r]   )
r   r&   aranger(   r#   r$   r%   r)   Zoneslen)r,   rl   rb   rB   r5   r5   r6   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,   indexr5   r5   r6   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 endr9   r   r
   zBlock 0 overlapsr   r	   )r   r<   r=   r>   )r,   locslengthsrC   r5   r5   r6   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	   ro   r   r   r   r   r   r   )r   r*   r#   r$   r+   r&   r'   r(   )r,   r{   r|   Zexp_indsre   Zdenser5   r5   r6   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   rR   r"   rx   r5   r5   r6   test_to_block_index  s    z"TestBlockIndex.test_to_block_indexN)rE   rF   rG   rg   r<   rH   rI   rw   rz   r}   r~   r   r5   r5   r5   r6   rr   h  s   

rr   c                   @   sF   e Zd Zdd Zdd Zdd Zejjde	e
d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 indicesr9   r   r   r	   )lengthr+   zNo index can be less than zeror
   z(All indices must be less than the lengthr   z#Indices must be strictly increasing)r<   r=   r>   r   )r,   rC   r5   r5   r6   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[   r_   ra   r5   r5   r6   rc     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"   rx   r5   r5   r6   rz     s    zTestIntIndex.test_equalsr   rK   c                 C   s^   t t||}t t||}|  }	|  }
t|	t s>t|	|sLt|
|sZtd S rU   )r   r   r*   rR   r!   r"   r;   )r,   r-   r.   r/   r0   r1   r2   r3   r4   ZxbindexZybindexr5   r5   r6   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   r*   r"   rx   r5   r5   r6   r~     s    zTestIntIndex.test_to_int_indexN)rE   rF   rG   r}   rc   rz   r<   rH   rI   rX   rY   r   r~   r5   r5   r5   r6   r     s   &
r   c                   @   s>   e Zd Zejddddddgejjdeedd	d
 ZdS )TestSparseOperatorsopnameaddsubmultruedivfloordivr   rK   c                 C   sH  t td| d}t t|}	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 )NZsparse_Z_float64g      $@r   g      Y@r   r   )getattrspliboperatorr   r   r*   r&   ru   r`   r;   r"   r#   r$   r   r+   ZreindexZfillnavalues)r,   r   r-   r.   r/   r0   r1   r2   Z	sparse_opZ	python_opr3   r4   ZxdindexZydindexxyZxfillZyfillZresult_block_valsZrb_indexZbfillZresult_int_valsZri_indexZifillZxseriesZyseriesZseries_resultr5   r5   r6   test_op  sJ    
     
     

zTestSparseOperators.test_opN)	rE   rF   rG   r<   rH   rI   rX   rY   r   r5   r5   r5   r6   r      s   r   )%r   Znumpyr&   r<   Zpandas._libs.sparseZ_libssparser   Zpandas.util._test_decoratorsutilZ_test_decoratorsrW   Zpandasr   Zpandas._testingZ_testingr#   Zpandas.core.arrays.sparser   r   r   r   r   r   r   r   r   r   Z
both_emptyrX   rY   r   rJ   rZ   rr   r   r   r5   r5   r5   r6   <module>   s   			


	
[. IO