U
    MfU                     @   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	 d dl
mZ d dlmZ d dlmZ G dd dZG dd deZG d	d
 d
ZdS )    N)CategoricalCategoricalIndex	DataFrameIndexSeries)recode_for_categories)TestCategoricalc                   @   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-eb6vo0j3/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|jdd |jrNt|jdd |jsdt|djstt|djrt|jddd |jst|jddd |jrt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
   ZinplaceFcan't set attributematch)	r   Zas_unorderedr   r   Z
as_orderedset_orderedpytestraisesAttributeError)r   catr   msgr   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>P       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_equalassert_produces_warningFutureWarningr   )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_raises_   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   r;   r<   r   r   r   test_rename_categories_seriesi   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   r0   r   r   r   r   r8   r9   r   )r   r"   r:   r<   r   r   r   test_rename_categories_dictp   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   r7   r8   r9   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   r7   r8   r9   r   r   r   r3   r4   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   r3   r4   r6   r   r8   r9   set_categoriesr   r   r1   r2   r   codesint8_codesr5   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   r8   r9   Zto_denser   r"   r   r   r   test_to_dense_deprecated,  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   r7   )r   valuesr   r>   r   r   r<   r;   r   r   r   test_set_categories_many2  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"   r;   r<   r   r   r   test_set_categories_rename_lessR  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   r7   )r   r"   r<   r   r   r   test_set_categories_privateY  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   r3   rj   remove_categoriesr   r7   r8   r9   r   rP   r   r   r   test_remove_categoriese  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@   rn   )r   rp   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   rl   Tr   r   r%   r&   r[   r(   FDr.   ZABCDEFG)rg   r   abcdefghijklmnopqrstuvwxyzi'  objectd   )r   r   r   r   r   Zremove_unused_categoriesr8   r9   r   r3   rj   r4   r^   r1   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   rf   rh   rk   rm   ro   r3   rj   rr   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  }| j jsttdddgd| j jd}tdddgdd	dgd
|d}t|| | j  }t	t
 |jddddgdd W 5 Q R X | }ttd| j 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   )factorZdescriber   r   r   r   r   Zassert_frame_equalrN   r8   r9   r\   r   r   r3   rj   )r   descZ	exp_indexr<   r"   r   r   r   test_describe  sZ    
   
   
  z*TestCategoricalAPIWithFactor.test_describec              	   C   sT   | j  }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   )	r   rN   r   r8   r9   r\   r   r   r   re   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   r8   r9   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jt	d
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(   r   r   z#assignment destination is read-onlyr?   )r   r3   rj   r4   r   r1   r]   r   r    r!   r@   r_   )r   r   rb   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(   )r3   Z
asanyarrayr^   r   r   r   r1   )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(   )r3   Zaranger   Zint16r   r   r1   )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numpyr3   r   Zpandasr   r   r   r   r   Zpandas._testingZ_testingr   Zpandas.core.arrays.categoricalr   Z&pandas.tests.arrays.categorical.commonr   r	   r   r   r   r   r   r   <module>   s      H