o
    ŀg2                     @   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sJ |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int8asarray)selffactorsubf r#   a/var/www/html/myenv/lib/python3.10/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sJ d|d< |d dks$J | }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   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   sf   t ddgddgd}tddg}d}tjt|d || ||< W d    d S 1 s,w   Y  d S )Nr   r   r/   TFCCannot set a Categorical with another, without identical categoriesmatchr   r   r   pytestraises	TypeErrorr    r.   r1   r2   msgr#   r#   r$   'test_setitem_different_unordered_raisesB   s   "zITestCategoricalIndexingWithFactor.test_setitem_different_unordered_raisesTr0   r   c                 C   sh   t ddgddgdd}tddg}d}tjt|d || ||< W d    d S 1 s-w   Y  d S )Nr   r   Tr?   Fr5   r6   r8   r<   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   rA   r   r   rA   )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   add_categoriesr   int64codesr   r   )r    rC   r+   resultr#   r#   r$   test_setitem_listlikek   s   z7TestCategoricalIndexingWithFactor.test_setitem_listlikeN)__name__
__module____qualname__r%   r-   r9   markparametrizer   r3   r>   r@   rD   rR   r#   r#   r#   r$   r      s6    

	

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      rY   rE   r   )r   r   r   rY   r/   )r   r   r*   )r    rC   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   rE   rF   rG   rI   )r   r   rJ   rK   rL   rM   r   rP   r   rO   r   r   )r    r   rQ   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-01r^   2014-02r_   2014-03r`   Mfreq)r   r   r   r   r   r   r   )r^   r_   r`   )r`   r`   r_   r^   r`   r^   Tr   )r   r   r   r   r   r   )2013-122013-112013-102013-092013-082013-072013-05)   rE      rZ   r   r   r   )rj   ri   rh   rg   rf   re   rd   )r	   r   strr   r   r   r   r   r   assert_index_equalr0   )
r    idx1cat1exp_arrexp_idxidx2cat2exp_idx2idx3cat3r#   r#   r$   test_periodindex   s@   		z(TestCategoricalIndexing.test_periodindexnull_valNr   natNATnanNaNNANc                 C   sB   t dd|gdd}t g ddd}|d tu sJ t|| d S )	N
2022-04-06
2022-04-07Drb   )r   r   r   z	period[D]r   r   )r	   r   r   rn   )r    ry   rQ   r,   r#   r#   r$   test_periodindex_on_null_types   s   z6TestCategoricalIndexing.test_periodindex_on_null_typesnew_categories)r   r   rZ   rl   r   r   c                 C   sN   t g d}d}tjt|d || W d    d S 1 s w   Y  d S )N)r   r   r   r   zKnew categories need to have the same number of items as the old categories!r6   )r   r9   r:   
ValueErrorrename_categories)r    r   rC   r=   r#   r#   r$   /test_categories_assignments_wrong_length_raises   s   "zGTestCategoricalIndexing.test_categories_assignments_wrong_length_raises
idx_values)r   rZ   r   rl   )r   rZ   rZ   rl   )r   r   r   rl   
key_valuesrE   	key_classr   )Ncategorykeyc                 C   s   ||t ddd}|dkr|j}t||d}||\}}||\}	}
t||	 t||
 | |}| |}t|| d S )Nr   rE   r/   r   r   )ranger   r   get_indexer_non_uniquer   r   uniqueget_indexer)r    r   r   r   r   r   idxr,   exp_missrQ   res_miss
exp_unique
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    serrQ   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 )Nr4   rY   r   r   r   r/   )TTFr   )r.   )r   r   r   )r
   r   r   rC   r0   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 )	Nr4   r   r/   r   r   r   r   r   r   rY   TFT)r   r   r   r   )r
   r   r   r   r   r   )r    r   r.   rQ   r,   r#   r#   r$   test_where_other_categorical  s
   z4TestCategoricalIndexing.test_where_other_categoricalc                 C   sX   t tg d}d}tjt|d |g dd W d    d S 1 s%w   Y  d S )Nr4   z3Cannot setitem on a Categorical with a new categoryr6   r   rY   r
   r   r9   r:   r;   r   )r    r   r=   r#   r#   r$   test_where_new_category_raises  s
   "z6TestCategoricalIndexing.test_where_new_category_raisesc                 C   st   t tg dg ddd}tg dg ddd}tjtdd |g d	| W d    d S 1 s3w   Y  d S )
Nr4   r   Tr?   r   r   zwithout identical categoriesr6   r   r   )r    r   r.   r#   r#   r$   !test_where_ordered_differs_rasies  s   "z9TestCategoricalIndexing.test_where_ordered_differs_rasies)rS   rT   rU   r\   r]   rx   r9   rV   rW   r   r|   r   r   mathr   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   rX   {   s.    	5


$rX   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 s,J W d    n1 s6w   Y  d	|vsAJ d|vsGJ t tdtjg tdd}tj|v s\J d S )
Naabbcacabr/   r   zzunhashable type: 'list'r6   r   r   )r   listr   r|   r9   r:   r;   rB   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}||v }||u sJ d S )NrZ   )r   r   from_breaksr   )r    itemr,   rC   rQ   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 s#w   Y  tjtdd ddg|v  W d    d S 1 sAw   Y  d S )N)r   r   rZ   r   zunhashable typer6   r   )r   r9   r:   r;   rB   r#   r#   r$   test_contains_listC  s   "zTestContains.test_contains_listN)rS   rT   rU   r   r9   rV   rW   r   r   r   rm   r   r   r#   r#   r#   r$   r     s    


r   indexTFc                 C   sX   t td}tg d}| rt|}t|sJ || }||d }t|| d S )NrZ   r   object)	r
   r   r   r   comis_bool_indexerrM   r   r   r   r   r   rQ   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 )NrZ   )TFNF)r
   r   r   r   fillnar   r   r   r#   r#   r$   *test_mask_with_boolean_na_treated_as_false]  s   r   c                 c   sN    ddd}|   }|td| dV  W d   dS 1 s w   Y  dS )z
    Monkeypatch Categorical.__array__ to ensure no implicit conversion.

    Raises
    ------
    ValueError
        When Categorical.__array__ is called.
    Nc                 S   s   t d)NzI cannot be converted.)r   )r    r   r#   r#   r$   r   x  s   z(non_coercible_categorical.<locals>.array	__array__)N)contextsetattrr   )monkeypatchr   mr#   r#   r$   non_coercible_categoricalk  s   

"r   c                  C   s.   t g d} t| }|jd }|dksJ d S )Nr4   r   r   )r   r
   at)arrr   rQ   r#   r#   r$   test_series_at  s   
r   )!r   numpyr   r9   pandasr   r   r   r   r   r   r   r	   r
   r   r   pandas._testing_testingr   pandas.core.commoncorecommonr   r   rX   r   rV   rW   r   r   fixturer   r   r#   r#   r#   r$   <module>   s"    4d %1


