
    iBM                         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      Z G d d      Z G d	 d
      Zy)    N)Pandas4Warning)CategoricalCategoricalIndex	DataFrameIndexSeriesStringDtype)recode_for_categoriesc                      e Zd Zd Zd Zd Zej                  j                  dg dddgg      d        Z	d	 Z
d
 Zd Zej                  j                  ddgg dg dg      d        Zd Zd Zd Zd Zej                  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      d        Zd Zd Zd Zej                  j                  d d!gd!ej.                  gd!d!d!gg      d"        Zd# Zy$)%TestCategoricalAPIc                    t        t        d      d      }t        j                  |j                  t        g d             |j                  rJ t        t        d      t        d      d      }t        j                  |j                  t        g d             |j                  rJ t        t        d      d      }t        j                  |j                  t        g d             |j                  sJ t        t        d      t        d      d      }t        j                  |j                  t        g d             |j                  sJ y )	NacbForderedabcbca
categoriesr   )r   r   r   T)r   listtmassert_index_equalr   r   r   )selfcat1cat2cat3cat4s        e/app/cer_product_mecsu/.venv/lib/python3.12/site-packages/pandas/tests/arrays/categorical/test_api.pytest_ordered_apiz#TestCategoricalAPI.test_ordered_api   s    4;6
doou_/EF<<4;4;N
doou_/EF<<4;5
doou_/EF|||4;4;M
doou_/EF|||    c                    t        g dd      }|j                         }|j                  rJ |j                         }|j                  sJ |j	                  d      j                  sJ |j	                  d      j                  rJ d}t        j                  t        |      5  d|_        d d d        t        j                  t        |      5  d|_        d d d        y # 1 sw Y   5xY w# 1 sw Y   y xY w)Nr   r   r   r   Tr   Fz8property 'ordered' of 'Categorical' object has no settermatch)r   as_unorderedr   
as_orderedset_orderedpytestraisesAttributeError)r   catr   msgs       r    test_set_orderedz#TestCategoricalAPI.test_set_ordered'   s    .=!<<~~|||%----##E*2222 I]]>5 	CK	]]>5 	 CK	  	 	 		  	 s   #CC+C(+C4c                    t        g d      }|j                  g d      }t        j                  |j	                         t        j                  g dt
        j                               t        j                  |j                  t        g d             t        j                  g dt
        j                        }t        j                  |j	                         |       t        g d      }t        j                  |j                  |       |j                  d       }t        g d      }t        j                  ||       y )Nr$            )r2   r3   r4   r2   dtyper   c                 "    | j                         S )N)upper)xs    r    <lambda>z;TestCategoricalAPI.test_rename_categories.<locals>.<lambda>I   s     r"   )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expecteds         r    test_rename_categoriesz)TestCategoricalAPI.test_rename_categories8   s    ./ ##I.
##MMORXXl"((C	
 	cnneI.>?((/rzzB
##CMMOW=(
cnng6 &&':;34
##FH5r"   new_categoriesr2   r3   r4      r2   r3   c                     t        g d      }d}t        j                  t        |      5  |j	                  |       d d d        y # 1 sw Y   y xY w)Nr$   zKnew categories need to have the same number of items as the old categories!r%   )r   r*   r+   
ValueErrorr>   r   rK   r-   r.   s       r    *test_rename_categories_wrong_length_raisesz=TestCategoricalAPI.test_rename_categories_wrong_length_raisesM   sJ    ./ 	 ]]:S1 	2!!.1	2 	2 	2s   AAc                     t        ddg      }|j                  t        ddgddg            }t        ddg      }t        j                  ||       y )Nr   r   r   r2   index)r   r>   r   r   rE   )r   r   rH   rI   s       r    test_rename_categories_seriesz0TestCategoricalAPI.test_rename_categories_seriesW   sM    c
#$$VQF3*%EF1v&
##FH5r"   c           	         t        g d      }|j                  ddddd      }t        g d      }t        j                  |j
                  |       t        g d      }|j                  ddd      }t        g d      }t        j                  |j
                  |       t        g d      }|j                  ddddd	d
d      }t        g d      }t        j                  |j
                  |       t        g d      }|j                  ddd      }t        g d      }t        j                  |j
                  |       y )Nr   r   r   drM   r4   r3   r2   rM   r4   r3   r2   )r   r   )r2   r   r4   rX         )r   r   r   rX   efrL   )r]   g)r   r>   r   r   r   r   )r   r-   rF   rI   s       r    test_rename_categories_dictz.TestCategoricalAPI.test_rename_categories_dict^   s   ./##!!!!$DE&
cnnh7 ./##!!$45)*
cnnh7 ./##!!!!!RS$TU&
cnnh7 ./##!!$45-.
cnnh7r"   c                     t        g dd      }|j                         }t        g dg dd      }|j                  g d      }t        j                  ||       t        j                  ||       y )Nr$   Tr   r   r   r   r   )r   copyreorder_categoriesr   rE   r   r-   oldnewrF   s        r    test_reorder_categoriesz*TestCategoricalAPI.test_reorder_categoriesy   s]    .=hhj _d
 $$_5
##C-
##C-r"   r   r   r   rX   rW   c                     t        g dd      }d}t        j                  t        |      5  |j	                  |       d d d        y # 1 sw Y   y xY w)Nr$   Tr   z=items in new_categories are not the same as in old categoriesr%   )r   r*   r+   rO   rc   rP   s       r    test_reorder_categories_raisesz1TestCategoricalAPI.test_reorder_categories_raises   sG     .=M]]:S1 	3"">2	3 	3 	3s   AAc                 Z   t        g dd      }|j                         }t        g dg dd      }|j                  d      }t        j                  ||       t        j                  ||       |j                  dg      }t        j                  ||       t        j                  ||       t        t        d      d      }t        t        d      t        d      d      }|j                  t        dd	g            }t        j                  ||       |j                  t        j                  dd	g            }t        j                  ||       |j                  t        dd	g            }t        j                  ||       |j                  dd	g      }t        j                  ||       y )
Nr$   Tr   rW   r   rX   abcabcder\   )
r   rb   add_categoriesr   rE   r   r   rA   rB   r   )r   r-   re   rf   rF   rI   s         r    test_add_categoriesz&TestCategoricalAPI.test_add_categories   s[   .=hhj -A4
   %
##C-
##C-  #'
##C-
##C- $u+t4tE{tG}dS  c
!34
##C2  3*!56
##C2  Sz!23
##C2  #s,
##C2r"   c                     t        g dd      }t        j                  d      }t        j                  t
        |      5  |j                  dg       d d d        y # 1 sw Y   y xY w)NrW   Tr   z5new categories must not include old categories: {'d'}r%   rX   )r   reescaper*   r+   rO   rn   )r   r-   r.   s      r    #test_add_categories_existing_raisesz6TestCategoricalAPI.test_add_categories_existing_raises   sQ    .=iiOP]]:S1 	&u%	& 	& 	&s    AA%c                    t        t        ddgd            }t        dgd      }|j                  |      }t        t        ddgd      t        g dd            }t        j                  ||       t        t        g dt                           }t        d	gt                     }|j                  |      }t        t        g dt                     t        g d
t                           }t        j                  ||       y )Nr2   r3   Int64r5   rM   )r2   r3   rM   r   r   r   r   rX   rh   )r   r   rn   r   rE   r	   )r   r-   serrH   rI   s        r    ,test_add_categories_losing_dtype_informationz?TestCategoricalAPI.test_add_categories_losing_dtype_information   s    &!Qw78aS(##C(Aq6)fYg6V
 	##FH5&FGcU+-0##C(?+-8o[]C
 	##FH5r"   c                 	   t        g dd      }t        g d      }t        j                  g dt        j                        }|j                  g d      }|j                  g d      }t        j                  |j                  |       t        j                  |j                         |       t        g d      }t        j                  |j                  |       t        j                  |j                         |       t        g dd      }|j                  dg      }t        j                  |j                  t        j                  g dt        j                               |j                  g d	      }t        j                  |j                  t        j                  g d
t        j                               t        j                  |j                  t        g d	             |j                  g d      }t        g d      }t        j                  |j                  |       t        g dg dd      }t        j                  |j                  t        j                  g dt        j                               t        j                  |j                  t        g d             t        j                  g dt        j                        }t        j                  t        j                  |      |       |j                  g d      }t        j                  |j                  t        j                  g dt        j                               t        j                  |j                  t        g d             t        j                  g dt        j                        }t        j                  t        j                  |      |       |j!                         dk(  sJ |j#                         dk(  sJ |j                  g dd      }|j$                  rJ t        j                  t        j                  |      t        j                  |             |j'                  d      j                  g d      }|j$                  rJ t        j                  t        j                  |      t        j                  |             y )Nr$   Tr   ra   r5   r   r   )r   r{   r   rh   )r   r2   r{   r   rW   )r2   r3   r4   rM   r2   rL   r   )r   r2   r3   r4   r   rY   )r4   r3   r2   r   r4   rM   r2   F)r   r   rA   rB   rD   set_categoriesr   r   r   r?   r@   codesint8_codesrC   asarrayminmaxr   r)   )	r   r-   exp_categories
exp_valuesrF   exp_categories_backr   expc2s	            r    test_set_categoriesz&TestCategoricalAPI.test_set_categories   s   .=/XX2"**E
  1  1
cnnn=
##CMMOZ@#O4
cnn.AB
##CMMOZ@ .=  #'
##CIIrxxbgg/VW   1
##CIIrxxRWW/UV
cnneO.DE   !5634
cnnn= L$O
##AHHbhhbgg.VW
allE,,?@hhbhh7
##BJJqM37 \* 	##AHHbhhbgg.VW 	allE,,?@ hhbhh7
##BJJqM37uuw!||uuw!|| lE:::~
##BJJqM2::b>B ]]5!00>::~
##BJJqM2::b>Br"   z"values, categories, new_categoriesrw   r   )r   r   r   r   )r   r   r   rX   r\   c                    d}t        |      j                  |      rt        nd }t        j                  ||      5  t        ||      }d d d        t        |      j                  |      rt        nd }t        j                  ||      5  t        |||      }	d d d        j                  ||      }
t        j                  |
	       y # 1 sw Y   xY w# 1 sw Y   ?xY w)Nz=Constructing a Categorical with a dtype and values containingr%   r   )set
differencer   r   assert_produces_warningr   r|   rE   )r   valuesr   rK   r   r.   warn1r   warn2rI   rH   s              r    test_set_categories_manyz+TestCategoricalAPI.test_set_categories_many  s    4 N"%f+"8"8"D$''S9 	0FJ/A	0 #&f+"8"8"Hd''S9 	D"6>7CH	D !!.'!B
##FH5	0 	0	D 	Ds   C

C
CCc                     t        ddg      }|j                  dgd      }t        dt        j                  g      }t	        j
                  ||       y )Nr;   r<   T)rename)r   r|   rA   nanr   rE   )r   r-   rH   rI   s       r    test_set_categories_rename_lessz2TestCategoricalAPI.test_set_categories_rename_less3  sI    3*%##SE$#7RVV}-
##FH5r"   c                 P   t        g dg d      }|j                  g d       t        g dt        d            }t        j                  ||       t        g dg d      }|j                  g dd       t        g dt        d            }t        j                  ||       y )	Nr   rW   rv   )r   r   rX   r\   )r   r   rX   acdeT)fastpath)r   _set_categoriesr   r   rE   )r   r-   rI   s      r    test_set_categories_privatez.TestCategoricalAPI.test_set_categories_private:  s    /6JK014<H
##C2 /6JK04@4<H
##C2r"   c                 |   t        g dd      }|j                         }t        ddt        j                  dgddgd      }|j	                  d      }t        j                  ||       t        j                  ||       |j	                  dg      }t        j                  ||       t        j                  ||       y )Nr$   Tr   r   r   r   r   )r   rb   rA   r   remove_categoriesr   rE   rd   s        r    test_remove_categoriesz)TestCategoricalAPI.test_remove_categoriesF  s    .=hhj3RVVS1sCjRVW##C(
##C-
##C-##SE*
##C-
##C-r"   removalsr   c                     t        g d      }t        j                  d      }t        j                  t
        |      5  |j                  |       d d d        y # 1 sw Y   y xY w)Nrw   z-removals must all be in old categories: {'c'}r%   )r   rq   rr   r*   r+   rO   r   )r   r   r-   messages       r    test_remove_categories_raisesz0TestCategoricalAPI.test_remove_categories_raisesS  sL    /*))KL]]:W5 	,!!(+	, 	, 	,s   AA"c           	      :   t        g dg d      }t        g d      }t        g d      }t        j                  |j                  |       |j                         }t        j                  |j                  |       t        j                  |j                  |       t        dddt        j                  gg d      }|j                         }t        j                  |j                  t        t        j                  g d                   t        j                  g d	t        j                  
      }t        j                  |j                  |       t        j                  |j                  |       dt        j                  ddddt        j                  g}t        |t        d            }|j                         }t        j                  |j                  t        g d             t        j                  g dt        j                  
      }t        j                  |j                  |       |j                         |k(  sJ t        d      }	t        j                  j                  d      j!                  |	d d d   d      j#                  d      }t        j                  |t        j                  j                  d      j!                  t%        |      d      <   t        ||	      }|j                         }|j                         |j                         k(  sJ y )N)r   r   r   rX   r   )r   r   r   rX   r\   rv   rW   r   r   r   r   )r   r2   r3   r{   r5   FDr<   ABCDEFG)r   r   )r<   r   r   )r3   r{   r2   r   r2   r3   r{   abcdefghijklmnopqrstuvwxyzr3   i'  objectd   )r   r   r   r   r   remove_unused_categoriesrA   r   rB   r~   r?   r}   r   tolistrandomdefault_rngchoiceastypelen)
r   r   exp_categories_allexp_categories_droppedrF   	exp_codesvalr-   outalphas
             r    test_remove_unused_categoriesz0TestCategoricalAPI.test_remove_unused_categories[  s/   1>WX"#<=!&';!<
all,>?((*
cnn.DE
all,>? c3/<UV((*
cnneBHH_4M.NOHH]"'':	
##CIIy9
all,>?BFFCc37iA**,
cnneO.DEHH4BGGD	
##CIIy9zz|s"""12ii##A&--eCaCj%@GGQ>@ffBII!!!$++CHc:;7**,zz|szz|+++r"   N)__name__
__module____qualname__r!   r/   rJ   r*   markparametrizerQ   rU   r_   rg   rj   ro   rs   ry   r   r   r   r   r   rA   r   r   r    r"   r    r   r      s   $ "6* [[-q!f/EF2 G2686. [[E 	
3338&6&@CD [[, sCj3*5sCj3*5sCj3*5sCj3*5sCj3*5sCj3*5sCj3*5sCj3*5sCj3%0sCj3%0sCj3%0sCj3%0sCj3*5)	
263266
3. [[Z3%#rvvc3Z)PQ, R,!,r"   r   c                       e Zd Zd Zy)TestCategoricalAPIWithFactorc                    t        g dd      }|j                         }|j                  sJ t        g dd|j                        }t	        g dg dd	|
      }t        j                  ||       |j                         }|j                  g d      }|j                         }t        t        d      |j                  d      }t	        g dg dd	|
      }t        j                  ||       t        g d      }|j                         }t        g d|j                  d      }t	        g dg dd	|
      }t        j                  ||       t        t        j                  dddg      }|j                         }t	        g dg dd	t        ddt        j                  gddgd      
      }t        j                  ||       y )N)r   r   r   r   r   r   r   r   Tr   r   r   )namer   )r4   r3   r4   )      ?      ?r   )countsfreqsrS   rW   abcd)r   r   )r4   r3   r4   r   )r   r   r   r   )r2   r3   r4   r2   r3   r4   r4   r3   r2   r2   r2   r1   )rZ   r4   r4   )g]tE?tE]t?r   r2   r3   )r2   r3   r2   )r   g      ?r   )r   r   )r   describer   r   r   r   assert_frame_equalrb   r|   r   rA   r   )r   factordesc	exp_indexrI   r-   s         r    test_describez*TestCategoricalAPIWithFactor.test_describe  s   EtT ~~~$,
	  +FGy
 	dH- kkm  !56||~$L&..|
	 #.LM
 	dH- ;<||~$Y,W	 +IJ
 	dH- 2661a+,||~ +FG"ArvvAq6
 	dH-r"   N)r   r   r   r   r   r"   r    r   r     s    /.r"   r   c                      e Zd Zd Zej
                  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        Zd Zy)TestPrivateCategoricalAPIc                    t        ddddt        j                  g      }t        j                  g dd      }t	        j
                  |j                  |       d}t        j                  t        |      5  t        j                  g d	d      |_        d d d        |j                  }t        j                  t        d
      5  d|d<   d d d        d|d<   t        j                  g dd      }t	        j
                  |j                  |       d|j                  d<   t        j                  g dd      }t	        j
                  |j                  |       y # 1 sw Y   xY w# 1 sw Y   xY w)Nr   r   r   )r   r2   r3   r   r{   r~   r5   z6property 'codes' of 'Categorical' object has no setterr%   )r   r2   r3   r   r2   z#assignment destination is read-onlyr2   rM   )r   r2   r3   r   r   r3   )r   r2   r3   r   r3   )r   rA   r   rB   r   r?   r}   r*   r+   r,   rO   r   )r   r   r   r.   r}   s        r    test_codes_immutablez.TestPrivateCategoricalAPI.test_codes_immutable  s   c3RVV45hh'v6
##AGGS1 G]]>5 	>hhf=AG	> ]]:-RS 	E!H	
 !hhf5
##AGGS1hhf5
##AGGS1!	> 	>	 	s   5EE$E!$E-zcodes, old, new, expectedr   r2   r   r   )r   r2   r   r2   r   )r   r2   r3   r3   )r   r2   r{   r{   )r   r2   r{   )r{   r   r{   rX   )r{   r{   r{   r{   c                    t        j                  |t         j                        }t        j                  |t         j                        }t        |      }t        |      }t	        |||d      }t        j                  ||       y )Nr5   Trb   )rA   
asanyarrayr~   r   r
   r   r?   )r   r}   re   rf   rI   rH   s         r    test_recode_to_categoriesz3TestPrivateCategoricalAPI.test_recode_to_categories  s^    $ e2773==9CjCj&uc3TB
##FH5r"   c                     d}t        j                  |      }t        |      }t        j                  |dz
  ddt         j                        }t        |      }t	        |||d      }t        j                  ||       y )Ni  r2   r{   r5   Tr   )rA   aranger   int16r
   r   r?   )r   Nr}   re   rI   rf   rH   s          r    test_recode_to_categories_largez9TestPrivateCategoricalAPI.test_recode_to_categories_large  sb    		!El99QUB"((;Ho&uc3TB
##FH5r"   N)	r   r   r   r   r*   r   r   r   r   r   r"   r    r   r     s/   24 [[#Vc3Z#saV4Vc3Z#saV4Vc3Z#saV4Vc3Z#saV4C:E?S#JG/:F3%=3%>"l;"XrC:Bx0Vc3Z#saV4	
"6#"66r"   r   )rq   numpyrA   r*   pandas.errorsr   pandasr   r   r   r   r   r	   pandas._testing_testingr   pandas.core.arrays.categoricalr
   r   r   r   r   r"   r    <module>r      sE    	   (   @h, h,V0. 0.f;6 ;6r"   