U
    c3                     @   s   d dl Z 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mZmZ d dlmZ d dlm  mZ G dd d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)NACategoricalCategoricalIndexIndexIntervalIntervalIndexNaTPeriodIndexSeries	Timedelta	Timestampc                
   @   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   |d dkst |d dks t |dddg }t|jtjdddgtjd |t|dk }t|jtjdddgtjd d S )Nr   ac      dtype)AssertionErrortmassert_numpy_array_equal_codesnparrayint8Zasarray)selffactorZsubf r   Q/tmp/pip-unpacked-wheel-g7fro6k3/pandas/tests/arrays/categorical/test_indexing.pytest_getitem   s     z.TestCategoricalIndexingWithFactor.test_getitemc              	   C   s   |  }d|d< |d dks td|d< |d dks8t|  }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)copyr   r   zeroslenr   r   assert_categorical_equal)r   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_unordered5   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	TypeErrorr   r,   r/   r0   msgr   r   r   'test_setitem_different_unordered_raisesA   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<   Fr2   r3   r5   r9   r   r   r    test_setitem_same_ordered_raisesR   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_tupleb   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_listlikej   s    z7TestCategoricalIndexingWithFactor.test_setitem_listlikeN)__name__
__module____qualname__r    r+   r6   markparametrizer   r1   r;   r=   r@   rM   r   r   r   r   r      s4   

	


r   c                   @   s4  e Zd Zdd Zdd Zdd Zdd Zej	d	d
e
jeeejddddddg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   rA   r-   )r   r   r   r(   )r   r?   Zslicedr*   r   r   r   test_getitem_slice|   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   rA   rB   rC   rE   )r   rF   rG   r   rH   rI   r   rK   r   rJ   r   r   )r   r   rL   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   rA      rU   )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   sz   t ddddg}tjddddgtjd}tjtdd	 dddg|_W 5 Q R X t|	 | t
|jtdddg d S )
Nr   r!   r   r   r   rU   r   Use rename_categoriesr3   )r   r   r   rJ   r   assert_produces_warningFutureWarningr.   r   	__array__r^   r   )r   r?   expr   r   r   test_categories_assignments   s    z3TestCategoricalIndexing.test_categories_assignmentsnull_valNr   ZnatZNATnanNaNZNANc                 C   sD   t dd|gdd}t dddgdd}|d tks4tt|| d S )	Nz
2022-04-06z
2022-04-07DrY   r   z	period[D]r   r   )r	   r   r   r   r^   )r   rf   rL   r*   r   r   r   test_periodindex_on_null_types   s    z6TestCategoricalIndexing.test_periodindex_on_null_typesnew_categoriesr   r   rU   r\   c              
   C   sR   t ddddg}d}tjtdd& tjt|d ||_W 5 Q R X W 5 Q R X d S )Nr   r!   r   zKnew categories need to have the same number of items as the old categories!r`   r3   )r   r   ra   rb   r6   r7   
ValueErrorr.   )r   rk   r?   r:   r   r   r   /test_categories_assignments_wrong_length_raises   s    zGTestCategoricalIndexing.test_categories_assignments_wrong_length_raises
idx_values
key_valuesrA   	key_classr   categorykeyc                 C   s   ||t ddd}|dkr |j}t||d}||\}}||\}	}
t||	 t||
 | |}| |}t|| d S )Nr   rA   r-   rr   r   )ranger   r   Zget_indexer_non_uniquer   r   uniqueZget_indexer)r   rn   ro   rp   r   rr   idxr*   Zexp_missrL   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   serrL   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   rT   r-   TF)r,   )r
   r   rw   r?   r.   r   rx   ry   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   rT   r-   TFr   )r
   r   rw   r   r   rx   )r   rz   r,   rL   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 categoryr3   TFrT   r
   r   r6   r7   r8   rw   )r   rz   r:   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   rT   Tr<   zwithout identical categoriesr3   Fr~   )r   rz   r,   r   r   r   !test_where_ordered_differs_rasies  s     
 z9TestCategoricalIndexing.test_where_ordered_differs_rasies)rN   rO   rP   rV   rW   r_   re   r6   rQ   rR   r   rg   r   r   mathrj   rm   r   r   rv   r{   r|   r}   r   r   r   r   r   r   rS   {   s0   	
3

 *$rS   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'r3   r   r   )r   listr   r   rg   r6   r7   r8   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 )NrU   )r   r   Zfrom_breaksrs   r   )r   itemr*   r?   rL   r   r   r   test_contains_interval7  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   rU   r   zunhashable typer3   r!   )r   r   r6   r7   r8   r>   r   r   r   test_contains_listI  s    zTestContains.test_contains_listN)rN   rO   rP   r   r6   rQ   rR   r   r   r   r]   r   r   r   r   r   r   r   %  s   


r   indexTFc                 C   sZ   t td}tdddg}| r&t|}t|s4t|| }||d }t	|| d S )NrU   TFobject)
r
   rs   r   r   comZis_bool_indexerr   rI   r   rx   r   rz   ru   rL   r*   r   r   r   test_mask_with_booleanV  s    r   c                 C   sL   t td}tddd g}| r&t|}|| }||d }t|| d S )NrU   TF)r
   rs   r   r   Zfillnar   rx   r   r   r   r   *test_mask_with_boolean_na_treated_as_falsec  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.)rl   )r   r   r   r   r   r   }  s    z(non_coercible_categorical.<locals>.arrayrc   )N)contextsetattrr   )Zmonkeypatchr   mr   r   r   non_coercible_categoricalq  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   )Zarrrz   rL   r   r   r   test_series_at  s    
r   )!r   Znumpyr   r6   Zpandasr   r   r   r   r   r   r   r	   r
   r   r   Zpandas._testingZ_testingr   Zpandas.core.commoncorecommonr   r   rS   r   rQ   rR   r   r   Zfixturer   r   r   r   r   r   <module>   s    4d +1


