o
    $jM                     @   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tmZassert_produces_warningFutureWarningZto_list)selfcat1msg r   i/var/www/html/Deteccion_Ine/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cZbca
categoriesr   )r   r   r   T)r   r   r   assert_index_equalr   r   r   )r   r   cat2Zcat3Z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   Zas_unorderedr   Z
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"            )r+   r,   r-   r+   Zdtyper   c                 S   s   |   S )N)upper)xr   r   r   <lambda>T   s    z;TestCategoricalAPI.test_rename_categories.<locals>.<lambda>)ABCr2   )r   rename_categoriesr   assert_numpy_array_equal	__array__nparrayint64r   r   r   object_assert_categorical_equal)r   r(   resZexp_catresultexpectedr   r   r   test_rename_categoriesC   s   z)TestCategoricalAPI.test_rename_categoriesnew_categoriesr+   r,   r-      r+   r,   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&   
ValueErrorr5   r   rA   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   r+   index)r   r5   r   r   r<   )r   r   r>   r?   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   drC   r-   r,   r+   rC   r-   r,   r+   )r   r   )r+   r   r-   rK         )r   r   r   rK   efrB   )rP   g)r   r5   r   r   r   r   )r   r(   r=   r?   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   r<   r   r(   oldnewr=   r   r   r   test_reorder_categories   s   z*TestCategoricalAPI.test_reorder_categoriesr   r   r   rK   rJ   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&   rD   rU   rE   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   rJ   r   rK   abcZabcderO   )
r   rT   add_categoriesr   r<   r   r   r8   r9   r   )r   r(   rW   rX   r=   r?   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 )NrJ   Tr   z5new categories must not include old categories: {'d'}r   rK   )r   reescaper%   r&   rD   r]   )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 )Nr+   r,   ZInt64r.   rC   )r+   r,   rC   r   r   r   r   rK   rZ   )r   r   r]   r   r<   r   )r   r(   Zserr>   r?   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   rS   r.   r   r   )r   re   r   rZ   )r   r+   re   r   rJ   )r+   r,   r-   rC   r+   rB   r   )r   r+   r,   r-   r   rL   )r-   r,   r+   r   r-   rC   r+   F)r   r   r8   r9   r;   set_categoriesr   r   r   r6   r7   codesint8_codesr:   Zasarrayminmaxr   r$   )	r   r(   Zexp_categoriesZ
exp_valuesr=   Zexp_categories_backr   expc2r   r   r   test_set_categories   sJ   z&TestCategoricalAPI.test_set_categoriesz"values, categories, new_categoriesrc   r   )r   r   r   r   )r   r   r   rK   rO   r   TFc                 C   s4   t ||}t |||}|j||d}t|| d S )Nr   )r   rf   r   r<   )r   valuesr   rA   r   r   r?   r>   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 )Nr2   r3   T)rename)r   rf   r8   nanr   r<   )r   r(   r>   r?   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   rJ   rb   )r   r   rK   rO   )r   r   rK   ZacdeT)Zfastpath)r   Z_set_categoriesr   r   r<   )r   r(   r?   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   rT   r8   rr   remove_categoriesr   r<   rV   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 )Nrc   z-removals must all be in old categories: {'c'}r   )r   r_   r`   r%   r&   rD   ru   )r   rw   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   rK   r   )r   r   r   rK   rO   rb   rJ   r   r   r   r   )r   r+   r,   re   r.   FDr3   ZABCDEFG)ro   r   )r3   r{   rz   )r,   re   r+   r   r+   r,   re   abcdefghijklmnopqrstuvwxyzr,   i'  objectd   )r   r   r   r   r   Zremove_unused_categoriesr8   rr   r9   rh   r6   rg   r   tolistrandomZdefault_rngchoiceZastypelen)
r   r   Zexp_categories_allZexp_categories_droppedr=   Z	exp_codesvalr(   outalphar   r   r   test_remove_unused_categories_  s4   $z0TestCategoricalAPI.test_remove_unused_categoriesN)__name__
__module____qualname__r   r!   r)   r@   r%   markparametrizerF   rI   rR   rY   r[   r^   ra   rd   rn   rp   rs   rt   rv   r8   rr   ry   r   r   r   r   r   r
      s^    
	
B 
r
   c                   @   s   e Zd Zdd ZdS )TestCategoricalAPIWithFactorc                 C   s\  t g ddd}| }|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 )N)r   r   r   r   r   r   r   r   Tr   r   r   )namer   )r-   r,   r-   )      ?      ?r   )countsZfreqsrG   rJ   abcd)r   r   )r-   r,   r-   r   )r   r   r   r   )r+   r,   r-   r+   r,   r-   r-   r,   r+   r+   r+   r*   )rM   r-   r-   )g]tE?tE]t?r   r+   r,   )r+   r,   r+   )r   g      ?r   )r   r   )r   Zdescriber   r   r   r   Zassert_frame_equalrT   rf   r   r8   rr   )r   factordescZ	exp_indexr?   r(   r   r   r   test_describe  sL   
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   r+   r,   r   re   rh   r.   z6property 'codes' of 'Categorical' object has no setterr#   r   )r   r+   r,   r   r+   z#assignment destination is read-onlyr+   rC   )r   r+   r,   r   r   r,   )r   r+   r,   r   r,   )r   r8   rr   r9   r   r6   rg   r   r%   r&   r'   rD   ri   )r   r   rl   r   rg   r   r   r   test_codes_immutable  s(   

z.TestPrivateCategoricalAPI.test_codes_immutablezcodes, old, new, expectedr   r+   r   r   )r   r+   r   r+   r   )r   r+   r,   r,   )r   r+   re   re   )r   r+   re   )re   r   re   rK   )re   re   re   re   c                 C   sL   t j|t jd}t j|t jd}t|}t|}t|||}t|| d S )Nr.   )r8   Z
asanyarrayrh   r   r	   r   r6   )r   rg   rW   rX   r?   r>   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  r+   re   r.   )r8   Zaranger   Zint16r	   r   r6   )r   Nrg   rW   r?   rX   r>   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   )r_   numpyr8   r%   Zpandas.compatr   Zpandasr   r   r   r   r   r   Zpandas._testingZ_testingr   Zpandas.core.arrays.categoricalr	   r
   r   r   r   r   r   r   <module>   s       q3