3
Ud.d                 @   sH  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m	Z	 d dl
Zd dl
mZmZmZmZ d dlj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"j#d$d%d%gd%d&gd%gd&gd%d&ggd'd'gd'd(gd'gd(gddd%gd&fd%d&gfd'ddd'gd(fdd&gd%dgdd(gdddgd)d* Z$ej"j#d+d%d&d,gd'd(gfg d'd(gfd%d&d,gg fgd-d. Z%d/d0 Z&d1d2 Z'd3d4 Z(d5d6 Z)d7d8 Z*d9d: Z+d;d< Z,d=d> Z-d?d@ Z.ej"j#dAg g fdBdCdDgg fg d'd(dEgfgdFdG Z/ej"j#dHe0e1dIdJdK Z2ej"j#dLd%d%gd%d&gd%gd&gd'd'gd'd(gd'gd(ggdMdN Z3dOdP Z4ej"j#dQdRdSgej"j#dTdUdV dWdV dXdV gdYdZ Z5d[d\ Z6d]d^ Z7ej"j#d_ejd%d&d,gdBd`ejd'd(gdCd`dBdCgfejd%d&d,gdBd`d'd(gdBdgfd%d&d,gd'd(gdfgdadb Z8dcdd Z9dedf Z:dgdh Z;didj Z<ej"j#dkejd%d&d,dIgd%d&d,dIgd%d&gd,dIgdldmggejd%d&d,dIgej=d%d&gd,dIgdldmggdngdodp Z>dqdr Z?ej"j#dsdddgfdudwgdudwgfgdxdy Z@ej"j#dzdd'd(dEgd}fgd~d ZAdd ZBdd ZCdd ZDdd ZEdS )    )datedatetimeN)	Timestamp)'construct_1d_object_array_from_listlike)Index
MultiIndexSeries
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| 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   `/var/www/html/virt/lib64/python3.6/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 d Q R X d} t jt| d tg d W d Q R X t jt| d tg d W d 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 d 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 d Q R X t jt|d	 |j|d W d Q R X d S )Nr   r   onetwor   r
   r   z(MultiIndex\.name must be a hashable type)r   )r   r   r   ZfoorZbarr)r   )r
   r   )r    r!   r#   r   renameZ	set_names)r   r   r   r$   miZrenamedr   r   r   "test_constructor_nonhashable_names&   s    r*   c          /   C   s  t jdgt jdgt jdgg}dg}d}tjt|d t||d W d 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 d Q R X tjt|d* tdgdggddddgddggd W d Q R X tjt|d | j jdgdgg W d Q R X tjt|d$ | j jddddgddgg W d Q R X | j 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 d Q R X d S )Nr   r   r   az+Length of levels and codes must be the same)r   )r   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_levels>   s&    "2. ("r5   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j| | 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j| | t ddddd	ggddddddggdjtjdtjdd gg} tj| | t tjdtjdd ggddddddggdjddddddgg} tj| | d S )N   r   r   r   r   r-   )r   r   s   r9   r9   r9   r9   r9   r9   r9   r9   r9   r9   r9   )	r   r/   nanpdNaTr   r   r2   r3   )r   r   r   r   r   test_na_levelsd   s$    (((($(r=   c              C   s   t jdddg} t j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 )Nr+   r,   cr   r   r   T)r   r   r1      ZPANDA)r/   r0   r   r   r   r   )r   r   valr)   r   r   r   test_copy_in_constructor   s    rA   c             C   s   dd t | j| jD }tj|| jd}tj||  tjtj	t
dgddgg}|jd jtt
dgsnt|jd jtddgstd S )	Nc             S   s    g | ]\}}t j|j|qS r   )r/   asarraytake).0levlevel_codesr   r   r   
<listcomp>   s   z$test_from_arrays.<locals>.<listcomp>)r   Z20130101r+   r,   r   r   )zipr   r   r   from_arraysr   r   r   r;   r<   r   equalsr   r   )r4   arraysr   r   r   r   test_from_arrays   s    rL   c          	   C   sd   dd t | j| jD }tjt|| jd}tj||  d}t	j
t|d tjd W d Q R X d S )Nc             S   s    g | ]\}}t j|j|qS r   )r/   rB   rC   )rD   rE   rF   r   r   r   rG      s   z-test_from_arrays_iterator.<locals>.<listcomp>)r   z/Input must be a list / sequence of array-likes.)r   r   )rH   r   r   r   rI   iterr   r   r   r    r!   r#   )r4   rK   r   r$   r   r   r   test_from_arrays_iterator   s    rN   c             C   s<   t dd t| j| jD }tj|| jd}tj||  d S )Nc             s   s&   | ]\}}t tj|j|V  qd S )N)tupler/   rB   rC   )rD   rE   rF   r   r   r   	<genexpr>   s   z*test_from_arrays_tuples.<locals>.<genexpr>)r   )	rO   rH   r   r   r   rI   r   r   r   )r4   rK   r   r   r   r   test_from_arrays_tuples   s
    rQ   c              C   s   t jddddd} t jddddd}t jj| |g}tj|jd|  tj|jd	| t jjt j| t j|g}tj|jd|  tj|jd	| tj|| d S )
Nz2015-01-01 10:00Dr   z
US/Eastern)freqperiodstzHz
Asia/Tokyor   r   )r;   r	   r   rI   r   r   get_level_valuesr   )idx1idx2r   result2r   r   r   (test_from_arrays_index_series_datetimetz   s    r[   c              C   s   t jdddd} t jdddd}t jj| |g}tj|jd|  tj|jd| t jjt j| t j|g}tj|jd|  tj|jd| tj|| d S )	Nz1 daysrR   r   )rS   rT   z2 hoursrV   r   r   )r;   timedelta_ranger   rI   r   r   rW   r   )rX   rY   r   rZ   r   r   r   'test_from_arrays_index_series_timedelta   s    r]   c              C   s   t jdddd} t jdddd}t jj| |g}tj|jd|  tj|jd| t jjt j| t j|g}tj|jd|  tj|jd| tj|| d S )	Nz
2011-01-01rR   r   )rS   rT   z
2015-01-01rV   r   r   )r;   period_ranger   rI   r   r   rW   r   )rX   rY   r   rZ   r   r   r   $test_from_arrays_index_series_period   s    r_   c              C   s"  t jddddd} t jdddd}t jdddd}t jd	ddd}t jj| |||g}tj|jd
|  tj|jd| tj|jd| tj|jd| t jjt j	| t j	|t j	|t j	|g}tj|jd
|  tj|jd| tj|jd| tj|jd| tj|| d S )Nz2015-01-01 10:00rR   r   z
US/Eastern)rS   rT   rU   rV   )rS   rT   z1 daysz
2011-01-01r   r   r   )
r;   r	   r\   r^   r   rI   r   r   rW   r   )rX   rY   Zidx3Zidx4r   rZ   r   r   r   )test_from_arrays_index_datetimelike_mixed   s     &r`   c              C   s   t jtdtddd} t jtdtddd}t jj| |g}tj|jd|  tj|jd| t jjt j| t j|g}tj|jd|  tj|jd| t jj| j	|j	g}tj|jd|  tj|jd| d S )NabcaabbacF)
categoriesorderedTr   r   )
r;   CategoricalIndexlistr   rI   r   r   rW   r   values)rX   rY   r   rZ   Zresult3r   r   r   )test_from_arrays_index_series_categorical  s    rh   c              C   s   d} t jt| d tjg d W d Q R X tjg gdgd}t|tsJttg dd}tj	|j
d | |jdgksxtxZdD ]R}g g| }td
d | }tj||d}tg g| g g| |d}tj	|| q~W d S )Nz)Must pass non-zero number of levels/codes)r   )rK   A)rK   r   )r   r   r   r   ABC)r   r   r   )r   r   )r    r!   r"   r   rI   r   r   r   r   r   r   r   rf   )r$   r   r   NrK   r   r   r   r   test_from_arrays_empty  s    

rl   invalid_sequence_of_arraysr   r   r+   r,   c          
   C   s.   d}t jt|d tj| d W d Q R X d S )Nz.Input must be a list / sequence of array-likes)r   )rK   )r    r!   r#   r   rI   )rm   r$   r   r   r   test_from_arrays_invalid_input)  s    rn   z
idx1, idx2r   c          
   C   s0   d}t jt|d tj| |g W d Q R X d S )Nz ^all arrays must be same length$)r   )r    r!   r"   r   rI   )rX   rY   r$   r   r   r   "test_from_arrays_different_lengthsK  s    ro   c              C   sx   t jdddgdd} t j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j|| d S )Nr   r   r   r
   )r   r+   r,   r>   r   )r   r   )r   r   r   )r;   r   r   rI   r   r   )r+   r,   r   r   r   r   r   $test_from_arrays_respects_none_namesU  s    ,rp   c              C   st   d} t jt| d tjg  W d 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j|| d S )Nz-Cannot infer number of levels from empty list)r   r   r   r   r-   r   r+   r,   )r   r   r   )r   r   r   r   r-   )rq   rr   )r    r!   r#   r   from_tuplesr   r   )r$   r   r   r   r   r   test_from_tuplese  s    (rt   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j||  d
}tjt|d t jd W d Q R X d S )Nr   r   r   r-   r   r+   r,   )r   r   r   )r   z/Input must be a list / sequence of tuple-likes.)r   )r   rs   rH   r   r   r    r!   r#   )r   r   r$   r   r   r   test_from_tuples_iterators  s    ( ru   c              C   s8   t jg ddgd} t jg g gddgd}tj| | d S )Nr+   r,   )r   )rK   r   )r   rs   rI   r   r   )r   r   r   r   r   test_from_tuples_empty  s    rv   c             C   s"   t j| }|j| jkj std S )N)r   rs   rg   allr   )r4   r   r   r   r   test_from_tuples_index_values  s    
rx   c              C   sb   dd	d
g} d}t jt|d tj| dd W d Q R X t jt|d tj| dd W d Q R X d S )Nr   r   z*Names should be list-like for a MultiIndex)r   abc)r   r+   )r   r   r   )r   r   r   )r   r   r   )r    r!   r"   r;   r   )lir$   r   r   r   test_tuples_with_name_string  s    
r{   c              C   sh   t jdddgdddggdddgdjddg} t jjddgdd	}t jddgdg|d
}tj| | d S )Nr   r   r-   r   r+   r,   r>   )columns)r   )r|   index)r   r   )r   r   r   r   )r-   r~   )r+   r,   )r;   	DataFrameZ	set_indexr   rs   r   Zassert_frame_equal)r   r4   r   r   r   r   !test_from_tuples_with_tuple_label  s     
r   c           	   C   s,   d} t jt| d tjg  W d Q R X d S )Nz)Must pass non-zero number of levels/codes)r   )r    r!   r"   r   from_product)r$   r   r   r   #test_from_product_empty_zero_levels  s    r   c              C   sF   t jg gdgd} tjg dd}tj| jd | | jdgksBtd S )Nri   )r   )r   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   r>   c             C   s@   ddg}t j| |g|d}t | |gg g g|d}tj|| d S )Nri   B)r   )r   r   r   )r   r   r   r   )r   secondr   r   r   r   r   r   "test_from_product_empty_two_levels  s    r   rk   r-   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j|| d S )Nri   r   C)r   )r   r   r   )rf   ranger   r   r   r   )rk   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 d Q R X d S )NzDInput must be a list / sequence of iterables|Input must be list-like)r   )	iterables)r    r!   r#   r   r   )r   r$   r   r   r   test_from_product_invalid_input  s    r   c              C   sj   t ddd} tjjddg| g}tdtjdfdtjdfdtjdfdtjdfg}tj|j| d S )Nz
2000-01-01r   )rT   r   z
2000-01-02)	r	   r;   r   r   r   r   r   Zassert_numpy_array_equalrg   )Zdt_indexr)   Zetalonr   r   r   test_from_product_datetimeindex  s    r   rd   FTfc             C   s   | S )Nr   )xr   r   r   <lambda>  s    r   c             C   s
   t j| S )N)r;   r   )r   r   r   r   r     s    c             C   s   | j S )N)rg   )r   r   r   r   r     s    c             C   sj   ddg}t jtdtd| d}t jtdtd td| d}t jj|||g}tj|jd| d S )Nr
   r   ra   rb   )rc   rd   r   )r;   re   rf   r   r   r   r   rW   )rd   r   r   r4   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j|| d S )Nr
   r   buzr+   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   rs   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j|| d
}tjt|d t jd W d Q R X d S )Nr
   r   r   r+   r,   r>   r   r   )r   z-Input must be a list / sequence of iterables.)r   r   )r
   r+   )r
   r,   )r
   r>   )r   r+   )r   r,   )r   r>   )r   r+   )r   r,   )r   r>   )	r   rs   r   rM   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_names)r   c          	   C   sV   t j| |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j|| d S )Nr   r   r   r+   r,   r   )r   r   r   )r   r   r   r   )r+   r,   Zexpected_namesr   r   r   r   r   test_from_product_infer_names"  s    r   c           	   C   s   t jdddgdd} t j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j|| d S )Nr   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   %test_from_product_respects_none_names9  s    r   c              C   sN   t jtd} ddg}tj| |g}| jdd tj| |g}tj|| d S )Nr   r+   r,   F)write)r/   r0   r   r   r   Zsetflagsr   r   )r+   r,   r   r   r   r   r   test_from_product_readonlyG  s    r   c          	   C   s~   | }ddg|_ tj|}ttddddddgdd}tj|| tj|d	d
}ttddddddgddd	d
}tj|| d S )Nr
   r   r&   r'   r   r   object)dtyperi   )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   )r4   r}   r   r   r   r   r   test_create_index_existing_nameR  s2    


r   c              C   sb   t jddgddgddgddggddgd} t jjddd	d
gddgd}t jj| }tj|| d S )Nr+   r,   L1L2)r|   )r   )r+   r+   )r+   r,   )r,   r+   )r,   r,   )r;   r   r   rs   
from_framer   r   )dfr   r   r   r   r   test_from_frame}  s    &r   	non_framer8         c          	   C   s*   t jtdd tjj|  W d Q R X d S )NzInput must be a DataFrame)r   )r    r!   r#   r;   r   r   )r   r   r   r   test_from_frame_error  s    r   c           
      s   t jt j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j }t jjt j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 jj|   fddt	 j
D }tj|  ||kstd S )NZ19910905r   z
US/Eastern)rT   rU   r   r   r+   r,   r>   T)rd   r   yz)datesr+   r,   r>   r   )r   c                s   i | ]\}} j | j|qS r   )r   r   )rD   ir   )r)   r   r   
<dictcomp>  s    z2test_from_frame_dtype_fidelity.<locals>.<dictcomp>)r;   r   r	   ZCategoricalZdtypesto_dictr   rI   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_outr   r   r   r   c             C   sT   t jddgddgddgddggt jjd	d
gd}t jj|| d}|j|ksPtd S )Nr+   r,   r   r   r   r   )r|   )r   )r   r   )r   r   )r;   r   r   rs   r   r   r   )Znames_inZ	names_outr   r)   r   r   r   test_from_frame_valid_names  s
    r   znames,expected_error_msg	bad_input*Names should be list-like for a MultiIndexz9Length 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 jjd
dgd}tjt|d t jj|| d	 W d Q R X d S )Nr+   r,   r   r   r   r   )r|   )r   )r   )r   r   )r   r   )r;   r   r   rs   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j| | d S )Nr+   r,   )r   r   r   )rK   r   )r   rI   r   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 d 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 d 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    (2r   c        
      C   s   t jdddgd dd} t jddd	d
d}tj| |g}t jdddgdd}tj|jd | tj|jd | tj	 }t
j	 }tj	 }xbtj|||g|||gD ]F\}}tj|g|gg}	t|	jd t jstt|	jd t jstqW d S )Nz2013-04-01 9:00z2013-04-02 9:00z2013-04-03 9:00r   z
Asia/Tokyo)rU   z
2010/01/01r   Mz
US/Eastern)rT   rS   rU   r   r   )r;   DatetimeIndexr	   r   rI   r   r   r   r   todayr   r   	itertoolsproductr   r   r   )
rX   rY   r4   Z	expected1Zdate1Zdate2Zdate3Zd1Zd2r}   r   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j| |g}|jddgksHttj|jd
 |  tj|jd | tjt	| t	|g}|jddgksttj|jd
 |  tj|jd | d S )Nz2013/01/01 09:00z2013/01/02 09:00Zdt1z
US/Pacific)r   rU   z2014/01/01 09:00z2014/01/02 09:00Zdt2z
Asia/Tokyor   r   )
r;   r   r   rI   r   r   r   r   r   r   )r}   r|   r   r   r   r   test_constructor_with_tz  s    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   )Fr   r   r   Znumpyr/   r    Zpandas._libs.tslibr   Zpandas.core.dtypes.castr   Zpandasr;   r   r   r   r	   Zpandas._testingZ_testingr   r   r%   r*   r5   r=   rA   rL   rN   rQ   r[   r]   r_   r`   rh   rl   markZparametrizern   ro   rp   rt   ru   rv   rx   r{   r   r   r   r   rf   r   r   r   r   r   r   r   r   r   r   r   r   r0   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   

& 	6	.	
8*  +
	(