a
    j=icD                     @   s   d Z ddlZddlZddlZddlmZmZ ddl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 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 except for MultiIndex. Makes use of the `index_flat`
fixture defined in pandas/conftest.py.
    N)IS64pa_version_under7p0)is_integer_dtype)CategoricalIndexDatetimeIndex
MultiIndexPeriodIndex
RangeIndexTimedeltaIndex)NumericIndexc                   @   s   e Zd Zejdddg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d d! Zd"d# Zejd$g d%d&d' Zd(d) Zd*d+ ZdS ),
TestCommonnameNnew_namec                 C   s   |}|r|}n
|j pd}|j|d}|j|u s2J t|jdksDJ |jd |ksVJ || j|jusjJ |jd|d}|j|usJ d S )Nr   r      F)indexr   )r   Zto_framer   lencolumnsvalues)selfr   
index_flatidxZidx_namedf r   q/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pandas/tests/indexes/test_common.pytest_to_frame    s    
zTestCommon.test_to_framec              	   C   s   |}| g |sJ |j|jgfD ]X}t|jtrB||ju rBq&d}tjt|d | | W d    q&1 st0    Y  q&ddgfD ]<}tjtdd | | W d    q1 s0    Y  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\)')	Z	droplevelequalsr   
isinstancetuplepytestraises
ValueErrorKeyError)r   r   r   levelmsgr   r   r   test_droplevel4   s    *zTestCommon.test_droplevelc                 C   s   |}d}dgg}t jt|d |j|d W d    n1 s@0    Y  t jt|d |j|d W d    n1 sz0    Y  d S )Nz"Index.name must be a hashable type1r   r   )names)r"   r#   	TypeErrorrename	set_names)r   r   r   messageZrenamedr   r   r   "test_constructor_non_hashable_nameM   s    *z-TestCommon.test_constructor_non_hashable_namec                 C   s*   |}t |||jd}t|j|j d S )Ndtype)typer1   tmZassert_equal_data)r   r   abr   r   r   test_constructor_unwraps_index\   s    z)TestCommon.test_constructor_unwraps_indexc                 C   s   |}|  }t|| d S N)Zto_flat_indexr3   assert_index_equal)r   r   r   resultr   r   r   test_to_flat_indexc   s    zTestCommon.test_to_flat_indexc                 C   s   |}d}|j }||g}|j |ks(J |j |ks6J |j|dd}|d u sPJ |j |ks^J |j|gksnJ tjtdd |jddd W d    n1 s0    Y  d	}|j|dd |j |ksJ |j|gksJ d S )
Nz#This is the new name for this indexTZinplacezLevel must be Noner   r5   r   r&   )AB)r   r-   r,   r*   r"   r#   r$   )r   r   r   r   original_nameZnew_indresr   r   r   r   test_set_name_methodsj   s     ,z TestCommon.test_set_name_methodsc                 C   sd   ddl m }m} |}||fD ]&}||}||us4J ||sJ q|j ddd}|jdks`J d S )Nr   )copydeepcopyTbanana)deepr   )rC   rD   r   r   )r   r   rC   rD   r   funcZidx_copyZnew_copyr   r   r   test_copy_and_deepcopy   s    z!TestCommon.test_copy_and_deepcopyc                 C   s   |}t ||ddd}t ||dd}||us2J t|| ||sLJ |jdksZJ |jdkshJ tjd|d}tjd|d d	 d}|| }|jjdksJ d S )
NTmario)rC   r   F)rC      )r      )r2   r3   r9   r   r   pdSeriesr   )r   r   r   firstseconds1s2Zs3r   r   r   test_copy_name   s    zTestCommon.test_copy_namec                 C   s   |}|j ddjdksJ tjtdd  |j ddgd W d    n1 sN0    Y  t|j d}tjt|d  |j dggd W d    n1 s0    Y  d S )NrI   r   z$Length of new names must be 1, got 2r   Zluigiz.name must be a hashable type)rC   r   r"   r#   r$   r2   __name__r+   )r   r   r   r'   r   r   r   test_copy_name2   s    .zTestCommon.test_copy_name2c                 C   s   |}|  }d|jd fD ]}|j|d}t|| qd}tjt|d |jdd W d    n1 sj0    Y  dt	|j
  d}tjt|d |jdd W d    n1 s0    Y  d S )	Nr   r=   z.Too many levels: Index has only 1 level, not 4r   rK   z6Requested level \(wrong\) does not match index name \(z\)r   )drop_duplicatesr   uniquer3   r9   r"   r#   
IndexErrorreescape__repr__r%   )r   r   r   expectedr&   r:   r'   r   r   r   test_unique_level   s    *zTestCommon.test_unique_levelc                 C   s  |}t |std |dgd  }|dg }|jdu s<J z|jdu sLJ W n ty`   Y n0 | }t|| |j	std |j
dgd  }tj|d< |d d }||}||}	|	jdu sJ |j|jksJ |	j|jksJ |	}
||	fD ]}| }t||
 qd S )Nz)Skip check for empty Index and MultiIndexr      TFz%Skip na-check if index cannot hold narJ   )r   r"   skip	is_uniquehasnansNotImplementedErrorrW   r3   r9   Z_can_hold_na_valuesnpnanZ_shallow_copyr1   )r   r   r   r   Z
idx_uniquer:   valsZvals_uniqueZidx_nanZidx_unique_nanr\   ir   r   r   test_unique   s4    





zTestCommon.test_uniquec                 C   s\  |}t |tjr,tjjdtd}|j| |j	r<t
d |d }d||k  }}|dkrft|}|jr|j|dd}||ksJ |j|dd}	||	ksJ |j|dd}
||
ksJ |j|dd}||ksJ n|jr|j|dd}||ksJ |j|dd}	||	ksXJ nBd}tjt|d	 |j|dd W d    n1 sN0    Y  d S )
Nz8IntervalIndex.searchsorted does not support Interval arg)reasonr#   zSkip check for empty Indexr   left)siderightz0index must be monotonic increasing or decreasingr   )r    rM   ZIntervalIndexr"   markxfailrb   node
add_markeremptyr_   Zargminr   Zis_monotonic_increasingZ_searchsorted_monotonicZsearchsortedZis_monotonic_decreasingr#   r$   )r   r   requestr   rm   valueZexpected_leftZexpected_rightZssm_leftZ	ssm_rightZss_leftZss_rightr'   r   r   r   test_searchsorted_monotonic   s<    
z&TestCommon.test_searchsorted_monotonicc                 C   s   |}t |trtd t|dkr.td t|}tt|}t |trR|j	nd }|||d}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 duplicatesr0   g      ?)keep)r    r	   r"   r_   r   r2   listsetr   r1   rd   randomchoiceintr   rM   rN   
duplicatedr3   assert_numpy_array_equalrV   r9   )r   r   ru   r   holderunique_valuesr1   
unique_idxnZduplicated_selectionr   expected_duplicatedZexpected_droppedr   r   r   test_drop_duplicates+  s*    
zTestCommon.test_drop_duplicatesc           	      C   s   |}t |tr|}n4t|}tt|}t |tr8|jnd }|||d}tjdgt	| dd}t
| | | }t
|| ||usJ d S )Nr0   Fbool)r    r	   r2   rv   rw   r   r1   rd   arrayr   r3   r|   r{   rV   r9   )	r   r   r   r   r}   r~   r1   r   Zresult_droppedr   r   r   "test_drop_duplicates_no_duplicatesN  s    
z-TestCommon.test_drop_duplicates_no_duplicatesc                 C   sB   d}t jt|d |jdd W d    n1 s40    Y  d S )Nz6drop_duplicates\(\) got an unexpected keyword argumentr   Tr<   )r"   r#   r+   rV   )r   r   r'   r   r   r   test_drop_duplicates_inplaced  s    z'TestCommon.test_drop_duplicates_inplacec                 C   sZ   |}t |}t|rt|tr(td ||d gd }|jdu sHJ |jdu sVJ d S )Nz6Skip check for empty Index, MultiIndex, and RangeIndexr   r^   FT)r2   r   r    r	   r"   r_   r`   Zhas_duplicates)r   r   r   r}   r   r   r   r   test_has_duplicatesi  s    
zTestCommon.test_has_duplicatesr1   )int64uint64float64categorydatetime64[ns]ztimedelta64[ns]c                 C   s  t |tr"dd t|jD |_nd|_d }t |trN|jd urN|dkrNt}n|j	j
dkrh|dv rhtj}t|j	dkoto|dk}z>tj||d	d
 ||}W d    n1 s0    Y  W n ttttfy   Y d S 0 t |tr|j|jksJ n|j|jksJ d S )Nc                 S   s   g | ]}d t | qS )r   )str).0rg   r   r   r   
<listcomp>      z9TestCommon.test_astype_preserves_name.<locals>.<listcomp>r   r   c)r   r   r   zstring[pyarrow]r   F)Zraise_on_extra_warningsZcheck_stacklevel)r    r   rangeZnlevelsr*   r   r   tzFutureWarningr1   kindrd   ZComplexWarningr   r   r3   assert_produces_warningZastyper$   r+   rb   SystemError)r   r   r1   warnZis_pyarrow_strr:   r   r   r   test_astype_preserves_namex  s<    
,
z%TestCommon.test_astype_preserves_namec                 C   sN   t |tttfrd }nt}t| |j W d    n1 s@0    Y  d S r8   )r    r   r
   r   r   r3   r   Zasi8)r   r   r   r   r   r   test_asi8_deprecation  s
    z TestCommon.test_asi8_deprecationc                 C   s   |}|j dd}tjdgt| td}t|j| |jdu sDJ |j dd}|j	}t|dkrfd S t
|tr~t|jr~d S |jtkrd S tj|d< t||}tjdgt| td}d|d< t|j| |jdu sJ d S )NT)rF   Fr0   r   r   )rC   rd   r   r   r   r3   r|   Z_isnanra   rc   r    r   r   r1   re   r2   )r   r   r   r   r\   r   r   r   r   test_hasnans_isnans  s&    

zTestCommon.test_hasnans_isnans)rT   
__module____qualname__r"   rm   parametrizer   r(   r/   r7   r;   rB   rH   rS   rU   r]   rh   rt   r   r   r   r   r   r   r   r   r   r   r   r      s0   
(2#
(
r   na_positionmiddlec                 C   sD   t jtd| d | j|d W d    n1 s60    Y  d S )Nzinvalid na_position: r   r   )r"   r#   r$   sort_values)index_with_missingr   r   r   r   $test_sort_values_invalid_na_position  s    r   rO   lastc                 C   s   t | tr"|jtjjddd t| 	 }| | 
  j}t|}|dkrftd g| |g}nt|d g| g}t| || jd}| j|d}t|| d S )Nz,missing value sorting order not well-definedF)ri   strictrO   r0   r   )r    r   ro   rp   r"   rm   rn   rd   sumZisnaZnotnar   sortZconcatenater2   r1   r   r3   r9   )r   r   rr   Zmissing_countZnot_na_valsZsorted_valuesr\   r:   r   r   r   test_sort_values_with_missing  s    

r   c                 C   s   t | trtstd | }|j|s,J | |s>J |j}|j	|j	ksTJ |j
|j
ksdJ |j|jkstJ t | ttfs|j|jksJ |j |jj d S )NOverflow)r    r   r   r"   r_   Tr   Z	transposer   shapendimsizer	   r   nbytes)r   r   r   r   r   r   test_ndarray_compat_properties  s    
r   )__doc__rY   numpyrd   r"   Zpandas.compatr   r   Zpandas.core.dtypes.commonr   ZpandasrM   r   r   r   r   r	   r
   Zpandas._testingZ_testingr3   Zpandas.core.apir   r   rm   r   r   r   r   r   r   r   r   <module>   s$       1

