U
    +ifd                     @   s  d dl mZm Z  d dlZd dlZd dlZd dlmZ d dlZ	d dlm
Z
mZmZmZmZ d dlm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e	jdddde	jddddfeddddd edddd!d fe	jd"ddde	jd#dddfgd$d% Zd&d' Z d(d) Z!d*d+ Z"ejd,d-d-gd-d.gd-gd.gd-d.ggd/d/gd/d0gd/gd0gd1d2d-gd.fd-d.gfd/d3d4d/gd0fd1d.gd-d5gd3d0gd6d7d8gd9d: Z#ejd;d-d.dgd/d0gfg d/d0gfd-d.dgg fgd<d= Z$d>d? Z%d@dA Z&dBdC Z'dDdE Z(dFdG Z)dHdI Z*dJdK Z+dLdM Z,dNdO Z-ejdPg g fdQdRdSgg fg d/d0dTgfgdUdV Z.ejdWe/e0dXdYdZ Z1ejd[d-d-gd-d.gd-gd.gd/d/gd/d0gd/gd0ggd\d] Z2d^d_ Z3d`da Z4ejdbdcddgejdedfdg dhdg didg gdjdk Z5dldm Z6dndo Z7ejdped-d.dgdQdqed/d0gdRdqdQdRgfed-d.dgdQdqd/d0gdQdgfd-d.dgd/d0gdfgdrds Z8dtdu Z9dvdw Z:dxdy Z;dzd{ Z<ejd|ed-d.ddXgd-d.ddXgd-d.gddXgd}d~gge
d-d.ddXge=d-d.gddXgd}d~ggdgdd Z>dd Z?ejddddgfddgddgfgdd Z@ejddd/d0dTgdfgdd ZAdd ZBdd ZCdd ZDdd ZEdd ZFdS )    )datedatetimeN)'construct_1d_object_array_from_listlike)Index
MultiIndexSeries	Timestamp
date_rangec                  C   sl   t ddddggddddggd	gd
} t| t s2ttddddgd	d}t| jd | | jd	gkshtd S )Nfoobarbazquxr            firstlevelscodesnamesname)r   
isinstanceAssertionErrorr   tmassert_index_equalr   r   resultexpected r   _/home/mars/bis/venv/lib/python3.8/site-packages/pandas/tests/indexes/multi/test_constructors.pytest_constructor_single_level   s      r!   c               	   C   sz   d} t jt| d tg g d W 5 Q R X d} t jt| d tg d W 5 Q R X t jt| d tg d W 5 Q R X d S )Nznon-zero number of levels/codesmatchr   r   zMust pass both levels and codesr   )r   )pytestraises
ValueErrorr   	TypeErrormsgr   r   r    test_constructor_no_levels!   s    r,   c               	   C   s   ddgddgg} ddddgddddgg}dgdgf}d}t jt|d	 t| ||d
 W 5 Q R X tddgddggddddgddddggdd
}dg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 )Nr   r   onetwor   r
   r   z(MultiIndex\.name must be a hashable typer"   r   )r
   r   ZfoorZbarrr   )r&   r'   r)   r   renameZ	set_names)r   r   r   r+   miZrenamedr   r   r    "test_constructor_nonhashable_names-   s     r2   c              	   C   s  t dgt dgt dgg}dg}d}tjt|d t||d W 5 Q R X d}d	}d
}tjt|d. tdgdggddddgddddggd W 5 Q R X tjt|d* tdgdggddddgddggd W 5 Q R X tjt|d |  dgdgg W 5 Q R X tjt|d$ |  ddddgddgg W 5 Q R X |  jddddgddggdd tjt|d& tdgdggddgddggd W 5 Q R X d S )Nr   r   r   az+Length of levels and codes must be the samer"   r$   zbOn level 0, code max \(3\) >= length of level \(1\)\. NOTE: this index is in an inconsistent statezUnequal code lengths: \[4, 2\]z"On level 0, code value \(-2\) < -1br      F)r   Zverify_integrity)	nparrayr&   r'   r(   r   copy
set_levels	set_codes)idxr   r   r+   Zlength_errorZlabel_errorZcode_value_errorr   r   r    (test_constructor_mismatched_codes_levelsE   s(    "2. ("r=   c                  C   sV  t tjd tjddggddddddggd} t tjd tjddggddddddggd}t| | t tjd	tjdd ggddddddggd} t tjd	tjdd ggddddddggd}t| | t ddddd
ggddddddggdtjd	tjdd gg} t| | t tjd	tjdd ggddddddggdddddddgg} t| | d S )N   r   r   r   r   r5   r$   s   )	r   r7   nanpdNaTr   r   r:   r;   r   r   r   r    test_na_levelsk   s@          rE   c               	   C   s   t dddg} t dddddddg}|d }t| | g||gdd}|jd d |ks\td	|d< |jd d |kszt| d }d
| d< |jd d |kstd S )Nr3   r4   cr   r   r   T)r   r   r9      ZPANDA)r7   r8   r   r   r   r   )r   r   valr1   r   r   r    test_copy_in_constructor   s    rI   c                 C   s   dd t | j| jD }tj|| jd}t||  ttj	t
dgddgg}|jd tt
dgsnt|jd tddgstd S )	Nc                 S   s    g | ]\}}t ||qS r   r7   asarraytake.0levZlevel_codesr   r   r    
<listcomp>   s   z$test_from_arrays.<locals>.<listcomp>r/   Z20130101r3   r4   r   r   )zipr   r   r   from_arraysr   r   r   rC   rD   r   equalsr   r   r<   arraysr   r   r   r    test_from_arrays   s    rV   c              	   C   sd   dd t | j| jD }tjt|| jd}t||  d}t	j
t|d td W 5 Q R X d S )Nc                 S   s    g | ]\}}t ||qS r   rJ   rM   r   r   r    rP      s   z-test_from_arrays_iterator.<locals>.<listcomp>r/   z/Input must be a list / sequence of array-likes.r"   r   )rQ   r   r   r   rR   iterr   r   r   r&   r'   r)   )r<   rU   r   r+   r   r   r    test_from_arrays_iterator   s    rX   c                 C   s<   t dd t| j| jD }tj|| jd}t||  d S )Nc                 s   s&   | ]\}}t t||V  qd S N)tupler7   rK   rL   rM   r   r   r    	<genexpr>   s   z*test_from_arrays_tuples.<locals>.<genexpr>r/   )	rZ   rQ   r   r   r   rR   r   r   r   rT   r   r   r    test_from_arrays_tuples   s
    r\   )idx1idx2
2011-01-01Dr   freqperiodsz
2015-01-01H2015-01-01 10:00
US/Easternrb   rc   tz
Asia/Tokyo1 daysz2 hoursc                 C   s|   t | |g}t|d|  t|d| t t| t|g}t|d|  t|d| t|| d S )Nr   r   )r   rR   r   r   get_level_valuesr   )r]   r^   r   result2r   r   r    =test_from_arrays_index_series_period_datetimetz_and_timedelta   s    rm   c                  C   s  t ddddd} t dddd}tjdddd}tjd	ddd}t| |||g}t|d
|  t|d| t|d| t|d| tt	| t	|t	|t	|g}t|d
|  t|d| t|d| t|d| t|| d S )Nre   r`   r   rf   rg   rd   ra   rj   r_   r   r   r   )
r	   rC   timedelta_rangeperiod_ranger   rR   r   r   rk   r   )r]   r^   Zidx3Zidx4r   rl   r   r   r    )test_from_arrays_index_datetimelike_mixed   s"    rp   c                  C   s   t jtdtddd} t jtdtddd}t| |g}t|d|  t|d| tt| t|g}t|d|  t|d| t| j	|j	g}t|d|  t|d| d S )NabcaabbacF
categoriesorderedTr   r   )
rC   CategoricalIndexlistr   rR   r   r   rk   r   values)r]   r^   r   rl   Zresult3r   r   r    )test_from_arrays_index_series_categorical   s    ry   c               	   C   s   d} t jt| d tjg d W 5 Q R X tjg gdgd}t|tsJttg dd}t	|j
d | |jdgksxtdD ]R}g g| }td	d | }tj||d}tg g| g g| |d
}t	|| q|d S )N)Must pass non-zero number of levels/codesr"   rU   ArU   r   r   r   )r   r   ABCr   )r&   r'   r(   r   rR   r   r   r   r   r   r   r   rw   )r+   r   r   NrU   r   r   r   r    test_from_arrays_empty  s    
r   invalid_sequence_of_arraysr   r   r3   r4   r   r   r   r3   r3   r4   r   )r   r   )r   r   )r   r4   c              	   C   s.   d}t jt|d tj| d W 5 Q R X d S )Nz.Input must be a list / sequence of array-likesr"   r{   )r&   r'   r)   r   rR   )r   r+   r   r   r    test_from_arrays_invalid_input#  s    r   z
idx1, idx2c              	   C   s0   d}t jt|d t| |g W 5 Q R X d S )Nz ^all arrays must be same length$r"   )r&   r'   r(   r   rR   )r]   r^   r+   r   r   r    "test_from_arrays_different_lengthsE  s    r   c                  C   st   t dddgdd} t dddgd	d}tj| |gd d
}tdddgdddggdddgdddggd d}t|| d S )Nr   r   r   r
   r   r3   r4   rF   r   r/   r   r   )r   r   rR   r   r   r3   r4   r   r   r   r   r    $test_from_arrays_respects_none_namesO  s      r   c               	   C   st   d} t jt| d tg  W 5 Q R X tddgddggddgddggdd	gd
}tjddd	gd}t|| d S )Nz-Cannot infer number of levels from empty listr"   r   r   r   r5   r   r3   r4   r   )r   )r   r5   r/   )r&   r'   r)   r   from_tuplesr   r   )r+   r   r   r   r   r    test_from_tuples_  s      r   c               	   C   s   t ddgddggddgddggddgd} t jtddgddgddgd	}t||  d
}tjt|d t d W 5 Q R X d S )Nr   r   r   r5   r   r3   r4   r   r/   z/Input must be a list / sequence of tuple-likes.r"   )r   r   rQ   r   r   r&   r'   r)   )r   r   r+   r   r   r    test_from_tuples_iteratorm  s       r   c                  C   s8   t jg ddgd} t jg g gddgd}t| | d S )Nr3   r4   r/   r}   )r   r   rR   r   r   r   r   r   r    test_from_tuples_empty}  s    r   c                 C   s"   t | }|j| jk std S rY   )r   r   rx   allr   )r<   r   r   r   r    test_from_tuples_index_values  s    
r   c               	   C   s^   dddg} d}t jt|d t| dd W 5 Q R X t jt|d t| dd W 5 Q R X d S )	N)r   r   r   )r   r   r   )r   r   r   *Names should be list-like for a MultiIndexr"   abcr   r3   )r&   r'   r(   r   )Zlir+   r   r   r    test_tuples_with_name_string  s    
r   c                  C   sf   t jdddgdddggdddgd	ddg} tjd
dgdd}t jddgdg|d}t| | d S )Nr   r   r5   r   r   r3   r4   rF   columns)r   r   )r5   r   r   r/   )r   index)rC   	DataFrameZ	set_indexr   r   r   Zassert_frame_equal)r   r<   r   r   r   r    !test_from_tuples_with_tuple_label  s     r   c               	   C   s,   d} t jt| d tg  W 5 Q R X d S )Nrz   r"   )r&   r'   r(   r   from_productr*   r   r   r    #test_from_product_empty_zero_levels  s    r   c                  C   sD   t jg gdgd} tg dd}t| jd | | jdgks@td S )Nr|   r/   r   r   )r   r   r   r   r   r   r   r   r   r   r   r    !test_from_product_empty_one_level  s    r   zfirst, secondr
   r   r   rF   c                 C   s@   ddg}t j| |g|d}t | |gg g g|d}t|| d S )Nr|   Br/   r   r   r   r   r   )r   secondr   r   r   r   r   r    "test_from_product_empty_two_levels  s    r   r   r5   c                 C   sT   dddg}t t| }tjg |g g|d}tg |g gg g g g|d}t|| d S )Nr|   r   Cr/   r   )rw   ranger   r   r   r   )r   r   Zlvl2r   r   r   r   r    $test_from_product_empty_three_levels  s
    
r   invalid_inputc              	   C   s.   d}t jt|d tj| d W 5 Q R X d S )NzDInput must be a list / sequence of iterables|Input must be list-liker"   )	iterables)r&   r'   r)   r   r   )r   r+   r   r   r    test_from_product_invalid_input  s    r   c                  C   s`   t ddd} tddg| g}tdtdfdtdfdtdfdtdfg}t|j| d S )Nz
2000-01-01r   )rc   r   z
2000-01-02)r	   r   r   r   r   r   Zassert_numpy_array_equalrx   )Zdt_indexr1   Zetalonr   r   r    test_from_product_datetimeindex  s    



r   c                  C   s<   t td} ddg}t| |g}tj|jd | dd d S )NrA   r3   r4   r   T)exact)r   r   r   r   r   r   Z_levels)rngotherr1   r   r   r    test_from_product_rangeindex  s    r   ru   FTfc                 C   s   | S rY   r   xr   r   r    <lambda>      r   c                 C   s   t | S rY   )r   r   r   r   r    r     r   c                 C   s   | j S rY   )rx   r   r   r   r    r     r   c                 C   sh   ddg}t jtdtd| d}t jtdtd td| d}t|||g}t|d| d S )Nr
   r   rq   rr   rs   r   )rC   rv   rw   r   r   r   r   rk   )ru   r   r   r<   r   r   r   r   r    *test_from_product_index_series_categorical  s      r   c               	   C   sb   dddg} dddg}ddg}t j| |g|d	}d
ddddddddg	}t j||d	}t|| d S )Nr
   r   buzr3   r4   rF   r   r   r/   r
   r3   r
   r4   r
   rF   r   r3   r   r4   r   rF   r   r3   r   r4   r   rF   )r   r   r   r   r   )r   r   r   r   tuplesr   r   r   r    test_from_product  s     

r   c               	   C   s   dddg} dddg}ddg}d	d
dddddddg	}t j||d}t jt| |g|d}t|| d}tjt|d t d W 5 Q R X d S )Nr
   r   r   r3   r4   rF   r   r   r   r   r   r   r   r   r   r   r   r/   z-Input must be a list / sequence of iterables.r"   r   )	r   r   r   rW   r   r   r&   r'   r)   )r   r   r   r   r   r   r+   r   r   r    test_from_product_iterator  s&    

r   za, b, expected_namesr   c              	   C   sV   t | |g}t dddgddggddddddgddddddgg|d}t|| d S )Nr   r   r   r3   r4   r   r   r   )r3   r4   Zexpected_namesr   r   r   r   r    test_from_product_infer_names$  s    r   c               	   C   s|   t dddgdd} t ddgdd}tj| |gd d	}tdddgddggd
d
ddddgd
dd
dd
dggd d}t|| d S )Nr   r   r   r
   r   r3   r4   r   r/   r   r   )r   r   r   r   r   r   r   r   r    %test_from_product_respects_none_names;  s    r   c                  C   sN   t td} ddg}t| |g}| jdd t| |g}t|| d S )Nr   r3   r4   F)write)r7   r8   r   r   r   Zsetflagsr   r   )r3   r4   r   r   r   r   r    test_from_product_readonlyI  s    r   c                 C   sz   | }ddg|_ t|}ttddddddgd	d
}t|| t|dd}ttddddddgd	d
dd}t|| d S )Nr
   r   )r
   r-   )r
   r.   )r   r-   )r   r.   )r   r-   )r   r.   object)dtyper|   r   )r   r   r   r   )r<   r   r   r   r   r   r    test_create_index_existing_nameT  s>    
r   c                  C   s^   t jddgddgddgddggddgd} tjdddd	gddgd
}t| }t|| d S )Nr3   r4   L1L2r   )r3   r3   r   )r4   r3   )r4   r4   r/   )rC   r   r   r   
from_framer   r   )dfr   r   r   r   r    test_from_frame  s     
 
r   	non_framerA         c              	   C   s(   t jtdd t|  W 5 Q R X d S )NzInput must be a DataFramer"   )r&   r'   r)   r   r   )r   r   r   r    test_from_frame_error  s    r   c                     s   t tddddddddddgt jddddd	d	gd
dddddddgd} | j }tjtddddddddddgt jddddd	d	gd
dddddddggdddd	gd}t|   fddt	 j
D }t|  ||kstd S )NZ19910905r   rf   )rc   rh   r   r   r3   r4   rF   T)ru   r   yz)datesr3   r4   rF   r   r/   c                    s   i | ]\}}| j | jqS r   )r   r   )rN   ir   r1   r   r    
<dictcomp>  s      z2test_from_frame_dtype_fidelity.<locals>.<dictcomp>)rC   r   r	   ZCategoricalZdtypesto_dictr   rR   r   	enumerater   r   r   r   )r   Zoriginal_dtypesZexpected_miZ	mi_dtypesr   r   r    test_from_frame_dtype_fidelity  s(    

	
r   znames_in,names_outr   r   r   r   r   r   c                 C   sP   t jddgddgddgddggtddgd}tj|| d}|j|ksLtd S )Nr3   r4   r   r   r   r/   )rC   r   r   r   r   r   r   )Znames_inZ	names_outr   r1   r   r   r    test_from_frame_valid_names  s    r   znames,expected_error_msg)Z	bad_inputr   z9Length of names must match number of levels in MultiIndexc              	   C   s\   t jddgddgddgddggtddgd}tjt|d tj|| d W 5 Q R X d S )Nr3   r4   r   r   r   r"   r/   )rC   r   r   r   r&   r'   r(   r   )r   Zexpected_error_msgr   r   r   r    test_from_frame_invalid_names  s    	r   c                  C   s@   t g g gg g gddgd} t jg g gddgd}t| | d S )Nr3   r4   r   r}   )r   rR   r   r   r   r   r   r    test_index_equal_empty_iterable  s    r   c               
   C   s   ddgdddgg} t | ddddddgddddddggdd tjtdd0 t | ddddddgddddddggdd W 5 Q R X tjtdd0 t | ddddddgddddddggdd W 5 Q R X d S )Nr   r   r   )r   r   Z	sortorderz%.* sortorder 2 with lexsort_depth 1.*r"   z%.* sortorder 1 with lexsort_depth 0.*)r   r&   r'   r(   r%   r   r   r    test_raise_invalid_sortorder  s$          r   c                  C   s6  t jdddgd dd} tddd	d
d}t| |g}t jdddgdd}t|jd | t|jd | t	d}t
 }t }t|||g|||gD ]F\}}t|g|gg}	t|	jd t jstt|	jd t jstqt }
t|
g|gg}	t|	jd t jrtt|	jd t js2td S )Nz2013-04-01 9:00z2013-04-02 9:00z2013-04-03 9:00r   ri   )rh   z
2010/01/01r   Mrf   )rc   rb   rh   r   r   today)rC   DatetimeIndexr	   r   rR   r   r   r   r7   Z
datetime64r   r   r   	itertoolsproductr   r   r   r   )r]   r^   r<   Z	expected1Zdate1Zdate2Zdate3Zd1Zd2r   Zdate4r   r   r    test_datetimeindex  s.      
 r   c                  C   s   t jddgddd} t jddgdd	d}t| |g}|jddgksHtt|jd
 |  t|jd | tt	| t	|g}|jddgkstt|jd
 |  t|jd | d S )Nz2013/01/01 09:00z2013/01/02 09:00Zdt1z
US/Pacific)r   rh   z2014/01/01 09:00z2014/01/02 09:00Zdt2ri   r   r   )
rC   r   r   rR   r   r   r   r   r   r   )r   r   r   r   r   r    test_constructor_with_tz  s$        r   c                  C   s   t  } | | g}t|}|jtks&tt|g}|jd }|jtksJtt	|g}|jd }|jtksntt
dd |D }|jd }|jtkstd S )Nr   c                 S   s   g | ]
}|fqS r   r   )rN   r   r   r   r    rP   ;  s     z9test_multiindex_inference_consistency.<locals>.<listcomp>)r   r   r   r   r   r   r   rR   r   r   r   )vZarrr<   r1   rO   r   r   r    %test_multiindex_inference_consistency)  s    


r   )Gr   r   r   numpyr7   r&   Zpandas.core.dtypes.castr   ZpandasrC   r   r   r   r   r	   Zpandas._testingZ_testingr   r!   r,   r2   r=   rE   rI   rV   rX   r\   markZparametrizero   rn   rm   rp   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   r   r    <module>   s  
& 

 (
  

	 *

+

 



 