3
Êb_T3  ã               @   sl   d 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	Z
ddl	mZmZmZ ddljZG dd„ dƒZdS )z¢
Collection of tests asserting things that should be true for
any index subclass. Makes use of the `indices` fixture defined
in pandas/tests/indexes/conftest.py.
é    N)ÚiNaT)Úneeds_i8_conversion)ÚCategoricalIndexÚ
MultiIndexÚ
RangeIndexc            	   @   sö   e Zd Zdd„ Zdd„ Zdd„ Zejjddd	gƒejj	d
ƒdd„ ƒƒZ
ejjdd5d6d7d8d9gƒ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d%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zejjd-d.d/d0gƒd1d2„ ƒZd3d4„ ZdS ):Ú
TestCommonc          
   C   s®   t |tƒrd S |jg ƒj|ƒs"t‚xN|j|jgfD ]<}t |jtƒrN||jkrNq2tjt	ƒ� |j|ƒ W d Q R X q2W x6ddgfD ](}tjt
dd�� |j|ƒ W d Q R X q~W d S )NÚwrongz>'Requested level \(wrong\) does not match index name \(None\)')Úmatch)Ú
isinstancer   Z	droplevelÚequalsÚAssertionErrorÚnameÚtupleÚpytestÚraisesÚ
ValueErrorÚKeyError)ÚselfÚindicesÚlevel© r   úB/tmp/pip-build-jkwsxyiy/pandas/pandas/tests/indexes/test_common.pyÚtest_droplevel   s    

zTestCommon.test_droplevelc             C   sp   t |tƒrtjdƒ d}dgg}tjt|d�� |j|d� W d Q R X tjt|d�� |j|d� W d Q R X d S )Nz#multiindex handled in test_multi.pyz"Index.name must be a hashable typeÚ1)r	   )r   )Únames)r
   r   r   Úskipr   Ú	TypeErrorÚrenameÚ	set_names)r   r   ÚmessageZrenamedr   r   r   Ú"test_constructor_non_hashable_name+   s    

z-TestCommon.test_constructor_non_hashable_namec             C   s:   t |tjƒrtjdƒ‚|}t|ƒ|ƒ}tj|j|jƒ d S )NzMultiIndex has no ._data)	r
   Úpdr   r   r   ÚtypeÚtmZassert_equalÚ_data)r   r   ÚaÚbr   r   r   Útest_constructor_unwraps_index<   s
    
z)TestCommon.test_constructor_unwraps_indexÚitmée   Zno_intzignore::FutureWarningc          	   C   s"   t jtƒ� ||  W d Q R X d S )N)r   r   Ú
IndexError)r   r   r(   r   r   r   Útest_getitem_errorC   s    zTestCommon.test_getitem_errorzfname, sname, expected_nameÚAÚBNc       	      C   s*  t |tƒs|j rtjdƒ |jƒ j|ƒ}|jƒ j|ƒ}|j|ƒ}|jƒ j|ƒ}tj	||ƒ |jƒ j|ƒ}|j
|ƒj|ƒ}|j|ƒ}|jƒ j|ƒ}tj	||ƒ |j
|ƒj|ƒ}|jƒ j|ƒ}|j|ƒ}|jƒ j|ƒ}tj	||ƒ |j
|ƒj|ƒ}|j
|ƒj|ƒ}|j|ƒ}|j
|ƒj|ƒ}tj	||ƒ d S )Nz&Not for MultiIndex or repeated indices)r
   r   Ú	is_uniquer   r   Úcopyr   Úunionr#   Úassert_index_equalZdrop)	r   r   ÚfnameZsnameZexpected_nameÚfirstÚsecondr0   Úexpectedr   r   r   Útest_corner_unionJ   s,    




zTestCommon.test_corner_unionc             C   s,   t |tƒrtjdƒ |jƒ }tj||ƒ d S )Nz#Separate expectation for MultiIndex)r
   r   r   r   Zto_flat_indexr#   r1   )r   r   Úresultr   r   r   Útest_to_flat_indexx   s    

zTestCommon.test_to_flat_indexc          
   C   s*   t jtdd�� dddg|_W d Q R X d S )Nz^Length)r	   ZappleÚbananaZcarrot)r   r   r   r   )r   r   r   r   r   Útest_wrong_number_names€   s    z"TestCommon.test_wrong_number_namesc             C   sÚ   d}t |tƒrtjdƒ |j}|j|gƒ}|j|ks8t‚|j|ksFt‚|j|dd�}|d ks`t‚|j|ksnt‚|j|gks~t‚tj	t
dd�� |jddd	� W d Q R X d}|j|dd� |j|ksÆt‚|j|gksÖt‚d S )Nz#This is the new name for this indexzSkip check for MultiIndexT)ZinplacezLevel must be None)r	   r%   r   )r   r,   r-   )r,   r-   )r
   r   r   r   r   r   r   r   r   r   r   )r   r   Únew_nameÚoriginal_nameZnew_indÚresr   r   r   r   Útest_set_name_methods„   s"    

z TestCommon.test_set_name_methodsc          	   C   s8   |}t jtdt|ƒj› d�d�� t|ƒ W d Q R X d S )Nzunhashable type: 'ú')r	   )r   r   r   r"   Ú__name__Úhash)r   r   Úindexr   r   r   Útest_hash_error¡   s    zTestCommon.test_hash_errorc             C   sx   ddl m }m} t|tƒr$tjdƒ x2||fD ]&}||ƒ}||k	sFt‚|j|ƒs.t‚q.W |j ddd�}|jdkstt‚d S )Nr   )r/   ÚdeepcopyzSkip check for MultiIndexTr9   )Údeepr   )	r/   rD   r
   r   r   r   r   r   r   )r   r   r/   rD   ÚfuncZidx_copyZnew_copyr   r   r   Útest_copy_and_deepcopy¨   s    

z!TestCommon.test_copy_and_deepcopyc             C   sº   t |ttfƒrtjdƒ |jƒ }x,d|jd fD ]}|j|d�}tj	||ƒ q.W d}tj
t|d�� |jdd� W d Q R X dtj|jjƒ ƒ› d�}tj
t|d�� |jd	d� W d Q R X d S )
Nz*Skip check for MultiIndex/CategoricalIndexr   )r   z.Too many levels: Index has only 1 level, not 4)r	   é   z6Requested level \(wrong\) does not match index name \(z\)r   )r
   r   r   r   r   Údrop_duplicatesr   Úuniquer#   r1   r   r*   ÚreÚescapeÚ__repr__r   )r   r   r5   r   r7   Úmsgr   r   r   Útest_unique¶   s    
zTestCommon.test_uniquec             C   s„  t |ƒ st|tƒrtjdƒ |dgd  }|dg }|jdksDt‚y|jdksTt‚W n tk
rj   Y nX x$d	D ]}|j	|d�}t
j||ƒ qrW |js¢tjdƒ t|ƒrÄ|jdgd  }t|d< n|jdgd  }tj|d< |d d… }|j|ƒ}|j|ƒ}	|	jdk�st‚|j|jk�s t‚|	j|jk�s2t‚xLtddg|	|gƒD ]6\}}
x*||	gD ]}|j	|d�}t
j||
ƒ �qXW �qFW d S )
Nz)Skip check for empty Index and MultiIndexr   é   TF)Údropnaz%Skip na-check if index cannot hold naé   )FT)Úlenr
   r   r   r   r.   r   ZhasnansÚNotImplementedErrorZ_get_unique_indexr#   r1   Z_can_hold_nar   Zasi8r   ÚvaluesÚnpÚnanZ_shallow_copyZdtypeÚzip)r   r   ÚidxZ
idx_uniquerQ   r7   ÚvalsZvals_uniqueZidx_nanZidx_unique_nanr5   Úir   r   r   Útest_get_unique_indexÍ   s:    







z TestCommon.test_get_unique_indexc          	   C   s*   d}t jt|d�� |jƒ  W d Q R X d S )Nz=cannot sort an Index object in-place, use sort_values instead)r	   )r   r   r   Úsort)r   r   rN   r   r   r   Ú	test_sortù   s    zTestCommon.test_sortc          
   C   s@   t |ƒstjdƒ d}tjt|d�� |d |d< W d Q R X d S )NzSkip check for empty Indexz)Index does not support mutable operations)r	   r   )rS   r   r   r   r   )r   r   rN   r   r   r   Útest_mutabilityþ   s
    
zTestCommon.test_mutabilityc             C   s   |j ƒ j|jkst‚d S )N)Úviewr   r   )r   r   r   r   r   Ú	test_view  s    zTestCommon.test_viewc             C   s   |j ƒ t|ƒkst‚d S )N)ÚtolistÚlistr   )r   r   r   r   r   Útest_compat  s    zTestCommon.test_compatc       	      C   s,  t |ttjfƒrtjdƒ |jr*tjdƒ |d }d||kjƒ  }}|dkrTt|ƒ}|j	rÄ|j
|dd�}||kstt‚|j
|dd�}||ksŽt‚|j|dd�}||ks¨t‚|j|dd�}||ksÂt‚nd|j�r|j
|dd�}||ksæt‚|j
|dd�}||k�s(t‚n$tjtƒ� |j
|dd� W d Q R X d S )Nz'Skip check for MultiIndex/IntervalIndexzSkip check for empty Indexr   Úleft)ZsideÚright)r
   r   r!   ZIntervalIndexr   r   ÚemptyZargminrS   Zis_monotonic_increasingZ_searchsorted_monotonicr   ZsearchsortedZis_monotonic_decreasingr   r   )	r   r   ÚvalueZexpected_leftZexpected_rightZssm_leftZ	ssm_rightZss_leftZss_rightr   r   r   Útest_searchsorted_monotonic  s0    

z&TestCommon.test_searchsorted_monotonicc             C   s0   |j d }|_ tj|ƒ}|j|ƒs&t‚||_ d S )NZfoo)r   r#   Zround_trip_pickler   r   )r   r   r<   Z	unpickledr   r   r   Útest_pickle7  s    
zTestCommon.test_pickleÚkeepr3   ÚlastFc       
      C   sž   t |ƒ st|ttfƒr"tjdƒ t|ƒ}||ƒ}|jr@|jƒ }t |ƒd }}t	j
j||| ƒ}tj|ƒj|d�j}||j| ƒ}|j|d�}	tj|	|ƒ d S )Nz2Skip check for empty Index, MultiIndex, RangeIndexé
   )rk   )rS   r
   r   r   r   r   r"   Úhas_duplicatesrI   rV   ÚrandomÚchoicer!   ZSeriesZ
duplicatedrU   r#   Zassert_numpy_array_equal)
r   r   rk   ÚholderrY   ÚnÚkZduplicated_selectionr5   r7   r   r   r   Útest_duplicated=  s    
zTestCommon.test_duplicatedc             C   s\   t |ƒ}t|ƒ s t|ttfƒr*tjdƒ ||d gd ƒ}|jdksJt‚|j	dksXt‚d S )Nz6Skip check for empty Index, MultiIndex, and RangeIndexr   rP   FT)
r"   rS   r
   r   r   r   r   r.   r   rn   )r   r   rq   rY   r   r   r   Útest_has_duplicatesX  s    
zTestCommon.test_has_duplicates)r,   r,   r,   )r,   r-   N)r,   NN)Nr-   N)NNN)r@   Ú
__module__Ú__qualname__r   r    r'   r   ÚmarkZparametrizeÚfilterwarningsr+   r6   r8   r:   r>   rC   rG   rO   r\   r^   r_   ra   rd   ri   rj   rt   ru   r   r   r   r   r      s6   ',,r   )Ú__doc__rK   ZnumpyrV   r   Zpandas._libs.tslibsr   Zpandas.core.dtypes.commonr   Zpandasr!   r   r   r   Zpandas._testingZ_testingr#   r   r   r   r   r   Ú<module>   s   
