U
    f/e8                     @   s   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	m
Z
mZ ddlZddlmZmZmZmZmZmZ ddlmZ G dd dZejd	dd
gdd Zejd	ddgdd Z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)IS64)is_period_dtypeneeds_i8_conversion)CategoricalIndexDatetimeIndex
MultiIndexPeriodIndex
RangeIndexTimedeltaIndexc                	   @   s   e Z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dddddd d!gd"d# Zd$d% Zd&S )'
TestCommonc              
   C   s   t |trd S |g |s"t|j|jgfD ]D}t |jtrL||jkrLq0d}tjt	|d || W 5 Q R X q0ddgfD ](}tjt
dd || W 5 Q R X qd S )NzTCannot remove 1 levels from an index with 1 levels: at least one level must be left.matchwrongz>'Requested level \(wrong\) does not match index name \(None\)')
isinstancer   Z	droplevelequalsAssertionErrornametuplepytestraises
ValueErrorKeyError)selfindexlevelmsg r   D/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/indexes/test_common.pytest_droplevel    s     
zTestCommon.test_droplevelc              	   C   s`   |}d}dgg}t jt|d |j|d W 5 Q R X t jt|d |j|d W 5 Q R X d S )Nz"Index.name must be a hashable type1r   )r   )names)r   r   	TypeErrorrename	set_names)r   
index_flatr   messageZrenamedr   r   r   "test_constructor_non_hashable_name:   s    z-TestCommon.test_constructor_non_hashable_namec                 C   s$   |}t ||}t|j|j d S N)typetmZassert_equal_data)r   r%   abr   r   r   test_constructor_unwraps_indexI   s    z)TestCommon.test_constructor_unwraps_indexc                 C   s   |}|  }t|| d S r(   )Zto_flat_indexr*   assert_index_equal)r   r%   r   resultr   r   r   test_to_flat_indexN   s    zTestCommon.test_to_flat_indexc              	   C   s   |}d}|j }||g}|j |ks(t|j |ks6t|j|dd}|d ksPt|j |ks^t|j|gksnttjtdd |jddd W 5 Q R X d	}|j|dd |j |kst|j|gkstd S )
Nz#This is the new name for this indexTZinplacezLevel must be Noner   r,   r   r   )AB)r   r$   r   r#   r!   r   r   r   )r   r%   r   new_nameoriginal_nameZnew_indresr   r   r   r   test_set_name_methodsU   s     z TestCommon.test_set_name_methodsc                 C   sd   ddl m }m} |}||fD ]&}||}||k	s4t||stq|j ddd}|jdks`td S )Nr   )copydeepcopyTZbanana)deepr   )r:   r;   r   r   r   )r   r%   r:   r;   r   funcZidx_copyZnew_copyr   r   r   test_copy_and_deepcopyq   s    z!TestCommon.test_copy_and_deepcopyc              	   C   s   |}|  }d|jd fD ]}|j|d}t|| qd}tjt|d |jdd W 5 Q R X dt	|j
  d}tjt|d |jdd W 5 Q R X d S )	Nr   r3   z.Too many levels: Index has only 1 level, not 4r      z6Requested level \(wrong\) does not match index name \(z\)r   )drop_duplicatesr   uniquer*   r/   r   r   
IndexErrorreescape__repr__r   )r   r%   r   expectedr   r0   r   r   r   r   test_unique   s    zTestCommon.test_uniquec                 C   s  |}t |std |dgd  }|dg }|jdks<tz|jdksLtW n tk
rb   Y nX | }t	|| |j
std t|jr|dgd  j}tj|d< n@t|jr|jjdgd  }t|d< n|jdgd  }tj|d< |d d }|jjdkr2t|j||jd	}t|jj||jd	}||}||}	|	jdksVt|j|jksht|	j|jkszt|	}
||	fD ]}| }t	||
 qd S )
Nz)Skip check for empty Index and MultiIndexr      TFz%Skip na-check if index cannot hold na   )mMdtype)lenr   skip	is_uniquer   ZhasnansNotImplementedErrorZ_get_unique_indexr*   r/   Z_can_hold_nar   rM   r+   pdZNaTr   Z_ndarrayr   valuesnpnankindr)   Z_simple_newZ_shallow_copy)r   r%   r   idxZ
idx_uniquer0   valsZvals_uniqueZidx_nanZidx_unique_nanrF   ir   r   r   test_get_unique_index   sF    








z TestCommon.test_get_unique_indexc              	   C   s4  |}t |tjrtd |jr*td |d }d||k  }}|dkrTt|}|jr|j	|dd}||kstt
|j	|dd}||kst
|j|dd}||kst
|j|dd}	||	kst
nl|jr|j	|dd}||kst
|j	|dd}||ks0t
n,d}
tjt|
d |j	|dd W 5 Q R X d S )	Nz'Skip check for MultiIndex/IntervalIndexzSkip check for empty Indexr   left)Zsiderightz0index must be monotonic increasing or decreasingr   )r   rR   ZIntervalIndexr   rO   emptyZargminrN   Zis_monotonic_increasingZ_searchsorted_monotonicr   ZsearchsortedZis_monotonic_decreasingr   r   )r   r%   r   valueZexpected_leftZexpected_rightZssm_leftZ	ssm_rightZss_leftZss_rightr   r   r   r   test_searchsorted_monotonic   s4    

z&TestCommon.test_searchsorted_monotonicc                 C   s   |}t |trtd t|dkr.td t|}tt|}||}t|}tj	
|t|d }||j| }	t|j|dj}
t|	j|d|
 |t|	j|d}t|	j|d| d S )NzWRangeIndex is tested in test_drop_duplicates_no_duplicates as it cannot hold duplicatesr   zXempty index is tested in test_drop_duplicates_no_duplicates as it cannot hold duplicatesg      ?)keep)r   r
   r   rO   rN   r)   listsetrT   randomchoiceintrS   rR   ZSeries
duplicatedr*   assert_numpy_array_equalr@   r/   )r   r%   r`   r   holderunique_values
unique_idxnZduplicated_selectionrW   expected_duplicatedZexpected_droppedr   r   r   test_drop_duplicates   s(    
zTestCommon.test_drop_duplicatesc                 C   s|   |}t |tr|}nt|}tt|}||}tjdgt| dd}t	|
 | | }t|| ||k	sxtd S )NFboolrL   )r   r
   r)   ra   rb   rT   arrayrN   r*   rg   rf   r@   r/   r   )r   r%   r   rj   rh   ri   rl   Zresult_droppedr   r   r   "test_drop_duplicates_no_duplicates  s    
z-TestCommon.test_drop_duplicates_no_duplicatesc              	   C   s.   d}t jt|d |jdd W 5 Q R X d S )Nz6drop_duplicates\(\) got an unexpected keyword argumentr   Tr2   )r   r   r"   r@   )r   r   r   r   r   r   test_drop_duplicates_inplace.  s    z'TestCommon.test_drop_duplicates_inplacec                 C   sZ   |}t |}t|rt|tr(td ||d gd }|jdksHt|jdksVtd S )Nz6Skip check for empty Index, MultiIndex, and RangeIndexr   rH   FT)	r)   rN   r   r
   r   rO   rP   r   Zhas_duplicates)r   r%   r   rh   rW   r   r   r   test_has_duplicates3  s    
zTestCommon.test_has_duplicatesrM   int64uint64Zfloat64categorydatetime64[ns]ztimedelta64[ns]c                 C   s   t |tr"dd t|jD |_nd|_d }|dkrDt|jrdt}n t |t	rd|j
d k	rd|dkrdt}z$t| ||}W 5 Q R X W n ttttfk
r   Y d S X t |tr|j|jkstn|j|jkstd S )Nc                 S   s   g | ]}d t | qS )rW   )str).0rY   r   r   r   
<listcomp>I  s     z9TestCommon.test_astype_preserves_name.<locals>.<listcomp>rW   )rs   rt   rv   )r   r   rangeZnlevelsr!   r   r   rM   FutureWarningr   tzr*   assert_produces_warningZastyper   r"   rQ   SystemErrorr   )r   r   rM   warnr0   r   r   r   test_astype_preserves_nameB  s,    


z%TestCommon.test_astype_preserves_namec              	   C   s:   t |tttfrd }nt}t| |j W 5 Q R X d S r(   )r   r   r   r	   r{   r*   r}   Zasi8)r   r   r   r   r   r   test_asi8_deprecationd  s
    z TestCommon.test_asi8_deprecationN)__name__
__module____qualname__r   r'   r.   r1   r9   r>   rG   rZ   r_   rm   rp   rq   rr   r   markparametrizer   r   r   r   r   r   r      s&   3."
r   na_positionZmiddlec              	   C   s0   t jtd| d | j|d W 5 Q R X d S )Nzinvalid na_position: r   r   )r   r   r   sort_values)index_with_missingr   r   r   r   $test_sort_values_invalid_na_positiono  s    r   firstlastc                 C   s   t | trtd t|  }| |   j}t	|}|dkrXt
d g| |g}nt
|d g| g}t| |}| j|d}t|| d S )Nz,missing value sorting order not well-definedr   r   )r   r   r   rO   rT   sumZisnaZnotnarS   sortZconcatenater)   r   r*   r/   )r   r   Zmissing_countZnot_na_valsZsorted_valuesrF   r0   r   r   r   test_sort_values_with_missingv  s    


r   c                 C   s   t | trtstd | }|j|s,t| |s>t|j	}|j
|j
ksTt|j|jksdt|j|jksttt | ttfs|j|jkst|j |j	j d S )NOverflow)r   r	   r   r   rO   Tr   r   Z	transposerS   shapendimsizer
   r   nbytes)r   rW   rS   r   r   r   test_ndarray_compat_properties  s    
r   )__doc__rC   ZnumpyrT   r   Zpandas._libs.tslibsr   Zpandas.compatr   Zpandas.core.dtypes.commonr   r   ZpandasrR   r   r   r   r	   r
   r   Zpandas._testingZ_testingr*   r   r   r   r   r   r   r   r   r   r   <module>   s"      R

