o
    ҷhZM                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ G dd dZG dd dZG d	d
 d
ZdS )    N)PY311)CategoricalCategoricalIndex	DataFrameIndexSeriesStringDtype)recode_for_categoriesc                   @   s  e Zd Zdd Zdd Zdd Zdd Zej	d	g d
ddggdd Z
dd Zdd Zdd Zej	d	dgg dg dgdd Zdd Zdd Zdd Zd d! Zej	d"g d#dd$gdd$gfg d#dd$gd$dgfg d%dd$gdd$gfg d%dd$gd$dgfg d&dd$gdd$gfg d&dd$gd$dgfg d'dd$gdd$gfg d'dd$gdd$gfg d&dd$gdgfg d&dd$gd$gfg d'dd$gdgfg d'dd$gdgfg d&dd$gd(d)gfgej	d*d+d,gd-d. Zd/d0 Zd1d2 Zd3d4 Zej	d5d6gd6ejgd6d6d6ggd7d8 Zd9d: Zd;S )<TestCategoricalAPIc                 C   sP   t tddd}d}tjt|d |  W d    d S 1 s!w   Y  d S )NacbForderedz5Categorical.to_list is deprecated and will be removedmatch)r   listtmassert_produces_warningFutureWarningto_list)selfcat1msg r   [/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_api.pytest_to_list_deprecated   s
   
"z*TestCategoricalAPI.test_to_list_deprecatedc                 C   s   t tddd}t|jtg d |jrJ t tdtddd}t|jtg d |jr3J t tddd}t|jtg d |jsKJ t tdtddd}t|jtg d |jsfJ d S )	Nr   Fr   abcbca
categoriesr   )r   r   r   T)r   r   r   assert_index_equalr!   r   r   )r   r   cat2cat3cat4r   r   r   test_ordered_api   s   


z#TestCategoricalAPI.test_ordered_apic                 C   s   t g ddd}| }|jrJ | }|jsJ |djs"J |djr*J tr.dnd}tjt|d d|_W d    n1 sEw   Y  tjt|d d|_W d    d S 1 s`w   Y  d S )Nr   r   r   r   Tr   Fz8property 'ordered' of 'Categorical' object has no settercan't set attributer   )	r   as_unorderedr   
as_orderedset_orderedr   pytestraisesAttributeError)r   catr#   r   r   r   r   test_set_ordered.   s"   

"z#TestCategoricalAPI.test_set_orderedc                 C   s   t g d}|g d}t| tjg dtjd t|j	t
g d tjg dtjd}t| | t
g d}t|j	| |dd }t g d}t|| d S )	Nr'            )r2   r3   r4   r2   dtyper   c                 S   s   |   S )N)upper)xr   r   r   <lambda>T   s    z;TestCategoricalAPI.test_rename_categories.<locals>.<lambda>)ABCr:   )r   rename_categoriesr   assert_numpy_array_equal	__array__nparrayint64r"   r!   r   object_assert_categorical_equal)r   r/   resexp_catresultexpectedr   r   r   test_rename_categoriesC   s   z)TestCategoricalAPI.test_rename_categoriesnew_categoriesr2   r3   r4      r2   r3   c                 C   sN   t g d}d}tjt|d || W d    d S 1 s w   Y  d S )Nr'   zKnew categories need to have the same number of items as the old categories!r   )r   r,   r-   
ValueErrorr=   r   rJ   r/   r   r   r   r   *test_rename_categories_wrong_length_raisesX   s   "z=TestCategoricalAPI.test_rename_categories_wrong_length_raisesc                 C   sB   t ddg}|tddgddgd}t ddg}t|| d S )Nr   r   r   r2   index)r   r=   r   r   rD   )r   r   rG   rH   r   r   r   test_rename_categories_seriesb   s   z0TestCategoricalAPI.test_rename_categories_seriesc              	   C   s   t g d}|ddddd}tg d}t|j| t g d}|ddd}tg d}t|j| t g d}|ddddd	d
d}tg d}t|j| t g d}|ddd}tg d}t|j| d S )Nr   r   r   drL   r4   r3   r2   rL   r4   r3   r2   )r   r   )r2   r   r4   rT         )r   r   r   rT   efrK   )rY   g)r   r=   r   r   r"   r!   )r   r/   rE   rH   r   r   r   test_rename_categories_dicti   s    z.TestCategoricalAPI.test_rename_categories_dictc                 C   sX   t g ddd}| }t g dg ddd}|g d}t|| t|| d S )Nr'   Tr   r   r   r   r    )r   copyreorder_categoriesr   rD   r   r/   oldnewrE   r   r   r   test_reorder_categories   s   z*TestCategoricalAPI.test_reorder_categoriesr   r   r   rT   rS   c                 C   sR   t g ddd}d}tjt|d || W d    d S 1 s"w   Y  d S )Nr'   Tr   z=items in new_categories are not the same as in old categoriesr   )r   r,   r-   rM   r^   rN   r   r   r   test_reorder_categories_raises   s
   	"z1TestCategoricalAPI.test_reorder_categories_raisesc                 C   s  t g ddd}| }t g dg ddd}|d}t|| t|| |dg}t|| t|| t tddd}t tdtddd}|tdd	g}t|| |tdd	g}t|| |t	dd	g}t|| |dd	g}t|| d S )
Nr'   Tr   rS   r    rT   abcabcderX   )
r   r]   add_categoriesr   rD   r   r   r@   rA   r   )r   r/   r`   ra   rE   rH   r   r   r   test_add_categories   s*   
z&TestCategoricalAPI.test_add_categoriesc                 C   sZ   t g ddd}td}tjt|d |dg W d    d S 1 s&w   Y  d S )NrS   Tr   z5new categories must not include old categories: {'d'}r   rT   )r   reescaper,   r-   rM   rg   )r   r/   r   r   r   r   #test_add_categories_existing_raises   s
   
"z6TestCategoricalAPI.test_add_categories_existing_raisesc                 C   s   t tddgdd}tdgdd}||}t tddgddtg dddd}t|| t tg dt d}td	gt d}||}t tg dt dtg d
t dd}t|| d S )Nr2   r3   Int64r5   rL   )r2   r3   rL   r!   r   r   r   rT   rc   )r   r   rg   r   rD   r   )r   r/   serrG   rH   r   r   r   ,test_add_categories_losing_dtype_information   s   

z?TestCategoricalAPI.test_add_categories_losing_dtype_informationc           	      C   s  t g ddd}tg d}tjg dtjd}|g d}|g d}t|j| t	|
 | tg d}t|j| t	|
 | t g ddd}|dg}t	|jtjg dtjd |g d	}t	|jtjg d
tjd t|jtg d	 |g d}tg d}t|j| t g dg ddd}t	|jtjg dtjd t|jtg d tjg dtjd}t	t|| |g d}t	|jtjg dtjd t|jtg d tjg dtjd}t	t|| | dksJ | dksJ |jg ddd}|jr)J t	t|t| |dg d}|jrEJ t	t|t| d S )Nr'   Tr   r\   r5   r   r   )r   rq   r   rc   )r   r2   rq   r   rS   )r2   r3   r4   rL   r2   rK   r    )r   r2   r3   r4   r   rU   )r4   r3   r2   r   r4   rL   r2   F)r   r   r@   rA   rC   set_categoriesr   r"   r!   r>   r?   codesint8_codesrB   asarrayminmaxr   r+   )	r   r/   exp_categories
exp_valuesrE   exp_categories_backr   expc2r   r   r   test_set_categories   sJ   z&TestCategoricalAPI.test_set_categoriesz"values, categories, new_categoriesrn   r   )r   r   r   r   )r   r   r   rT   rX   r   TFc                 C   s4   t ||}t |||}|j||d}t|| d S )Nr   )r   rr   r   rD   )r   valuesr!   rJ   r   r   rH   rG   r   r   r   test_set_categories_many  s   
z+TestCategoricalAPI.test_set_categories_manyc                 C   s:   t ddg}|jdgdd}t dtjg}t|| d S )Nr:   r;   T)rename)r   rr   r@   nanr   rD   )r   r/   rG   rH   r   r   r   test_set_categories_rename_less7  s   z2TestCategoricalAPI.test_set_categories_rename_lessc                 C   s   t g dg dd}|g d t g dtdd}t|| t g dg dd}|jg ddd t g dtdd}t|| d S )	Nr   rS   rm   )r   r   rT   rX   )r   r   rT   acdeT)fastpath)r   _set_categoriesr   r   rD   )r   r/   rH   r   r   r   test_set_categories_private>  s   z.TestCategoricalAPI.test_set_categories_privatec                 C   s~   t g ddd}| }t ddtjdgddgdd}|d}t|| t|| |dg}t|| t|| d S )Nr'   Tr   r   r   r    r   )r   r]   r@   r   remove_categoriesr   rD   r_   r   r   r   test_remove_categoriesJ  s   
z)TestCategoricalAPI.test_remove_categoriesremovalsr   c                 C   sT   t g d}td}tjt|d || W d    d S 1 s#w   Y  d S )Nrn   z-removals must all be in old categories: {'c'}r   )r   ri   rj   r,   r-   rM   r   )r   r   r/   messager   r   r   test_remove_categories_raisesW  s
   
"z0TestCategoricalAPI.test_remove_categories_raisesc           
      C   s  t g dg dd}tg d}tg d}t|j| | }t|j| t|j| t dddtjgg dd}| }t|jttg d tjg d	tj	d
}t
|j| t|j| dtjddddtjg}t |tdd}| }t|jtg d tjg dtj	d
}t
|j| | |ksJ td}	tjd|	d d d dd}tj|tjdt|d< t ||	d}| }| | ksJ d S )N)r   r   r   rT   r   )r   r   r   rT   rX   rm   rS   r   r   r   r   )r   r2   r3   rq   r5   FDr;   ABCDEFG)r   r!   )r;   r   r   )r3   rq   r2   r   r2   r3   rq   abcdefghijklmnopqrstuvwxyzr3   i'  objectd   )r   r   r   r"   r!   remove_unused_categoriesr@   r   rA   rt   r>   rs   r   tolistrandomdefault_rngchoiceastypelen)
r   r   exp_categories_allexp_categories_droppedrE   	exp_codesvalr/   outalphar   r   r   test_remove_unused_categories_  s4   $z0TestCategoricalAPI.test_remove_unused_categoriesN)__name__
__module____qualname__r   r&   r0   rI   r,   markparametrizerO   rR   r[   rb   rd   rh   rk   rp   r~   r   r   r   r   r@   r   r   r   r   r   r   r   r
      s^    
	
B 
r
   c                   @   s   e Zd Zdd ZdS )TestCategoricalAPIWithFactorc                 C   sL  |  }|js	J tg dd|jd}tg dg dd|d}t|| | }|g d}|  }ttd	|jdd
}tg dg dd|d}t|| t	g d}|  }tg d|jdd
}tg dg dd|d}t|| t	t
jdddg}|  }tg dg ddtddt
jgddgddd}t|| d S )Nr   r!   )namer   )r4   r3   r4   )      ?      ?r   )countsfreqsrP   rS   abcd)r   r   )r4   r3   r4   r   )r   r   r   r   )r2   r3   r4   r2   r3   r4   r4   r3   r2   r2   r2   r1   )rV   r4   r4   )g]tE?tE]t?r   r2   r3   )r2   r3   r2   )r   g      ?r   )r!   r   )describer   r   r   r   assert_frame_equalr]   rr   r   r   r@   r   )r   factordesc	exp_indexrH   r/   r   r   r   test_describe  sJ   
z*TestCategoricalAPIWithFactor.test_describeN)r   r   r   r   r   r   r   r   r     s    r   c                   @   s^  e Zd Zdd Zejdddgddgddgddgfddgddgddgddgfddgddgddgddgfddgddgddgddgfg dddgg d	g dfg d
g d	ddgg dfg dg d	g d	g dfg dg d	dgg dfg dg d	dgg dfg dg d	g g dfddgg ddgddgfddgddgddgddgfgdd Zdd ZdS )TestPrivateCategoricalAPIc                 C   s  t ddddtjg}tjg ddd}t|j| trdnd}tj	t
|d	 tjg d
dd|_W d    n1 s<w   Y  |j}tj	tdd	 d|d< W d    n1 sZw   Y  d|d< tjg ddd}t|j| d|jd< tjg ddd}t|j| d S )Nr   r   r   )r   r2   r3   r   rq   rt   r5   z6property 'codes' of 'Categorical' object has no setterr(   r   )r   r2   r3   r   r2   z#assignment destination is read-onlyr2   rL   )r   r2   r3   r   r   r3   )r   r2   r3   r   r3   )r   r@   r   rA   r   r>   rs   r   r,   r-   r.   rM   ru   )r   r   r|   r   rs   r   r   r   test_codes_immutable  s(   

z.TestPrivateCategoricalAPI.test_codes_immutablezcodes, old, new, expectedr   r2   r   r   )r   r2   r   r2   r   )r   r2   r3   r3   )r   r2   rq   rq   )r   r2   rq   )rq   r   rq   rT   )rq   rq   rq   rq   c                 C   sL   t j|t jd}t j|t jd}t|}t|}t|||}t|| d S )Nr5   )r@   
asanyarrayrt   r   r	   r   r>   )r   rs   r`   ra   rH   rG   r   r   r   test_recode_to_categories  s   z3TestPrivateCategoricalAPI.test_recode_to_categoriesc                 C   sR   d}t |}t|}t j|d ddt jd}t|}t|||}t|| d S )Ni  r2   rq   r5   )r@   aranger   int16r	   r   r>   )r   Nrs   r`   rH   ra   rG   r   r   r   test_recode_to_categories_large  s   
z9TestPrivateCategoricalAPI.test_recode_to_categories_largeN)	r   r   r   r   r,   r   r   r   r   r   r   r   r   r     s(    
r   )ri   numpyr@   r,   pandas.compatr   pandasr   r   r   r   r   r   pandas._testing_testingr   pandas.core.arrays.categoricalr	   r
   r   r   r   r   r   r   <module>   s       q2