U
    ckX                     @   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 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)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-g7fro6k3/pandas/tests/arrays/categorical/test_api.pytest_ordered_api   s    


z#TestCategoricalAPI.test_ordered_apic              	   C   st  d}t ddddgdd}| }|jr*t| }|js<ttjt|d |jdd W 5 Q R X |jrlttjt|d |jdd W 5 Q R X |jst|djst|d	jrttjt|d |jddd W 5 Q R X |jsttjt|d |jd	dd W 5 Q R X |jr"t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 )NzThe `inplace` parameter in pandas.Categorical.set_ordered is deprecated and will be removed in a future version. setting ordered-ness on categories will always return a new Categorical objectr   r   r   Tr
   matchZinplaceFz8property 'ordered' of 'Categorical' object has no settercan't set attribute)r   Zas_unorderedr   r   Z
as_orderedr   assert_produces_warningFutureWarningset_orderedr   pytestraisesAttributeError)r   msgcatr   r   r   r   test_set_ordered&   s:    




z#TestCategoricalAPI.test_set_orderedc              	   C   sB  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|| tt |jdddgdd}W 5 Q R X |d kstt| tjddddgtjd t|j	t
dddg d S )Nr   r   r            Zdtypec                 S   s   |   S )N)upper)xr   r   r   <lambda>]       z;TestCategoricalAPI.test_rename_categories.<locals>.<lambda>ABCTr   )r   rename_categoriesr   assert_numpy_array_equal	__array__nparrayint64r   r   r   object_assert_categorical_equalr   r   r   )r   r%   resZexp_catresultexpectedr   r   r   test_rename_categoriesL   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"   
ValueErrorr2   r   r>   r%   r$   r   r   r   *test_rename_categories_wrong_length_raisesl   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   r2   r   r   r9   )r   r   r;   r<   r   r   r   test_rename_categories_seriesv   s    z0TestCategoricalAPI.test_rename_categories_seriesc              	   C   sP  t ddddg}|ddddd	}tddddg}t|j| tt |jddddd	d
d}W 5 Q R X |d ks|t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   rF   Tr   )r   r         )r   r   r   rF   ef)rJ   g)	r   r2   r   r   r   r   r   r   r   )r   r%   r:   r<   r   r   r   test_rename_categories_dict}   s(    "z.TestCategoricalAPI.test_rename_categories_dictc              	   C   s   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|| tt |jdddgdd}W 5 Q R X |d kstt|| d S Nr   r   r   Tr
   r   r   )r   copyreorder_categoriesr   r9   r   r   r   r   r%   oldnewr:   r   r   r   test_reorder_categories   s    
  z*TestCategoricalAPI.test_reorder_categoriesr   r   rF   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@   rO   rA   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 |jddd}W 5 Q R X t|| |d kst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
   rF   r   r   abcZabcderI   )r   rN   add_categoriesr   r9   r   r   r   r   r   r5   r6   r   )r   r%   rQ   rR   r:   r<   r   r   r   test_add_categories   s6    
 
 
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   rF   Tr
   z5new categories must not include old categories: {'d'}r   )r   reescaper!   r"   r@   rV   )r   r%   r$   r   r   r   #test_add_categories_existing_raises   s    
z6TestCategoricalAPI.test_add_categories_existing_raisesc           	   	   C   sH  t ddddgdd}tdddg}tjddddgtjd}tt |jdddgdd}W 5 Q R X t	|j
| t| | |d kst|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st| dkst|jddddgdd}|jrttt|t| |dddddg}|jr,ttt|t| d S )Nr   r   r   Tr
   r*   r   r   rF   r'   r(   r)   r?   r   F)r   r   r5   r6   r8   r   r   r   set_categoriesr   r   r3   r4   r   codesint8_codesr7   Zasarrayminmaxr   r    )	r   r%   Zexp_categoriesZ
exp_valuesr:   Zexp_categories_backr   expc2r   r   r   test_set_categories   sR    "" $$z&TestCategoricalAPI.test_set_categoriesc              	   C   s6   t ddddgdd}tt |  W 5 Q R X d S )Nr   r   r   Tr
   )r   r   r   r   Zto_dense)r   r%   r   r   r   test_to_dense_deprecated9  s    z+TestCategoricalAPI.test_to_dense_deprecatedz"values, categories, new_categoriesrI   r   TFc                 C   s4   t ||}t |||}|j||d}t|| d S )Nr
   )r   r\   r   r9   )r   valuesr   r>   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 )Nr/   r0   T)rename)r   r\   r5   nanr   r9   )r   r%   r;   r<   r   r   r   test_set_categories_rename_less_  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   rF   r   rI   ZacdeT)Zfastpath)r   Z_set_categoriesr   r   r9   )r   r%   r<   r   r   r   test_set_categories_privatef  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|| tt |jddd}W 5 Q R X t|| |d kst	d S rM   )
r   rN   r5   ri   remove_categoriesr   r9   r   r   r   rP   r   r   r   test_remove_categoriesr  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   rX   rY   r!   r"   r@   rm   )r   ro   r%   messager   r   r   test_remove_categories_raises  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t |jdd}W 5 Q R X t|j| |d kst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sttd}	t	j|	d d d dd}t	j
|t	jt|d< t ||	d}| }| | kstd S )Nr   r   r   rF   rI   rk   Tr   r   r'   r(   r[   r*   FDr0   ZABCDEFG)rf   r   abcdefghijklmnopqrstuvwxyzi'  objectd   )r   r   r   r   r   Zremove_unused_categoriesr   r   r   r5   ri   r6   r^   r3   r]   r   tolistrandomchoiceZastypelen)
r   r   Zexp_categories_allZexp_categories_droppedr:   Z	exp_codesvalr%   outalphar   r   r   test_remove_unused_categories  s<      z0TestCategoricalAPI.test_remove_unused_categoriesN)__name__
__module____qualname__r   r&   r=   r!   markparametrizerB   rE   rL   rS   rT   rW   rZ   rd   re   rg   rj   rl   rn   r5   ri   rq   r~   r   r   r   r   r	      sZ   & 
	#

%I 
r	   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestCategoricalAPIWithFactorc                 C   s  |  }|jsttdddgd|jd}tdddgdd	dgd
|d}t|| | }tt	 |j
ddddgdd W 5 Q R X |  }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freqsrC   rF   Tr   Zabcd)r   r   r   r'   rG   g]tE?gtE]t?g      ?)r   r   )Zdescriber   r   r   r   r   Zassert_frame_equalrN   r   r   r\   r   r   r5   ri   )r   factordescZ	exp_indexr<   r%   r   r   r   test_describe  sZ    
      
  z*TestCategoricalAPIWithFactor.test_describec              	   C   sR   |  }tt |jddddgdd W 5 Q R X t|jtddddg d S )Nr   r   r   rF   Tr   )rN   r   r   r   r\   r   r   r   )r   r   r%   r   r   r   test_set_categories_inplace  s     z8TestCategoricalAPIWithFactor.test_set_categories_inplacec                 C   sV   t dddddddddddg}|jd }tt ||_W 5 Q R X |j|ksRtd S )Nr'   r(   r)   )r   r_   r   r   r   r   )r   r%   Z	new_codesr   r   r   test_codes_setter_deprecated  s
    
z9TestCategoricalAPIWithFactor.test_codes_setter_deprecatedN)r   r   r   r   r   r   r   r   r   r   r     s   4	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   r5   ri   r6   r   r3   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[   rF   c                 C   sL   t j|t jd}t j|t jd}t|}t|}t|||}t|| d S )Nr*   )r5   Z
asanyarrayr^   r   r   r   r3   )r   r]   rQ   rR   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'   r[   r*   )r5   Zaranger   Zint16r   r   r3   )r   Nr]   rQ   r<   rR   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   )rX   Znumpyr5   r!   Zpandas.compatr   Zpandasr   r   r   r   r   Zpandas._testingZ_testingr   Zpandas.core.arrays.categoricalr   r	   r   r   r   r   r   r   <module>   s      +H