a
    Pf2                     @   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g ddeddgg d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g dddgdd Zdd Zdd ZdS )!TestCategoricalIndexingWithFactorc                 C   s   t g ddd}|d dks J |d dks0J |g d }t|jtjg d	tjd
 |t|dk }t|jtjg dtjd
 d S )Nabr   r   r   cr   r   Torderedr   r   r   )r         )r   r   r   dtype)r   r   r   )r   tmassert_numpy_array_equal_codesnparrayint8Zasarray)selffactorZsubf r!   f/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/tests/arrays/categorical/test_indexing.pytest_getitem   s    z.TestCategoricalIndexingWithFactor.test_getitemc                 C   s   t g ddd}| }d|d< |d dks0J d|d< |d dksHJ | }tjt|dd	}d|d< d|d< d
||< t g ddd}t|| d S )Nr   Tr   r   r   r   r   boolr   r   )r   r   r   r   r   r   r   r   )r   copyr   Zzeroslenr   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_unordered6   s
    zATestCategoricalIndexingWithFactor.test_setitem_same_but_unordered)r   r   r   r   r   r   c                 C   sd   t ddgddgd}tddg}d}tjt|d || ||< W d    n1 sV0    Y  d S )Nr   r   r,   TFCCannot set a Categorical with another, without identical categoriesmatchr   r   r   pytestraises	TypeErrorr   r+   r.   r/   msgr!   r!   r"   'test_setitem_different_unordered_raisesB   s
    zITestCategoricalIndexingWithFactor.test_setitem_different_unordered_raisesTr-   r   c                 C   sf   t ddgddgdd}tddg}d}tjt|d || ||< W d    n1 sX0    Y  d S )Nr   r   Tr<   Fr2   r3   r5   r9   r!   r!   r"    test_setitem_same_ordered_raisesS   s
    
zBTestCategoricalIndexingWithFactor.test_setitem_same_ordered_raisesc                 C   s,   t g d}|d |d< |d dks(J d S )N)r   r   )r   r   r>   r   r   r>   )r   r   catr!   r!   r"   test_setitem_tuplec   s    z4TestCategoricalIndexingWithFactor.test_setitem_tuplec                 C   s~   t tjd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   r   randomdefault_rngintegersastyper   Zadd_categoriesr   int64codesr   r   )r   r@   r(   resultr!   r!   r"   test_setitem_listlikek   s    z7TestCategoricalIndexingWithFactor.test_setitem_listlikeN)__name__
__module____qualname__r#   r*   r6   markparametrizer   r0   r;   r=   rA   rN   r!   r!   r!   r"   r      s4   
	


r   c                   @   s  e Z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g dddggdd Zejdg dg dg dg d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g dd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ ZdS ),TestCategoricalIndexingc                 C   sP   t g d}|d }|dks J |dd }t ddgg dd}t|| d S )N)r   r   r   dr   r   r      rU   rB   r   )r   r   r   rU   r,   )r   r   r'   )r   r@   Zslicedr)   r!   r!   r"   test_getitem_slice|   s    z*TestCategoricalIndexing.test_getitem_slicec                 C   sh   t tjdjddddtj}|jtdgtj	 }|tdgtj	 j}t
|| d S )Nr   r   rB   rC   rD   rF   )r   r   rG   rH   rI   rJ   r   rL   r   rK   r   r   )r   r   rM   r)   r!   r!   r"   test_getitem_listlike   s    z-TestCategoricalIndexing.test_getitem_listlikec           
      C   s$  t g ddd}t|}t| tjg dtjd}t g ddd}t|j| t	|j
| t g ddd}t|dd	}t| tjg d
tjd}t g ddd}t|j| t	|j
| t g ddd}t|dd	}	tjg dtjd}t g ddd}t|	j| t	|	j
| d S )N)2014-01rY   2014-02rZ   2014-03r[   Mfreq)r   r   r   r   r   r   r   )rY   rZ   r[   )r[   r[   rZ   rY   r[   rY   Tr   )r   r   r   r   r   r   )2013-122013-112013-102013-092013-082013-072013-05)   rB      rV   r   r   r   )re   rd   rc   rb   ra   r`   r_   )r	   r   strr   r   r   r   r   r   assert_index_equalr-   )
r   Zidx1Zcat1Zexp_arrZexp_idxZidx2cat2Zexp_idx2Zidx3Zcat3r!   r!   r"   test_periodindex   s@    		z(TestCategoricalIndexing.test_periodindexnull_valNr   ZnatZNATnanNaNZNANc                 C   sB   t dd|gdd}t g ddd}|d tu s2J t|| d S )	N
2022-04-06
2022-04-07Dr]   )ro   rp   r   z	period[D]r   r   )r	   r   r   ri   )r   rl   rM   r)   r!   r!   r"   test_periodindex_on_null_types   s    z6TestCategoricalIndexing.test_periodindex_on_null_typesnew_categories)r   r   rV   rg   r   r   c                 C   sL   t g d}d}tjt|d || W d    n1 s>0    Y  d S )N)r   r   r   r   zKnew categories need to have the same number of items as the old categories!r3   )r   r6   r7   
ValueErrorZrename_categories)r   rs   r@   r:   r!   r!   r"   /test_categories_assignments_wrong_length_raises   s
    zGTestCategoricalIndexing.test_categories_assignments_wrong_length_raises
idx_values)r   rV   r   rg   )r   rV   rV   rg   )r   r   r   rg   
key_valuesrB   	key_classr   )Ncategorykeyc                 C   s   ||t ddd}|dkr |j}t||d}||\}}||\}	}
t||	 t||
 | |}| |}t|| d S )Nr   rB   r,   rz   r   )ranger   r   Zget_indexer_non_uniquer   r   uniqueZget_indexer)r   rv   rw   rx   r   rz   idxr)   Zexp_missrM   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   serrM   r)   r!   r!   r"   test_where_unobserved_nan   s    z1TestCategoricalIndexing.test_where_unobserved_nanc                 C   sR   t tg dg dd}|jg ddd}t tg d|jjd}t|| d S )Nr1   rU   r   r   r   r,   )TTFr   )r+   )r   r   r   )r
   r   r   r@   r-   r   r   r   r!   r!   r"    test_where_unobserved_categories  s    z8TestCategoricalIndexing.test_where_unobserved_categoriesc                 C   sb   t tg dg dd}tg dg dd}|g d|}t tg d|jd}t|| d S )	Nr1   r   r,   r   r   r   r   r   r   rU   TFT)r   r   r   r   )r
   r   r   r   r   r   )r   r   r+   rM   r)   r!   r!   r"   test_where_other_categorical  s
    z4TestCategoricalIndexing.test_where_other_categoricalc                 C   sV   t tg d}d}tjt|d  |g dd W d    n1 sH0    Y  d S )Nr1   z3Cannot setitem on a Categorical with a new categoryr3   r   rU   r
   r   r6   r7   r8   r   )r   r   r:   r!   r!   r"   test_where_new_category_raises  s    z6TestCategoricalIndexing.test_where_new_category_raisesc                 C   sr   t tg dg ddd}tg dg ddd}tjtdd  |g d	| W d    n1 sd0    Y  d S )
Nr1   r   Tr<   r   r   zwithout identical categoriesr3   r   r   )r   r   r+   r!   r!   r"   !test_where_ordered_differs_rasies  s    z9TestCategoricalIndexing.test_where_ordered_differs_rasies)rO   rP   rQ   rW   rX   rk   r6   rR   rS   r   rm   r   r   mathrr   ru   r   r   r~   r   r   r   r   r   r!   r!   r!   r"   rT   {   s,   	5


$rT   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|v s J d|vs,J tj|vs:J tjtdd dg|v sXJ W d    n1 sl0    Y  d	|vsJ d|vsJ t tdtjg tdd}tj|v sJ d S )
NZaabbcaZcabr,   r   zzunhashable type: 'list'r3   r   r   )r   listr   rm   r6   r7   r8   r?   r!   r!   r"   test_contains   s    ,zTestContains.test_containszitem, expectedr   r   T)      ?Tg      ?r   F)r   F)Zidsc                 C   s*   t ttd}||v }||u s&J d S )NrV   )r   r   Zfrom_breaksr{   )r   itemr)   r@   rM   r!   r!   r"   test_contains_interval1  s    z#TestContains.test_contains_intervalc                 C   s   t g d}d|vsJ tjtdd dg|v  W d    n1 sF0    Y  tjtdd ddg|v  W d    n1 s0    Y  d S )N)r   r   rV   r   zunhashable typer3   r   )r   r6   r7   r8   r?   r!   r!   r"   test_contains_listC  s    (zTestContains.test_contains_listN)rO   rP   rQ   r   r6   rR   rS   r   r   r   rh   r   r   r!   r!   r!   r"   r     s   


r   indexTFc                 C   sX   t td}tg d}| r$t|}t|s2J || }||d }t|| d S )NrV   r   object)	r
   r{   r   r   comZis_bool_indexerrJ   r   r   r   r   r}   rM   r)   r!   r!   r"   test_mask_with_booleanP  s    r   c                 C   sJ   t td}tg d}| r$t|}|| }||d }t|| d S )NrV   )TFNF)r
   r{   r   r   Zfillnar   r   r   r!   r!   r"   *test_mask_with_boolean_na_treated_as_false]  s    r   c                 c   sJ   ddd}|   $}|td| dV  W d   n1 s<0    Y  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.)rt   )r   r   r!   r!   r"   r   x  s    z(non_coercible_categorical.<locals>.arrayZ	__array__)N)contextsetattrr   )Zmonkeypatchr   mr!   r!   r"   non_coercible_categoricalk  s    

r   c                  C   s.   t g d} t| }|jd }|dks*J d S )Nr1   r   r   )r   r
   at)Zarrr   rM   r!   r!   r"   test_series_at  s    
r   )!r   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   rT   r   rR   rS   r   r   Zfixturer   r   r!   r!   r!   r"   <module>   s    4d %1


