3
pd}                 @   s   d dl Z d dlmZ d dlZd dlZd dlmZ d dlm	Z	 d dl
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mZmZmZmZmZmZ d dljZd dlm Z  G d	d
 d
Z!dS )    N)Type)iNaT)InvalidIndexError)is_datetime64tz_dtype)CategoricalDtype)CategoricalIndexDatetimeIndexIndex
Int64IndexIntervalIndex
MultiIndexPeriodIndex
RangeIndexSeriesTimedeltaIndexUInt64Indexisna)DatetimeIndexOpsMixinc               @   s^  e Zd ZU dZee ddddgZedddZd	d
 Z	e
j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d.d/ Zd0d1 Zd2d3 Zd4d5 Z e
jjd6e!e"e#j$e%gd7d8 Z&e
jjd9d:d;ge
jjd<d=d>d?d@gdAdB Z'dCdD Z(dEdF Z)dGdH Z*dIdJ Z+dKdL Z,dMdN Z-dOdP Z.dQdR Z/dSdT Z0dUdV Z1dWdX Z2dYdZ Z3d[d\ Z4d]d^ Z5d_d` Z6dadb Z7e
jjdcddde dfde gdgdh Z8didj Z9dkdl Z:e
jjdmdndoge
jjdddpge
jjdqdndogdrds Z;dtdu Z<dvdw Z=dxdy Z>dzd{ Z?d|d} Z@d~d ZAdS )Basez& base class for index sub-class tests shapendimsizenbytes)returnc             C   s   t dd S )NzMethod not implemented)NotImplementedError)self r   =/tmp/pip-build-7vycvbft/pandas/pandas/tests/indexes/common.pycreate_index&   s    zBase.create_indexc          	   C   s*   d}t jt|d | j  W d Q R X d S )NzIndex\(\.\.\.\) must be called with a collection of some kind, None was passed|__new__\(\) missing 1 required positional argument: 'data'|__new__\(\) takes at least 2 arguments \(1 given\))match)pytestraises	TypeError_holder)r   msgr   r   r   test_pickle_compat_construction)   s    z$Base.test_pickle_compat_constructionnameNnew_namec             C   s   | j  }|r|}n
|jpd}|j|d}|j|ks6tt|jdksHt|jd |ksZt|| j|jk	snt|jd|d}|j|k	std S )Nr   )r&      F)indexr&   )r   r&   Zto_framer)   AssertionErrorlencolumnsvalues)r   r&   idxZidx_nameZdfr   r   r   test_to_frame4   s    
zBase.test_to_framec             C   sf   | j  }dt|j }tjt|d |jd W d Q R X tjt|d |jdd W d Q R X d S )NzNot supported for type )r   r(      )r   type__name__r    r!   r   shift)r   r.   r$   r   r   r   
test_shiftH   s    zBase.test_shiftc             C   s6   | j  }tjtdd t||g d W d Q R X d S )Nz"Index.name must be a hashable type)r   )r&   )r   r    r!   r"   r1   )r   r.   r   r   r    test_constructor_name_unhashableR   s    z%Base.test_constructor_name_unhashablec             C   s   | j  }t|tsPd|_tj|}tj|| tj|dd}d|_tj|| n~ddg|_tj|}tj|ttddddddgdd	ddgd
 tj|ddgd
}tj|ttddddddgdd	ddgd
 d S )Nfoobar)r&   onetwobazquxobject)dtype)namesAB)r6   r8   )r6   r9   )r7   r8   )r:   r9   )r;   r8   )r;   r9   )r6   r8   )r6   r9   )r7   r8   )r:   r9   )r;   r8   )r;   r9   )	r   
isinstancer   r&   pdr	   tmassert_index_equalr>   )r   expectedresultr   r   r   test_create_index_existing_nameY   sF    



z$Base.test_create_index_existing_namec          ,   C   s   | j  }t|t sttjtdd |d  W d Q R X tjtdd d|  W d Q R X d}tjt|d |d  W d Q R X |jdd}tjt|d d|  W d Q R X tjtdd |d  W d Q R X tjtd	d d|  W d Q R X d S )
Nzcannot perform __mul__)r   r(   zcannot perform __rmul__zcannot perform __truediv__z __z __rzcannot perform __floordiv__zcannot perform __rfloordiv__)r   rA   r   r*   r    r!   r"   replace)r   r.   Zdiv_errr   r   r   test_numeric_compat   s     zBase.test_numeric_compatc             C   sP   | j  }tjtdd |j  W d Q R X tjtdd |j  W d Q R X d S )Nzcannot perform all)r   zcannot perform any)r   r    r!   r"   allany)r   r.   r   r   r   test_logical_compat   s
    zBase.test_logical_compatc             C   s\   | j  }tj|jtjd}|j|}tj|| tj	t
dd |j|dd W d Q R X d S )N)r=   zInvalid fill method)r   invalid)method)r   nparanger   intpget_indexerrC   assert_numpy_array_equalr    r!   
ValueError)r   r.   rE   actualr   r   r   test_reindex_base   s    
zBase.test_reindex_basec             C   s   t |trd S |jst |trR|j|dd }t |tjs@t|jtj	kstn0d}t
jt|d |j|dd  W d Q R X |j|dd \}}t |tjst|jtj	kstd S )Nr   r0   z8Reindexing only valid with uniquely valued Index objects)r   )rA   r   	is_uniquer   rR   rO   ndarrayr*   r=   rQ   r    r!   r   Zget_indexer_non_unique)r   r)   indexere_r   r   r   test_get_indexer_consistency   s    
z!Base.test_get_indexer_consistencyc             C   sj   | j  }|jj|st|j j|s*t|j}x&| jD ]}t||t||ks8tq8W |j |jj d S )N)	r   Tequalsr*   Z	transposer-   _compat_propsgetattrr   )r   r.   r-   propr   r   r   test_ndarray_compat_properties   s    z#Base.test_ndarray_compat_propertiesc             C   s    | j  }tjtt|| d S )N)r   rC   rD   evalrepr)r   r.   r   r   r   test_repr_roundtrip   s    zBase.test_repr_roundtripc          
   C   sF   | j  }|jd}tjdd  t| dt|ks8tW d Q R X d S )N2   zdisplay.max_seq_itemsz...)r   repeatrB   Zoption_contextrd   strr*   )r   r.   r   r   r   test_repr_max_seq_item_setting   s
    
z#Base.test_repr_max_seq_item_settingc             C   s   t |trd S t||ddd}t||dd}||k	s<t|j|sJt|jdksXt|jdksfttd|d}td|d d
 d}t |ts|| }|jjdkstd S )NTZmario)copyr&   F)rj   r0   )r)      r(   )	rA   r   r1   r*   r^   r&   r   r   r)   )r   r)   firstseconds1s2Zs3r   r   r   test_copy_name   s    

zBase.test_copy_namec             C   s   i }t |tr|j|d< nt |tttfr.d S t|}||jfddi|}t|j	rh|j
dj|j}t |ttfr|jd }tj|| t |tr|f |jdd|}tj|j|jdd n6t |trn*||jfddi|}tj|j|jdd d S )	Nfreqrj   TUTCF)Zordinalrj   Zsame)Z
check_same)rA   r   rr   r   r   r   r1   r-   r   r=   Ztz_localizeZ
tz_converttzr   r   
_with_freqrC   rD   asi8rS   r   )r   r)   Zinit_kwargsZ
index_typerF   r   r   r   test_ensure_copied_data   s&    




zBase.test_ensure_copied_datac             C   s~   |j j  |j }|jr(|dks$td S |j|d  |j }|jdd}t|ttfsd||ksdt|j	dkrz||ksztd S )Nr   T)deepr<   )
_engineZclear_mappingZmemory_usageemptyr*   get_locrA   r   r   Zinferred_type)r   r)   rF   Zresult2Zresult3r   r   r   test_memory_usage  s    

zBase.test_memory_usagec             C   s8   t |trd S |j }tj|j }tj||dd d S )NF)Zcheck_dtype)rA   r   argsortrO   arrayrC   rS   )r   requestr)   rF   rE   r   r   r   test_argsort2  s
    
zBase.test_argsortc             C   s   t j|}|j }tj|| tt|ttfrd}tj	t
|d t j|dd W d Q R X d}tj	t
|d t j|dd W d Q R X d}tj	t
|d t j|dd W d Q R X d S )Nz%the 'axis' parameter is not supported)r   r(   )axisz%the 'kind' parameter is not supportedZ	mergesort)kindz&the 'order' parameter is not supportedab)order)r   r   )rO   r}   rC   rS   rA   r1   r   r   r    r!   rT   )r   r)   rF   rE   r$   r   r   r   test_numpy_argsort;  s    
	zBase.test_numpy_argsortc          	   C   st   ddddg}t |dk rd S |j|}|| }|j|s<tt|tttfspd}tj	t
|d |j W d Q R X d S )N   rk   r   r0      z*'(.*Index)' object has no attribute 'freq')r   )r+   taker^   r*   rA   r   r   r   r    r!   AttributeErrorrr   )r   r)   rY   rF   rE   r$   r   r   r   	test_takeT  s    
zBase.test_takec             C   s   | j  }ddg}d}tjt|d |j|dd W d Q R X d}tjt|d |j||d W d Q R X d}tjt|d |j|d	d
 W d Q R X d S )Nr(   r0   z1take\(\) got an unexpected keyword argument 'foo')r   )r6   z$the 'out' parameter is not supported)outz%the 'mode' parameter is not supportedZclip)mode)r   r    r!   r"   r   rT   )r   r.   indicesr$   r   r   r   test_take_invalid_kwargsd  s    zBase.test_take_invalid_kwargsc             C   sz   d}| j  }tj|jj||jd}tj|j|| | j  }tj	t
|}tj|jj||jd}tj|j|| d S )Nr0   )r&   )r   rB   r	   r-   rg   r&   rC   rD   rO   rP   r+   )r   repirE   r   r   r   test_repeatt  s    zBase.test_repeatc             C   s\   d}| j  }|j|}tjtj||| d}tjt|d tj||dd W d Q R X d S )Nr0   z%the 'axis' parameter is not supported)r   r   )r   )r   rg   rC   rD   rO   r    r!   rT   )r   r   r   rE   r$   r   r   r   test_numpy_repeat  s    
zBase.test_numpy_repeatklassc             C   s   | j  }t|tjtjfr$|jd }dgt| }|j||}|}tj	|| dgdgt|dd    }tj
|jg|dd  j  |jd}|j||}tj	|| d S )NTFr(   )r=   )r   rA   rB   r   r   ru   r+   whererC   rD   r	   Z	_na_valuetolistr=   )r   r   r   ZcondrF   rE   r   r   r   
test_where  s    
$zBase.test_wherecaseg      ?ZxxxrN   intersectionunion
differencesymmetric_differencec          
   C   s0   d}t jt|d t||| W d Q R X d S )Nz!Input must be Index or array-like)r   )r    r!   r"   r`   )r   r   rN   r)   r$   r   r   r   test_set_ops_error_cases  s    zBase.test_set_ops_error_casesc                s   t |trd S |d d }|d d  |j }tj| s@tt|jrNd S  fddtj	t
tgD }x&|D ]}|j|}tj| sntqnW t |trd}tjt|d |jdddg W d Q R X d S )	Nr   rk   c                s   g | ]}| j qS r   )r-   ).0r   )rn   r   r   
<listcomp>  s    z/Base.test_intersection_base.<locals>.<listcomp>z.other must be a MultiIndex or a list of tuples)r   r(   r0   )rA   r   r   rC   equalContentsr*   r   r=   rO   r~   r   listr   r    r!   r"   )r   r)   rm   Z	intersectcasesr   rF   r$   r   )rn   r   test_intersection_base  s     





zBase.test_intersection_basec       	         s   |dd  }|d d  |}|j  }tj||s6tt|jrDd S  fddtjtt	gD }x0|D ](}t
|tsd|j |}tj||sdtqdW t
|trd}tjt|d |j dddg W d Q R X d S )	Nrk   r   c                s   g | ]}| j qS r   )r-   )r   r   )rn   r   r   r     s    z(Base.test_union_base.<locals>.<listcomp>z.other must be a MultiIndex or a list of tuples)r   r(   r0   )r   rC   r   r*   r   r=   rO   r~   r   r   rA   r   r   r    r!   r"   )	r   r)   rm   Z
everythingr   r   r   rF   r$   r   )rn   r   test_union_base  s     





zBase.test_union_basec       	         s  |dd  }|d d  t |ts*|j r0g }n|dd  }|j |}tj||sXt fddtjt	t
gD }xd|D ]\}t |ttfrt|t|ksttj|j j|j j qx|j||}tj||sxtqxW t |trd}tjt|d |jdddg| W d Q R X d S )	Nr0   r   c                s   g | ]}| j qS r   )r-   )r   r   )rn   r   r   r     s    z-Base.test_difference_base.<locals>.<listcomp>z.other must be a MultiIndex or a list of tuples)r   r(   rk   )rA   r   Z
is_booleanr   rC   r   r*   rO   r~   r   r   r   r   r1   rS   Zsort_valuesrv   r   r    r!   r"   )	r   sortr)   rm   answerrF   r   r   r$   r   )rn   r   test_difference_base  s&    
zBase.test_difference_basec                s   t |trd S |dd  }|d d	  |dd
g }|j }tj||sLt fddtjtt	gD }x&|D ]}|j|}tj||sltqlW t |t
rd}tjt|d |jdddg W d Q R X d S )Nr(   r   c                s   g | ]}| j qS r   )r-   )r   r   )rn   r   r   r     s    z2Base.test_symmetric_difference.<locals>.<listcomp>z.other must be a MultiIndex or a list of tuples)r   r0   rk   rl   rl   )rA   r   r   rC   r   r*   rO   r~   r   r   r   r    r!   r"   )r   r)   rm   r   rF   r   r   r$   r   )rn   r   test_symmetric_difference  s    




zBase.test_symmetric_differencec             C   s>   |dd }t |sd S |dd j|jd|d s:td S )Nr(   r   r   )r+   r^   insertr*   )r   r)   rF   r   r   r   test_insert_base  s    zBase.test_insert_basec          	   C   s   t |sd S t|trd S |dd  }|jd}|j|s>t|j|jksNt|d d }|jd}|j|srt|j|jkstt |}d| d| }tjt	|d |j| W d Q R X d S )Nr(   r   zindex z' is out of bounds for axis 0 with size )r   rl   rl   )
r+   rA   r   deleter^   r*   r&   r    r!   
IndexError)r   r)   rE   rF   lengthr$   r   r   r   test_delete_base  s     


zBase.test_delete_basec             C   s   t |trd S |j|st|j|j s.t|j|jtsBt|jt| sVt|jtj	| sltt |t
st|td}|j|st|j|st|jdkr|jt| std S )N)r=   r(   )rA   r   r^   r*   rj   astyper<   r   rO   r~   r   r	   Znlevelsr   )r   r)   Zsame_valuesr   r   r   test_equals%  s    


zBase.test_equalsc          <   C   s  | j  }t|trtjd t|}|dd }|dd j|dd }|dd }d}tjt|d ||k W d Q R X t	j
dg| }t	j
dg|d  dg }tj||k| tj||k| t	j
|}	t	j
|dd }
t	j
|dd j|dd }t	j
|dd }tjt|d ||
k W d Q R X tj||	k| tj||k| t|	}t|
}t|}t|}tjt|d ||k W d Q R X tj||k| tj||k| tjtd	d ||k W d Q R X tjtd	d ||k W d Q R X tjtd	d ||k W d Q R X d
}tjt|d ||k W d Q R X tjtd	d ||k W d Q R X t|tst	j
dgt|d  ddg }|d }tj||k| tj||kt| d S )NzSkip check for PeriodIndexr   r(   r0   z)Lengths must match|could not be broadcast)r   TFzLengths must matchz3Can only compare identically-labeled Series objectsrl   rl   rl   rl   rl   r   rl   r   )r   rA   r   r    skipr+   appendr!   rT   rO   r~   rC   rS   r   r   Zassert_series_equal)r   Zindex_anZindex_bZindex_cZindex_dr$   Z	expected1Z	expected2Zarray_aZarray_bZarray_cZarray_dZseries_aZseries_bZseries_cZseries_dZ	expected3itemr   r   r   test_equals_op;  s\    


  zBase.test_equals_opc             C   s*   | j  }dd |D }|j |ks&td S )Nc             S   s   g | ]}t |qS r   )rh   )r   xr   r   r   r   ~  s    z$Base.test_format.<locals>.<listcomp>)r   formatr*   )r   r.   rE   r   r   r   test_format{  s    zBase.test_formatc             C   s4   | j g }|j g kst|jdddgks0td S )NT)r&    )r#   r   r*   )r   Z	empty_idxr   r   r   test_format_empty  s    
zBase.test_format_emptyc             C   s  t |trd S |jdd}tjdgt| td}tj|j	| |j
dksNt|jdd}tj|j}t|dkrvd S t |trt|d< nt |ttfrd S tj|d< t |trt|||jd}nJt||}tjdgt| td}d|d< tj|j	| |j
dkstd S )NT)rx   F)r=   r   r(   )rr   )rA   r   rj   rO   r~   r+   boolrC   rS   _isnanhasnansr*   asarrayr-   r   r   r
   r   nanr   r1   rr   )r   r)   r.   rE   r-   r   r   r   test_hasnans_isnans  s,    




zBase.test_hasnans_isnansc             C   sl  t |dkrnXt|trV|jdd}d}tjt|d |j|d  W d Q R X n|jdd}|j|d }tj	|| ||k	st
d}tjt|d |j|d g W d Q R X |jdd}tj|j}t|trt|d< nt|ttfrd S tj|d< t|trt|||jd}nt||}tjd	gt | td
}d|d< tj|j| |jdksht
d S )Nr   T)rx   z"isna is not defined for MultiIndex)r   z"'value' must be a scalar, passed: r(   )rr   F)r=   )r+   rA   r   rj   r    r!   r   ZfillnarC   rD   r*   r"   rO   r   r-   r   r   r
   r   r   r   r1   rr   r~   r   rS   r   r   )r   r)   r.   r$   rF   r-   rE   r   r   r   test_fillna  s8    



zBase.test_fillnac          	   C   s   t |dkr(tj|j tjg td nt|trb|j	 }d}t
jt|d |j  W d Q R X nn|jstj|j tjt |td tj|j tjt |td n*t|}tj|j | tj|j |  d S )Nr   )r=   z"isna is not defined for MultiIndex)r   )r+   rC   rS   r   rO   r~   r   rA   r   rj   r    r!   r   r   zerosZnotnaones)r   r)   r.   r$   rF   r   r   r   
test_nulls  s    
 zBase.test_nullsc             C   s*   | j  }|j st|d d js&td S )Nr   )r   rz   r*   )r   r)   r   r   r   
test_empty  s    zBase.test_emptyc             C   s0   | j  }|jr,|j||d}||kj s,td S )N)how)r   rW   joinrJ   r*   )r   Z	join_typer)   Zjoinedr   r   r   test_join_self_unique  s    zBase.test_join_self_uniquec             C   sB   | j  }t|tjr |jd}n|}|jdd }tj|| d S )Nint64c             S   s   | S )Nr   )r   r   r   r   <lambda>  s    zBase.test_map.<locals>.<lambda>)r   rA   rB   r   r   maprC   rD   )r   r)   rE   rF   r   r   r   test_map  s    zBase.test_mapmapperc             C   s   dd t | |D S )Nc             S   s   i | ]\}}||qS r   r   )r   rZ   r   r   r   r   
<dictcomp>  s    z!Base.<lambda>.<locals>.<dictcomp>)zip)r-   r)   r   r   r   r     s    zBase.<lambda>c             C   s   t j| |S )N)rB   r   )r-   r)   r   r   r   r     s    c             C   s   | j  }t|tjtjfr.tjdt|  ||j|}t|tj	r\t|t
r\|jd}n|}|j|}tj|| tjtjgt| }|j|||}tj|| d S )Nzskipping tests for r   )r   rA   rB   r   r   r    r   r1   r-   r   dictr   r   rC   rD   r	   rO   r   r+   )r   r   r)   identityrE   rF   r   r   r   test_map_dictlike  s    	
zBase.test_map_dictlikec             C   s8   | j  }|jt}tdd |D td}tj|| d S )Nc             S   s   g | ]}t |qS r   )rh   )r   r   r   r   r   r     s    z%Base.test_map_str.<locals>.<listcomp>)r=   )r   r   rh   r	   r<   rC   rD   )r   r)   rF   rE   r   r   r   test_map_str  s    
zBase.test_map_strc             C   s   | j  }d}tjt|d$ |jtjt|d tjd W d Q R X tjt|d$ |jtjt|d tjd W d Q R X tjt|d |jdd W d Q R X d S )Nz,putmask: mask and data must be the same size)r   r(   r6   )	r   r    r!   rT   ZputmaskrO   r   r+   Zbool_)r   r)   r$   r   r   r   test_putmask_with_wrong_mask  s    ((z!Base.test_putmask_with_wrong_maskrj   TFr6   orderedc             C   s   | j  }|r|j|}t|d}|j||d}t|j||d}tj|| t|j j	 d d	 |}|j||d}t|j||d}tj|| |dkr|jd|d}t|j|d}tj|| d S )
N)r   )rj   )r&   r   r(   )r&   r=   Fcategory)r&   rl   )
r   renamer   r   r   r-   rC   rD   uniquer   )r   rj   r&   r   r)   r=   rF   rE   r   r   r   test_astype_category,  s    

zBase.test_astype_categoryc             C   s   | j  j }|jdkst|d d }|jdks4t|jd|d }|jdksRt|jdtj}|jdksnt|jdtj}|jdkstd S )NTr   F)r   Zdrop_duplicatesrW   r*   r   rO   r   )r   r)   Zindex_emptyZ	index_dupZindex_naZindex_na_dupr   r   r   test_is_uniqueG  s    zBase.test_is_uniquec             C   s6   | j  }ttj|}|j ttj||ks2td S )N)r   r+   gcZget_referrersry   r*   )r   r)   Z	nrefs_prer   r   r   test_engine_reference_cycle\  s    z Base.test_engine_reference_cyclec          
   C   sN   | j  }tjtdd |d d d f }W d Q R X t|tjsJtt|d S )NF)Zcheck_stacklevel)	r   rC   Zassert_produces_warningFutureWarningrA   rO   rX   r*   r1   )r   r.   resr   r   r   test_getitem_2d_deprecatedc  s    zBase.test_getitem_2d_deprecatedc             C   sj   | j  }d}tjt|d g |k W d Q R X djdddddg}tjt|d i |jk W d Q R X d S )	Nzunhashable type: 'list')r   |zunhashable type: 'dict'zmust be real number, not dictzan integer is requiredz\{\}z6pandas\._libs\.interval\.IntervalTree' is not iterable)r   r    r!   r"   r   ry   )r   r.   r$   r   r   r   &test_contains_requires_hashable_raisesk  s    z+Base.test_contains_requires_hashable_raisesc             C   sp   | j  }|j|d  |j }|j|jks.t|j|jk	s>tx,|jj D ]\}}|j| |ksJt|qJW d S )Nr   )r   r{   rj   _cacher*   items)r   r.   rj   keyvalr   r   r   test_copy_copies_cache~  s    zBase.test_copy_copies_cachec             C   sp   | j  }|j|d  |j }|j|jks.t|j|jk	s>tx,|jj D ]\}}|j| |ksJt|qJW d S )Nr   )r   r{   Z_shallow_copyr   r*   r   )r   r.   Zshallow_copyr   r   r   r   r   test_shallow_copy_copies_cache  s    z#Base.test_shallow_copy_copies_cache)Br2   
__module____qualname____doc__r   r	   r#   r_   r   r%   r    markZparametrizer/   r4   r5   rG   rI   rL   rV   r\   rb   re   ri   rq   rw   r|   r   r   r   r   r   r   r   tuplerO   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   r   r   r   r       sx   

5
 	
 	@!'r   )"r   typingr   numpyrO   r    Zpandas._libsr   Zpandas.errorsr   Zpandas.core.dtypes.commonr   Zpandas.core.dtypes.dtypesr   ZpandasrB   r   r   r	   r
   r   r   r   r   r   r   r   r   Zpandas._testingZ_testingrC   Z pandas.core.indexes.datetimeliker   r   r   r   r   r   <module>   s   8
