U
    sVcJ                     @   s   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mZmZmZ d dlmZ d dlmZ ejdd Zejdd ZG d	d
 d
ZdS )    N)is_categorical_dtype)CategoricalCategoricalIndex	DataFrameIndexIntervalSeries	Timedelta	Timestamp)CategoricalDtypec                   C   s0   t dtjdddittdttdddd	S )
NA   int64dtypeaabbcacabBr   nameindexr   nparanger   listCDT r   r   J/tmp/pip-unpacked-wheel-xj8nt62q/pandas/tests/indexing/test_categorical.pydf   s     r   c                   C   s0   t dtjdddittdttdddd	S )
Nr   r   r   r   r   caber   r   r   r   r   r   r   r   df2!   s     r!   c                   @   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dd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zejd'd(d)d*gd+d,d-gd.d/d0gd1d2d3gfd4d5 ejD d6d5 ejD ejd(d7d0ged8ed(d9ed9d:ed:d;ge d<d(d(e d<d)d(e d<d*d(ge!d(d=e!d)d=e!d*d>gfd?d5 ej"D e#j$%d(d9d:d;gje#j&d@d*dAje#j'dBd*dCjfdDdE Z(dFdG Z)dHS )ITestCategoricalIndexc              	   C   s(  t td}|jd }ttddd|}|j|ks:ttdddd	git|d
}t	
|| | }d|jd< ttddd|}|j|ksttdddddddgit|d
}t	
|| tjtdd |jd  W 5 Q R X | }| }|jt|_d|jd< d|jd< t	
|| d S )Nr   aZaaar   r   r   r         r      r            z^'d'$matchd
   )r   r   locr   astyper   AssertionErrorr   r   tmassert_frame_equalcopypytestraisesKeyErrorr   object)selfr   r   resultZbidxexpectedZbidx2r!   r   r   r   test_loc_scalar,   s2    

 

z$TestCategoricalIndex.test_loc_scalarc              	   C   s   d|j d< | }d|j d< ttddd}tdddd	d
dddgit|d}t|| | }d|j d< tddd	d
ddtjgtjtjtjtjtjtjdgdt|d}t|| d S )Nr'   r#   r.   )r-   r   Zaabbcadr   r$   r   r(   r)   r*   g      $@r   )r-   C)r   r=   )	r/   r4   r   r   r   r2   r3   r   nan)r9   r   Zdf3Zbidx3Z	expected3Zdf4r   r   r   ,test_loc_setitem_with_expansion_non_categoryL   s(    

 
zATestCategoricalIndex.test_loc_setitem_with_expansion_non_categoryc              	   C   s(   t jtdd |jd  W 5 Q R X d S )Nz^1$r+   r%   r5   r6   r7   r/   )r9   r   r   r   r   $test_loc_getitem_scalar_non_categoryh   s    z9TestCategoricalIndex.test_loc_getitem_scalar_non_categoryc                 C   sP  t tddddg}|d d d }tjddddgtjd}t| | tdt	dd 
d	i}tj|jd
ddddgd|d< t dtd
dgddgdd}|jd }t|| tdt	dd
d	it	dd
d	d}tj|jd
ddddgd|d< |jdd }t|| t dtd
dgddgdd}|jd }t|| d S )Nr%   r(   r)   r*   r   valued   r   r      2   K   )ZbinsD   r.   r   r      r'   r   	      )r   r   r   arrayr   r2   Zassert_numpy_array_equalZ	__array__r   r   r0   pdZcutrC   r   ilocassert_series_equalr3   r/   )r9   catreverseexpr   r;   r:   r   r   r   test_slicingl   s&    

z!TestCategoricalIndex.test_slicingc                 C   s  t dddddddgdddgd}tddddd	d
dg}dddddddg}t||d|d}t ddgdddgd}tddg}ddg}t||d|d}t||dd}	tddgddgddd}
d}|jddd d f }t|| t|d jst	|jdd d f }t
||
 t|d ts&t	|jd d df }t
||	 t|jsTt	|jd }||kslt	|jddd d f }t|| t|d jst	|jdd d f }t
||
 t|d tst	|jd d df }t
||	 t|jst	|jd }||kst	|jd|jd f }||ks<t	|jd }||ksTt	|jd }||kslt	|jdg }||d dk }t|| ||d dk }t|| |jd }||kst	|jd }t
||
 t|d tst	|jtdd }t|| t|d js"t	|jddg }t|| t|d jsPt	|jd d df }t
||	 t|js~t	|jd d tddf }t|| t|d jst	|jd d ddgf }t|| t|d jst	d S )Nr#   cb
categorieshijklmnr%   r(   r)   r*   r&   r      catsvaluesr   rc   rJ   rd   r8   )r   r   r   r   )r(   r   )r\   rc   )r   r   r   r   rP   r2   r3   r   r   r1   rQ   
isinstancestrr/   columnsZiatatslice)r9   rc   idxrd   r   Zcats2Zidx2Zvalues2Zexp_dfZexp_colZexp_rowZexp_valZres_dfZres_rowZres_colZres_valZ	exp_fancyZ	res_fancyr   r   r   test_slicing_and_getting_ops   s    	 





z1TestCategoricalIndex.test_slicing_and_getting_opsc                 C   sz  t dddddddgdddgd}tddddd	d
dg}dddddddg}t||d|d}|jddd d f }tt ddgdddgdddgdddgd}t|| |jddd d f j}tddgddg}t|| |j	dddf }tt dddgdddgddddgdd}t|| |j	dd|j
dd f }tdt dddgdddgdidddgd}t|| d S )Nr#   rW   rV   rX   rZ   r[   r\   r]   r^   r_   r`   r%   r(   r)   r*   r&   rb   r   categoryr   rc   rd   rJ   r   )r   r   r   rP   r2   r3   Zdtypesr   rQ   r/   rg   )r9   rc   rj   rd   r   r:   r;   r   r   r   test_slicing_doc_examples   s>     z.TestCategoricalIndex.test_slicing_doc_examplesc                 C   s4   |j ddg }|jddddg }tj||dd d S )	NrV   r#   r*   r   r%   r&   TZcheck_index_type)r/   rP   r2   r3   )r9   r   r:   r;   r   r   r    test_loc_getitem_listlike_labels&  s    z5TestCategoricalIndex.test_loc_getitem_listlike_labelsc              	   C   s4   t jttdd |jdddg  W 5 Q R X d S )N['e'] not in indexr+   r#   rW   er5   r6   r7   reescaper/   r9   r!   r   r   r   )test_loc_getitem_listlike_unused_category,  s    z>TestCategoricalIndex.test_loc_getitem_listlike_unused_categoryc              	   C   s(   t jtdd |jd  W 5 Q R X d S )Nz^'e'$r+   rq   r@   ru   r   r   r   &test_loc_getitem_label_unused_category2  s    z;TestCategoricalIndex.test_loc_getitem_label_unused_categoryc              	   C   s2   t jttdd |jddg  W 5 Q R X d S )Nz['d'] not in indexr+   r#   r-   rr   ru   r   r   r   test_loc_getitem_non_category7  s    z2TestCategoricalIndex.test_loc_getitem_non_categoryc                 C   sd   |  }d|jd< |jdddg }ttdtddd}td	d
dddddgi|d}t|| d S )Nr'   rq   r#   rW   Zaaabber    r   )rY   r   r   r   r%   r&   r(   r)   r   )r4   r/   r   r   r   r2   r3   )r9   r!   r   r:   	exp_indexr;   r   r   r   0test_loc_setitem_expansion_label_unused_category<  s    
zETestCategoricalIndex.test_loc_setitem_expansion_label_unused_categoryc              	   C   s   t dddg}tdddgddd	gd
|d}|jddg }t ddg|jd}tddgddgd
|d}tj||dd |jdddg }t dddg|jd}tdddgdddgd
|d}tj||dd tjtt	
dd |jddg  W 5 Q R X d S )Nr#   rW   rV   r%   r(   r)   r*   r&   r   r   r   r   rX   Trn   ['x'] not in indexr+   x)r   r   r/   rY   r2   r3   r5   r6   r7   rs   rt   )r9   r   r   resry   rT   r   r   r   test_loc_listlike_dtypesE  s    z-TestCategoricalIndex.test_loc_listlike_dtypesc              	   C   s   t dddg}tdddgdddgd	|d
}|jddg }tdddgdddgd	t dddgd
}tj||dd |jdddg }tdddddgdddddgd	t dddddgd
}tj||dd tjtt	dd |jddg  W 5 Q R X d S )Nr#   rW   r%   r(   r)   r*   r&   r   r{   r   Trn   r|   r+   r}   )
r   r   r/   r2   r3   r5   r6   r7   rs   rt   r9   r   r   r~   rT   r   r   r   8test_loc_listlike_dtypes_duplicated_categories_and_codes\  s      zMTestCategoricalIndex.test_loc_listlike_dtypes_duplicated_categories_and_codesc              	   C   s  t ddddgtdd}tdddd	gd
dddgd|d}|jddg }tdddgd
ddgdt dddgtddd}tj||dd |jdddg }tdddddgd
dd
ddgdt dddddgtddd}tj||dd tjtt	
dd |jddg  W 5 Q R X d S )Nr#   rW   rV   abcderX   r%   r(   r)   r*   r&   r   ra   rM   r{   r   Trn   r|   r+   r}   )r   r   r   r/   r2   r3   r5   r6   r7   rs   rt   r   r   r   r   (test_loc_listlike_dtypes_unused_categorys  s     "z=TestCategoricalIndex.test_loc_listlike_dtypes_unused_categoryc              	   C   s   t ddddgtdd}tdddd	gd
dddgd|d}tjtdd |jd  W 5 Q R X tjttdd |jddg  W 5 Q R X d S )Nr#   rW   rV   r   rX   r%   r(   r)   r*   r&   r   ra   rM   r{   r   rq   r+   rp   )	r   r   r   r5   r6   r7   r/   rs   rt   )r9   r   r   r   r   r   9test_loc_getitem_listlike_unused_category_raises_keyerror  s    "zNTestCategoricalIndex.test_loc_getitem_listlike_unused_category_raises_keyerrorc                 C   s`  t tjddtdtdd}| }t|j|_t|j|_t	|j
dd d f |jdd}t|j
dd d f | t	|j
d d df |jdd}t|j
d d df | ttddd	d
gd}t |j
dd	gd d f |j|d}t|j
dd	gd d f | ttddddgd}t |j
d d ddgf |j|d}t|j
d d ddgf | d S )Nr)   ABCZXYZr   rg   r   rJ   XZABr   r=   rX   rg   r   ZXYYZ)r   r   randomrandnr   r4   r   r   rg   r   r/   r2   rQ   r3   )r9   r   cdfexpectry   exp_columnsr   r   r   test_ix_categorical_index  s     ""z.TestCategoricalIndex.test_ix_categorical_indexc                 C   sl  t tjddtdtdd}| }t|j|_t|j|_ttdddgd}t |j	dd d f |j|d	}t
|j	dd d f | ttd
ddgd}t |j	d d df |j|d}t
|j	d d df | t |j	ddgd d f |jttdd	}t
|j	ddgd d f | t |j	d d ddgf |jttdd}t
|j	d d ddgf | d S )Nr)   ZABAZXYXr   ZAAr   r   rX   r   ZXXr   r   ZAABZXXY)r   r   r   r   r   r4   r   r   rg   r/   r2   r3   )r9   r   r   ry   r   r   r   r   r   $test_ix_categorical_index_non_unique  s,     

z9TestCategoricalIndex.test_ix_categorical_index_non_uniquec              	   C   sZ   d}t jt|d |jdd  W 5 Q R X |jdd }|jddd	g }t|| d S )
NzRcannot do slice indexing on CategoricalIndex with these indexers \[1\] of type intr+   r%   r&   rW   rV   r(   r)   r*   )r5   r6   	TypeErrorr/   rP   r2   r3   )r9   r   msgr:   r;   r   r   r   test_loc_slice  s    z#TestCategoricalIndex.test_loc_slicec                 C   s|   t ddgddgddggtddd	gd
}|d }|jd dksBt|jd dksTt|jd dksft|jd dksxtd S )Nr%   r(   r)   r*   r&   r   r   r   r=   r   r   )r   r%   )r   r   r/   r1   rh   )r9   r   sr   r   r   &test_loc_and_at_with_categorical_index  s     z;TestCategoricalIndex.test_loc_and_at_with_categorical_index
idx_valuesr%   r(   r)   rB         ?      @      @g      g      g      c                 c   s"   | ]}t jd ddg|dV  qdS r%   r(   r)   r   Nr   rN   .0r   r   r   r   	<genexpr>  s     zTestCategoricalIndex.<genexpr>c                 c   s"   | ]}t jd ddg|dV  qdS )r   r   r   r   Nr   )r   Zdtypr   r   r   r     s     rW   r   r*   r   rL   i  r-   rH   c                 c   s"   | ]}t jd ddg|dV  qdS r   )rO   rN   r   r   r   r   r     s     z
2019-01-01)periodsZ1d)startr   c                 C   s  t ||d}tddddgi|d}t|d |d }|j|d  }tdgdg|d d	}t|| |j|d d
  }tddg|d d
 dgd}t|| |j| }tddg|d d
 dgd}t|| | }d|j|d < tddddgi|d}t|| | }ddg|j|d d
 df< tddddgi|d}t|| | }ddg|j|df< tddddgi|d}t|| d S )N)orderedr   ZfoobarZbazr   r   r%   rJ   r(   r   ZquxZqux2)	r   r   ri   r/   r   r2   rQ   r3   r4   )r9   r   r   Zcat_idxr   slr:   r;   r   r   r   +test_loc_getitem_with_non_string_categories  s0    
z@TestCategoricalIndex.test_loc_getitem_with_non_string_categoriesc                 C   s   t ddtjg}ttd|d}|tj dks2t|jtj dksFtt|}|jtjdf dksft|jtj d dks~td S )Nr   r   r)   r   r(   r   )r   r   r>   r   ranger1   r/   r   )r9   ciZserr   r   r   r   !test_getitem_categorical_with_nan$  s    z6TestCategoricalIndex.test_getitem_categorical_with_nanN)*__name__
__module____qualname__r<   r?   rA   rU   rk   rm   ro   rv   rw   rx   rz   r   r   r   r   r   r   r   r   r5   markZparametrizer2   ZALL_INT_NUMPY_DTYPESZFLOAT_NUMPY_DTYPESr   rN   r8   r   r
   r	   ZALL_INT_EA_DTYPESrO   ZIntervalIndexZfrom_breaksZ
date_rangeZtimedelta_ranger   r   r   r   r   r   r"   +   sR    z'	 
'r"   )rs   Znumpyr   r5   Zpandas.core.dtypes.commonr   ZpandasrO   r   r   r   r   r   r   r	   r
   Zpandas._testingZ_testingr2   Zpandas.api.typesr   r   Zfixturer   r!   r"   r   r   r   r   <module>   s   (

	
	