o
    dNJ                     @   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g dg dg dg dg d	g d
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g 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   )r         )      r
   )   	      )r   r	   r
   )r   r   r   )   r	      r
   r   r   r	   
   r            r   )
plain_casedelete_blockssplit_blocks
skip_blockno_intersect	one_emptyc                   @   s&  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g d	g d
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g dg ddgdggddgddgddgddgg dg d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   r   r	   r      r      r   )r   r   r   )r	   r   r   )r   r
   r   r   )r	   r   r	   r   c                 C   s   t t||}t t||}||}	t|	t sJ t|	jtj|tj	d t|	j
tj|tj	d | }
| }|
|}t|tsFJ t|j|	 j d S )NZdtype)r   TEST_LENGTH
make_union
isinstance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 r4   ]/app/.heroku/python/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_libsparse.pytest_index_make_union[   s   ,

z*TestSparseIndexUnion.test_index_make_unionc                 C   s  t dtjg dtjd}t dtjddgtjd}||}t dtg dtj}||s2J 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}||sbJ t dtjg tjd}t dtjg tjd}||}t dtg tj}||sJ t dtjg dtjd}t dtjg dtjd}||}t dtg dtj}||sJ 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 d    d S 1 sw   Y  d S )Nr
   )r   r	   r   r   r   r   )r   r   r	   r   r   r   r   r	   r   r   r   -Indices must reference same underlying lengthmatch)	r   r%   r&   r'   r    equalspytestraises
ValueError)r+   abresexpmsgr4   r4   r5   test_int_index_make_union   s4   



"z.TestSparseIndexUnion.test_int_index_make_unionN)__name__
__module____qualname__r<   markparametrizer6   rD   r4   r4   r4   r5   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g d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   Zidsc                 C   s   t t||}t t||}t t||}	t td ||}
||}||	s&J | | }||	 s8J d}tjt|d ||
 W d    n1 sQw   Y  tjt|d | |
  W d    d S 1 srw   Y  d S )Nr   r8   r9   )r   r   	intersectr;   r)   r<   r=   	Exception)r+   r,   r-   r.   r/   r0   r1   r2   r3   expectedZlonger_indexresultrC   r4   r4   r5   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}|||s"J |||s,J | }| }|||s>J |||sHJ d S )Nr   r   r   r	   )r   r%   r&   r'   rL   r;   to_block_index)r+   r2   r3   r4   r4   r5   test_intersect_empty   s   z-TestSparseIndexIntersect.test_intersect_emptycaser
   r   r   r   )r   r   r   r   c                 C   s4   | ||s
J | }| ||sJ d S N)rL   r;   rQ   )r+   rS   r4   r4   r5   test_intersect_identical   s   
z1TestSparseIndexIntersect.test_intersect_identicalN)rE   rF   rG   tdZskip_if_windowsr<   rH   rI   CASESIDSrP   rR   r   r%   r&   r'   rU   r4   r4   r4   r5   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     t dtjddgtjddd}t|tsJ |jdksJ t|j	tjddgtjd t dtjg tjddd}t|ts@J |jdksGJ t|j	tjg tjd t dtjg dtjddd}t|tsjJ |jdksqJ t|j	tjg dtjd d S 	Nr   r   r	   r   integerkindr   r   r   r   r	   
r   r%   r&   r'   r!   r   npointsr"   r#   r*   r+   idxr4   r4   r5   test_int_internal      "z'TestSparseIndexCommon.test_int_internalc                 C     t dtjddgtjddd}t|tsJ |jdksJ 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sMJ |jdksTJ t|j	tjg tjd t|j
tjg tjd t dtjg dtjddd}t|tsJ |jdksJ t|j	tjdgtjd t|j
tjdgtjd t dtjg d	tjddd}t|tsJ |jdksJ 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   ra   r"   r#   r$   r(   rb   r4   r4   r5   test_block_internal   (   "z)TestSparseIndexCommon.test_block_internalr^   r\   rh   c                 C   s  t dtjddgtjd|d}|ddksJ |ddks!J |ddks*J |ddks3J |ddks<J |ddksEJ t dtjg tjd|d}tdd	D ]}||dksbJ qWt dtjg d
tjd|d}|ddks{J |ddksJ |ddksJ |ddksJ |ddksJ |ddksJ t dtjg dtjd|d}|ddksJ |ddksJ |ddksJ |ddksJ |ddksJ |ddksJ d S )Nr   r   r	   r   r]   r   r   r
   r_   ri   )r   r%   r&   r'   lookuprange)r+   r^   rc   ir4   r4   r5   test_lookup  s0   z!TestSparseIndexCommon.test_lookupc                 C   s  t dtjddgtjd|d}|tjg dtjd}tjg dtjd}t|| |tjg dtjd}tjg d	tjd}t|| t dtjg tjd|d}|tjg d
tjd}tjg dtjd}t|| t dtjg dtjd|d}|tjg dtjd}tjg dtjd}t|| |tjg dtjd}tjg dtjd}t|| t dtjg dtjd|d}|tjg dtjd}tjg dtjd}t|| |tjg dtjd}tjg dtjd}t|| d S )Nr   r   r	   r   r]   )rm   r   r   )rm   rm   r   )r   r   r   r	   )rm   r   rm   r   )rm   r   r   r   )rm   rm   rm   rm   r_   )rm   r   r   r	   ri   )r   r   r	   r   )r   rm   r   r   )r   r   r   r
   )rm   rm   r   rm   )r   r%   r&   r'   Zlookup_arrayr"   r#   )r+   r^   rc   rA   rB   r4   r4   r5   test_lookup_array.  s2   z'TestSparseIndexCommon.test_lookup_arrayzidx, expectedr   rm   r
   r   r   r   r   r      r   r	   r      c                 C   sD   t dddgddg}|||ksJ | }|||ks J d S )Nr   r
   r   r	   r   )r   rn   r)   )r+   rc   rN   ZbindexZiindexr4   r4   r5   test_lookup_basicsQ  s   z(TestSparseIndexCommon.test_lookup_basicsN)rE   rF   rG   rd   rk   r<   rH   rI   rq   rr   ru   r4   r4   r4   r5   rY      s,    

"rY   c                   @   sN   e Zd Zdd Zejdg ddd Zdd Zd	d
 Z	dd Z
dd ZdS )TestBlockIndexc                 C   rf   rg   rj   rb   r4   r4   r5   rk   i  rl   z"TestBlockIndex.test_block_internalrp   )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   rh   r]   )
r   r%   aranger'   r"   r#   r$   r(   Zoneslen)r+   rp   rc   rB   r4   r4   r5   test_make_block_boundary  s   "z'TestBlockIndex.test_make_block_boundaryc                 C   sD   t dddgddg}||sJ |t dddgddgr J d S )Nr   r   r   r   r
   r   )r   r;   r+   indexr4   r4   r5   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    n1 s,w   Y  d}tjt|d t dddgdd	g W d    d S 1 sPw   Y  d S )
Nr   r   zBlock 0 extends beyond endr9   r   r
   zBlock 0 overlapsr   r	   )r   r<   r=   r>   )r+   locslengthsrC   r4   r4   r5   test_check_integrity  s   "z#TestBlockIndex.test_check_integrityc                 C   sJ   ddg}ddg}g d}t d||}| }t|jtj|tjd d S )Nr   r   r   r   )
r   r   r   r	   r   rs   r   r   r   r   r   r   )r   r)   r"   r#   r*   r%   r&   r'   )r+   r   r   Zexp_indsrh   Zdenser4   r4   r5   test_to_int_index  s   z TestBlockIndex.test_to_int_indexc                 C   s(   t dddgddg}| |u sJ d S )Nr   r   r
   r   )r   rQ   r|   r4   r4   r5   test_to_block_index  s   z"TestBlockIndex.test_to_block_indexN)rE   rF   rG   rk   r<   rH   rI   r{   r~   r   r   r   r4   r4   r4   r5   rv   h  s    

rv   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   s  d}t jt|d tdg dd W d    n1 sw   Y  d}t jt|d tdg dd W d    n1 s=w   Y  d}t jt|d tdg dd W d    n1 s^w   Y  d	}t jt|d tdg d
d W d    n1 sw   Y  t jt|d tdg dd W d    n1 sw   Y  d}t jt|d tdg dd W d    n1 sw   Y  t jt|d tdg dd W d    d S 1 sw   Y  d S )NzToo many indicesr9   r   )r   r   r	   )lengthr*   zNo index can be less than zeror
   )r   r	   z(All indices must be less than the length)r   r   r
   )r   r   r   z#Indices must be strictly increasing)r   r	   r   )r   r	   r	   )r<   r=   r>   r   )r+   rC   r4   r4   r5   r     s4   "z!TestIntIndex.test_check_integrityc                 C   rZ   r[   r`   rb   r4   r4   r5   rd     re   zTestIntIndex.test_int_internalc                 C   s8   t dg d}||sJ |t dg drJ d S )Nr   r7   r_   )r   r;   r|   r4   r4   r5   r~     s   zTestIntIndex.test_equalsr   rK   c                 C   s^   t t||}t t||}|  }	|  }
t|	t sJ |	|s&J |
|s-J d S rT   )r   r   r)   rQ   r!   r;   )r+   r,   r-   r.   r/   r0   r1   r2   r3   ZxbindexZybindexr4   r4   r5   r     s   z TestIntIndex.test_to_block_indexc                 C   s"   t dg d}| |u sJ d S )Nr   )r   r	   r   r
   r   )r   r)   r|   r4   r4   r5   r     s   zTestIntIndex.test_to_int_indexN)rE   rF   rG   r   rd   r~   r<   rH   rI   rW   rX   r   r   r4   r4   r4   r5   r     s    %
r   c                   @   s8   e Zd Zejdg d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}|||
||||\}}}|||||||\}}}| 	|s[J t
|| ||ksgJ 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%   ry   ra   r;   r"   r#   r   r*   ZreindexZfillnavalues)r+   r   r,   r-   r.   r/   r0   r1   Z	sparse_opZ	python_opr2   r3   ZxdindexZydindexxyZxfillZyfillZresult_block_valsZrb_indexZbfillZresult_int_valsZri_indexZifillZxseriesZyseriesZseries_resultr4   r4   r5   test_op   s6   



zTestSparseOperators.test_opN)	rE   rF   rG   r<   rH   rI   rW   rX   r   r4   r4   r4   r5   r     s    r   )%r   numpyr%   r<   Zpandas._libs.sparseZ_libssparser   Zpandas.util._test_decoratorsutilZ_test_decoratorsrV   Zpandasr   Zpandas._testingZ_testingr"   Zpandas.core.arrays.sparser   r   r   r   r   r   r   r   r   r   Z
both_emptyrW   rX   r   rJ   rY   rv   r   r   r4   r4   r4   r5   <module>   s    			


	
[. IN