U
    NZf&L                     @   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                   @   s6  e Zd Zdd Zdd Zdd Zejddd	d
dgdd	ggdd Z	dd Z
dd Zdd Zejddgdddgddddggdd Zdd Zdd Zdd Zd d! Zejd"dddgddgddgfdddgddgddgfdddgddgddgfdddgddgddgfdddgddgddgfdddgddgddgfdddgddgddgfdddgddgddgfdddgddgdgfdddgddgdgfdddgddgdgfdddgddgdgfdddgddgdd#gfgejd$d%d&gd'd( Zd)d* Zd+d, Zd-d. Zejd/dgdejgdddggd0d1 Zd2d3 Zd4S )5TestCategoricalAPIc                 C   s   t tddd}t|jtdddg |jr2tt tdtddd}t|jtdddg |jrjtt tdd	d}t|jtdddg |jstt tdtdd	d}t|jtdddg |jstd S )
NZacbForderedabcZbca
categoriesr   T)r   listtmassert_index_equalr   r   r   AssertionError)selfZcat1cat2Zcat3Zcat4 r   L/tmp/pip-unpacked-wheel-nbcvw55c/pandas/tests/arrays/categorical/test_api.pytest_ordered_api   s    


z#TestCategoricalAPI.test_ordered_apic              	   C   s   t ddddgdd}| }|jr&t| }|js8t|djsHt|djrXttr`dnd}tjt	|d	 d|_W 5 Q R X tjt	|d	 d|_W 5 Q R X d S )
Nr   r   r   Tr   Fz8property 'ordered' of 'Categorical' object has no settercan't set attributematch)
r   Zas_unorderedr   r   Z
as_orderedset_orderedr   pytestraisesAttributeError)r   catr   msgr   r   r   test_set_ordered'   s    

z#TestCategoricalAPI.test_set_orderedc                 C   s   t ddddg}|dddg}t| tjddddgtjd t|j	t
dddg tjddddgtjd}t| | t
dddg}t|j	| |dd	 }t d
ddd
g}t|| d S )Nr   r   r            Zdtypec                 S   s   |   S )N)upper)xr   r   r   <lambda>M       z;TestCategoricalAPI.test_rename_categories.<locals>.<lambda>ABC)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_categories<   s     z)TestCategoricalAPI.test_rename_categoriesnew_categoriesr%   r&   r'      c              	   C   s<   t ddddg}d}tjt|d || 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   )r   r   r    
ValueErrorr0   r   r<   r"   r#   r   r   r   *test_rename_categories_wrong_length_raisesQ   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   r0   r   r   r7   )r   r   r9   r:   r   r   r   test_rename_categories_series[   s    z0TestCategoricalAPI.test_rename_categories_seriesc              	   C   s  t ddddg}|ddddd	}tddddg}t|j| t ddddg}|ddd
}tddddg}t|j| t ddddg}|ddddddd}tddddg}t|j| t ddddg}|ddd}tddddg}t|j| d S )Nr   r   r   dr=   r'   r&   r%   )r   r   r   rD   )r   r         )r   r   r   rD   ef)rH   g)r   r0   r   r   r   r   )r   r"   r8   r:   r   r   r   test_rename_categories_dictb   s     z.TestCategoricalAPI.test_rename_categories_dictc                 C   sd   t ddddgdd}| }t ddddgdddgdd}|dddg}t|| t|| d S Nr   r   r   Tr   r   )r   copyreorder_categoriesr   r7   r   r"   oldnewr8   r   r   r   test_reorder_categories}   s    
  z*TestCategoricalAPI.test_reorder_categoriesr   r   rD   r   c              	   C   s@   t ddddgdd}d}tjt|d || W 5 Q R X d S )Nr   r   r   Tr   z=items in new_categories are not the same as in old categoriesr   )r   r   r    r>   rM   r?   r   r   r   test_reorder_categories_raises   s    	z1TestCategoricalAPI.test_reorder_categories_raisesc                 C   s   t ddddgdd}| }t ddddgddddg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   r   r   Tr   rD   r   abcZabcderG   )
r   rL   add_categoriesr   r7   r   r   r3   r4   r   )r   r"   rO   rP   r8   r:   r   r   r   test_add_categories   s.    
 
 
z&TestCategoricalAPI.test_add_categoriesc              	   C   sH   t ddddgdd}td}tjt|d |dg W 5 Q R X d S )	Nr   r   r   rD   Tr   z5new categories must not include old categories: {'d'}r   )r   reescaper   r    r>   rT   )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dddgddd}t|| t tdddgt d}td	gt d}||}t tdddgt dtddd	gt dd}t|| d S )
Nr%   r&   ZInt64r(   r=   r   r   r   rD   )r   r   rT   r   r7   r   )r   r"   Zserr9   r:   r   r   r   ,test_add_categories_losing_dtype_information   s     
 
z?TestCategoricalAPI.test_add_categories_losing_dtype_informationc           	   	   C   s  t ddddgdd}tdddg}tjddddgtjd}|dddg}|dddg}t|j| t	|
 | tdddg}t|j| t	|
 | t ddddgdd}|dg}t	|jtjddddgtjd |ddd	g}t	|jtjdd
ddgtjd t|jtddd	g |dddd	g}tdddd	g}t|j| t d
dddd
gd
dddgdd}t	|jtjdd
dddgtjd t|jtd
dddg tjd
dddd
gtjd}t	t|| |dddd
g}t	|jtjddd
ddgtjd t|jtdddd
g tjd
dddd
gtjd}t	t|| | dksxt| d
kst|jdddd
gdd}|jrtt	t|t| |ddddd
g}|jrtt	t|t| d S )Nr   r   r   Tr   r(   r   rD   r%   r&   r'   r=   r   F)r   r   r3   r4   r6   set_categoriesr   r   r   r1   r2   codesint8_codesr5   Zasarrayminr   maxr   r   )	r   r"   Zexp_categoriesZ
exp_valuesr8   Zexp_categories_backr   expc2r   r   r   test_set_categories   sJ    "" $$z&TestCategoricalAPI.test_set_categoriesz"values, categories, new_categoriesrG   r   TFc                 C   s4   t ||}t |||}|j||d}t|| d S )Nr   )r   r\   r   r7   )r   valuesr   r<   r   r   r:   r9   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   r\   r3   nanr   r7   )r   r"   r9   r:   r   r   r   test_set_categories_rename_less0  s    z2TestCategoricalAPI.test_set_categories_rename_lessc                 C   s   t dddgddddgd}|ddddg t dddgtdd}t|| t dddgddddgd}|jddddgdd	 t dddgtdd}t|| d S )
Nr   r   r   rD   rY   rG   ZacdeT)Zfastpath)r   Z_set_categoriesr   r   r7   )r   r"   r:   r   r   r   test_set_categories_private7  s    z.TestCategoricalAPI.test_set_categories_privatec                 C   s   t ddddgdd}| }t ddtjdgddgdd}|d}t|| t|| |dg}t|| t|| d S rK   )r   rL   r3   rh   remove_categoriesr   r7   rN   r   r   r   test_remove_categoriesC  s    
z)TestCategoricalAPI.test_remove_categoriesremovalsc              	   C   s@   t dddg}td}tjt|d || W 5 Q R X d S )Nr   r   z-removals must all be in old categories: {'c'}r   )r   rV   rW   r   r    r>   rk   )r   rm   r"   messager   r   r   test_remove_categories_raisesP  s    
z0TestCategoricalAPI.test_remove_categories_raisesc           
   	   C   s  t dddddgdddddgd}tdddddg}tddddg}t|j| | }t|j| t|j| t dddtjgdddddgd}| }t|jttdddg tjddd	d
gtj	d}t
|j| t|j| dtjddddtjg}t |tdd}| }t|jtdddg tjd	d
dddd	d
gtj	d}t
|j| | |ksrttd}	tj|	d d d	 dd}tj|tjt|d< t ||	d}| }| | kstd S )Nr   r   r   rD   rG   rY   r   r%   r&   r[   r(   FDr.   ZABCDEFG)re   r   abcdefghijklmnopqrstuvwxyzi'  objectd   )r   r   r   r   r   Zremove_unused_categoriesr3   rh   r4   r^   r1   r]   r   tolistr   randomchoiceZastypelen)
r   r   Zexp_categories_allZexp_categories_droppedr8   Z	exp_codesvalr"   outalphar   r   r   test_remove_unused_categoriesX  s4      z0TestCategoricalAPI.test_remove_unused_categoriesN)__name__
__module____qualname__r   r$   r;   r   markparametrizer@   rC   rJ   rQ   rR   rU   rX   rZ   rd   rf   ri   rj   rl   r3   rh   ro   r|   r   r   r   r   r
      sZ   
	

B 
r
   c                   @   s   e Zd Zdd ZdS )TestCategoricalAPIWithFactorc                 C   sz  |  }|jsttdddgd|jd}tdddgdd	dgd
|d}t|| | }|ddddg}|  }tt	d|jdd}tddddgdd	ddgd
|d}t|| t
dddddddddddg}|  }tdddg|jdd}tdddgdddgd
|d}t|| t
tjdddg}|  }tdddgd	dd	gd
tddtjgddgddd}t|| d S )Nr   r   r   r   )namer   r'   r&   g      ?g      ?)countsZfreqsrA   rD   Zabcd)r   r   r   r%   rE   g]tE?gtE]t?g      ?)r   r   )Zdescriber   r   r   r   r   Zassert_frame_equalrL   r\   r   r   r3   rh   )r   ZfactordescZ	exp_indexr:   r"   r   r   r   test_describe}  sX    
     
  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ddddgddgdddgddddgfddd	d	gdddgddgddd
d
gfddd
gdddgdddgddd
gfddd
gdddgdgd
dd
gfddd
gdddgdgd
d
d
gfddd
gdddgg d
d
d
g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dddddgdd	}t|j| trBd
nd}tj	t
|d  tjdddddgdd	|_W 5 Q R X |j}tj	tdd d|d< W 5 Q R X d|d< tjdddddgdd	}t|j| d|jd< tjdddddgdd	}t|j| d S )Nr   r   r   r   r%   r&   r[   r^   r(   z6property 'codes' of 'Categorical' object has no setterr   r   z#assignment destination is read-onlyr=   )r   r3   rh   r4   r   r1   r]   r   r   r    r!   r>   r_   )r   r   rb   r#   r]   r   r   r   test_codes_immutable  s$    $
z.TestPrivateCategoricalAPI.test_codes_immutablezcodes, old, new, expectedr   r%   r   r   r   r&   r[   rD   c                 C   sL   t j|t jd}t j|t jd}t|}t|}t|||}t|| d S )Nr(   )r3   Z
asanyarrayr^   r   r	   r   r1   )r   r]   rO   rP   r:   r9   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%   r[   r(   )r3   Zaranger   Zint16r	   r   r1   )r   Nr]   rO   r:   rP   r9   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   )rV   Znumpyr3   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      j2