U
    f/e1                     @   s   d dl 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 d dlmZ d dlm  mZ d dlmZ G dd deZG dd dZG dd	 d	Zejd
ddgdd Zejd
ddgdd Zejdd Zdd ZdS )    N)	CategoricalCategoricalIndexIndexIntervalIntervalIndexPeriodIndexSeries	Timedelta	Timestamp)TestCategoricalc                
   @   s  e Zd Zdd Zdd Zejdeddgeddgddgdgd	d
 Z	ejdeddgdddgdeddgdddgdeddgdgdeddgdgdgdd Z
ejdeddgeddgddgddeddgdddgddgdd Zdd Zdd ZdS )!TestCategoricalIndexingWithFactorc                 C   s   | j d dkst| j d dks$t| j dddg }t|jtjdddgtjd | j t| j dk }t|jtjdddgtjd d S )Nr   ac      dtype)	factorAssertionErrortmassert_numpy_array_equal_codesnparrayint8Zasarray)selfZsubf r   Q/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/arrays/categorical/test_indexing.pytest_getitem   s     z.TestCategoricalIndexingWithFactor.test_getitemc              	   C   s   | j  }d|d< |d dks"td|d< |d dks:t| j  }tjt|dd}d|d< d|d< d||< tddddddddgdd	}t|| d S )
Nbr   r   r   boolr   Tr   ordered)	r   copyr   r   zeroslenr   r   assert_categorical_equal)r   r   indexerexpectedr   r   r   test_setitem   s    

z.TestCategoricalIndexingWithFactor.test_setitemotherr    r   
categoriesc                 C   sR   t ddgddgd}tddg}|| ||< t ddgddgd}t|| d S )Nr   r    r,   TF)r   r   r   r   r'   )r   r+   targetmaskr)   r   r   r   test_setitem_same_but_unordered2   s
    zATestCategoricalIndexingWithFactor.test_setitem_same_but_unorderedr   c              	   C   sP   t ddgddgd}tddg}d}tjt|d || ||< W 5 Q R X d S )Nr   r    r,   TFCCannot set a Categorical with another, without identical categoriesmatchr   r   r   pytestraises
ValueErrorr   r+   r.   r/   msgr   r   r   'test_setitem_different_unordered_raises>   s
    zITestCategoricalIndexingWithFactor.test_setitem_different_unordered_raisesTr-   r#   c              	   C   sR   t ddgddgdd}tddg}d}tjt|d || ||< W 5 Q R X d S )Nr   r    Tr;   Fr1   r2   r4   r8   r   r   r    test_setitem_same_ordered_raisesO   s
    
zBTestCategoricalIndexingWithFactor.test_setitem_same_ordered_raisesc                 C   s.   t dddg}|d |d< |d dks*td S )N)r   r   )r   r   r   r   )r   r   r   catr   r   r   test_setitem_tuple_   s    z4TestCategoricalIndexingWithFactor.test_setitem_tuplec                 C   s   t jd tt jjddddt jdg}t dgt j	}d||< |j
t dgt j	 }t|t jdgdd	 d S )
Nr   r      I sizei順 r   r   )r   randomseedr   randintastyper   Zadd_categoriesr   int64codesr   r   )r   r   r(   resultr   r   r   test_setitem_listlikeg   s    z7TestCategoricalIndexingWithFactor.test_setitem_listlikeN)__name__
__module____qualname__r   r*   r5   markparametrizer   r0   r:   r<   r?   rL   r   r   r   r   r      s4   

	


r   c                
   @   s  e Zd Zdd Zdd Zdd Zdd Zej	d	d
dddgd
dggdd Z
ej	dd
dddgd
dddgd
dddgd
dddggej	dd
dgd
dgd
d
gddggej	deegej	ddddgdd Zdd Zdd Zdd Zd d! Zd"d# ZdS )$TestCategoricalIndexingc                 C   s^   t dddddddg}|d }|dks*t|dd }t ddgddddgd}t|| d S )Nr   r    r   d   r@   r,   )r   r   r   r'   )r   r>   Zslicedr)   r   r   r   test_getitem_slicey   s    z*TestCategoricalIndexing.test_getitem_slicec                 C   sn   t jd tt jjddddt j}|jt dgt j	 }|t dgt j	 j}t
|| d S )Nr   r   r@   rA   rB   rD   )r   rE   rF   r   rG   rH   r   rJ   r   rI   r   r   )r   r   rK   r)   r   r   r   test_getitem_listlike   s
    z-TestCategoricalIndexing.test_getitem_listlikec           
      C   sf  t ddddddgdd}t|}t| tjddddddgtjd	}t dddgdd}t|j| t	|j
| t ddddddgdd}t|d
d}t| tjddddddgtjd	}t dddgdd}t|j| t	|j
| t dddddddgdd}t|d
d}	tjdddddddgtjd	}t dddddddgdd}t|	j| t	|	j
| d S )Nz2014-01z2014-02z2014-03M)freqr   r   r   r   Tr"   z2013-12z2013-11z2013-10z2013-09z2013-08z2013-07z2013-05   r@      rT   )r   r   strr   r   r   r   r   r   assert_index_equalr-   )
r   Zidx1Zcat1Zexp_arrZexp_idxZidx2Zcat2Zexp_idx2Zidx3Zcat3r   r   r   test_periodindex   s\      		z(TestCategoricalIndexing.test_periodindexc                 C   s`   t ddddg}tjddddgtjd}dddg|_t| | t|jt	dddg d S )Nr   r    r   r   r   rT   r   )
r   r   r   rI   r-   r   r   	__array__r\   r   )r   sexpr   r   r   test_categories_assignments   s
    z3TestCategoricalIndexing.test_categories_assignmentsnew_categoriesr   r   rT   rZ   c              	   C   s8   t ddddg}d}tjt|d ||_W 5 Q R X d S )Nr   r    r   zKnew categories need to have the same number of items as the old categories!r2   )r   r5   r6   r7   r-   )r   rb   r>   r9   r   r   r   /test_categories_assignments_wrong_length_raises   s
    zGTestCategoricalIndexing.test_categories_assignments_wrong_length_raises
idx_values
key_valuesr@   	key_classr   Ncategorykeyc                 C   s   ||t ddd}|dkr |j}t||d}||\}}||\}	}
t||	 t||
 | |}| |}t|| d S )Nr   r@   r,   rh   r   )ranger   r   Zget_indexer_non_uniquer   r   uniqueZget_indexer)r   rd   re   rf   r   rh   idxr)   Zexp_missrK   Zres_missZ
exp_uniqueZ
res_uniquer   r   r   test_get_indexer_non_unique   s    	z3TestCategoricalIndexing.test_get_indexer_non_uniquec                 C   s   t tddg}|ddg}t tdd gddgd}t|| t tddg}|ddg}t td d gddgd}t|| d S )Nr   r    TFr,   )r   r   wherer   assert_series_equalr   serrK   r)   r   r   r   test_where_unobserved_nan   s    z1TestCategoricalIndexing.test_where_unobserved_nanc                 C   s\   t tdddgddddgd}|jdddgdd}t tdddg|jjd}t|| d S )	Nr   r    r   rS   r,   TF)r+   )r   r   rm   r>   r-   r   rn   ro   r   r   r    test_where_unobserved_categories   s    z8TestCategoricalIndexing.test_where_unobserved_categoriesc                 C   sr   t tdddgddddgd}tdddgddddgd}|dddg|}t tdddg|jd}t|| d S )	Nr   r    r   rS   r,   TFr   )r   r   rm   r   r   rn   )r   rp   r+   rK   r)   r   r   r   test_where_other_categorical   s
    z4TestCategoricalIndexing.test_where_other_categoricalc              	   C   sF   t tdddg}d}tjt|d |dddgd W 5 Q R X d S )	Nr   r    r   z3Cannot setitem on a Categorical with a new categoryr2   TFrS   r   r   r5   r6   r7   rm   )r   rp   r9   r   r   r   test_where_new_category_raises  s    z6TestCategoricalIndexing.test_where_new_category_raisesc              	   C   sl   t tdddgddddgdd}tdddgddddgdd}tjtdd |dd	dg| W 5 Q R X d S )
Nr   r    r   rS   Tr;   zwithout identical categoriesr2   Frt   )r   rp   r+   r   r   r   !test_where_ordered_differs_rasies
  s     
 z9TestCategoricalIndexing.test_where_ordered_differs_rasies)rM   rN   rO   rU   rV   r]   ra   r5   rP   rQ   rc   r   r   rl   rq   rr   rs   ru   rv   r   r   r   r   rR   x   s&   	
3

 *$rR   c                	   @   sf   e Zd Zdd Zejjdedddfdedd	d
fdedd
fe	dd
fge
ddd Zdd ZdS )TestContainsc              	   C   s   t tdtdd}d|ks td|ks,ttj|ks:ttjtdd dg|ksXtW 5 Q R X d	|ksntd|ksztt tdtjg tdd}tj|kstd S )
NZaabbcaZcabr,   r    zzunhashable type: 'list'r2   r   r   )r   listr   r   nanr5   r6   	TypeError)r   r   r   r   r   test_contains  s    zTestContains.test_containszitem, expectedr   r   T)      ?Tg      ?r}   F)r   F)idsc                 C   s*   t ttd}||k}||ks&td S )NrT   )r   r   Zfrom_breaksri   r   )r   itemr)   r>   rK   r   r   r   test_contains_interval'  s    z#TestContains.test_contains_intervalc              	   C   sh   t dddg}d|ksttjtdd dg|k W 5 Q R X tjtdd ddg|k W 5 Q R X d S )Nr   r   rT   r   zunhashable typer2   r    )r   r   r5   r6   r{   r=   r   r   r   test_contains_list9  s    zTestContains.test_contains_listN)rM   rN   rO   r|   r5   rP   rQ   r   r
   r	   r[   r   r   r   r   r   r   rw     s   


rw   indexTFc                 C   sZ   t td}tdddg}| r&t|}t|s4t|| }||d }t	|| d S )NrT   TFobject)
r   ri   r   r   comZis_bool_indexerr   rH   r   rn   r   r_   rk   rK   r)   r   r   r   test_mask_with_booleanF  s    r   c                 C   sL   t td}tddd g}| r&t|}|| }||d }t|| d S )NrT   TF)r   ri   r   r   Zfillnar   rn   r   r   r   r   *test_mask_with_boolean_na_treated_as_falseS  s    r   c              	   c   s6   ddd}|   }|td| dV  W 5 Q R X dS )z
    Monkeypatch Categorical.__array__ to ensure no implicit conversion.

    Raises
    ------
    ValueError
        When Categorical.__array__ is called.
    Nc                 S   s   t dd S )NzI cannot be converted.)r7   )r   r   r   r   r   r   m  s    z(non_coercible_categorical.<locals>.arrayr^   )N)contextsetattrr   )Zmonkeypatchr   mr   r   r   non_coercible_categoricala  s    

r   c                 C   s0   t dddg}t|}|jd }|dks,td S )Nr   r    r   r   )r   r   atr   )r   Zarrrp   rK   r   r   r   test_series_atu  s    
r   ) Znumpyr   r5   Zpandasr   r   r   r   r   r   r   r	   r
   Zpandas._testingZ_testingr   Zpandas.core.commoncorecommonr   Z&pandas.tests.arrays.categorical.commonr   r   rR   rw   rP   rQ   r   r   Zfixturer   r   r   r   r   r   <module>   s    ,d 1


