
    OwgM                         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)PY311)CategoricalCategoricalIndex	DataFrameIndexSeriesStringDtype)recode_for_categoriesc                      e Zd 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      ej                  j                  dddg      d                Zd! Zd" Zd# Zej                  j                  d$d%gd%ej0                  gd%d%d%gg      d&        Zd' Zy())TestCategoricalAPIc                     t        t        d      d      }d}t        j                  t        |      5  |j                          d d d        y # 1 sw Y   y xY w)NacbForderedz5Categorical.to_list is deprecated and will be removedmatch)r   listtmassert_produces_warningFutureWarningto_list)selfcat1msgs      _/var/www/horilla/myenv/lib/python3.12/site-packages/pandas/tests/arrays/categorical/test_api.pytest_to_list_deprecatedz*TestCategoricalAPI.test_to_list_deprecated   sE    4;6E''SA 	LLN	 	 	s   AAc                    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 )	Nr   Fr   abcbca
categoriesr   )r    r!   r   T)r   r   r   assert_index_equalr$   r   r   )r   r   cat2cat3cat4s        r   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 t
        rdn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can't set attributer   )	r   as_unorderedr   
as_orderedset_orderedr   pytestraisesAttributeError)r   catr&   r   s       r   test_set_orderedz#TestCategoricalAPI.test_set_ordered.   s    .=!<<~~|||%----##E*2222
  G& 	
 ]]>5 	CK	]]>5 	 CK	  	 	 		  	 s   +C'C3'C03C<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,            )r8   r9   r:   r8   dtyper   c                 "    | j                         S )N)upper)xs    r   <lambda>z;TestCategoricalAPI.test_rename_categories.<locals>.<lambda>T   s     r*   )ABCrA   )r   rename_categoriesr   assert_numpy_array_equal	__array__nparrayint64r%   r$   r   object_assert_categorical_equal)r   r4   resexp_catresultexpecteds         r   test_rename_categoriesz)TestCategoricalAPI.test_rename_categoriesC   s    ./ ##I.
##MMORXXl"((C	
 	cnneI.>?((/rzzB
##CMMOW=(
cnng6 &&':;34
##FH5r*   new_categoriesr8   r9   r:      r8   r9   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   r1   r2   
ValueErrorrD   r   rQ   r4   r   s       r   *test_rename_categories_wrong_length_raisesz=TestCategoricalAPI.test_rename_categories_wrong_length_raisesX   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   r8   index)r   rD   r   r   rK   )r   r!   rN   rO   s       r   test_rename_categories_seriesz0TestCategoricalAPI.test_rename_categories_seriesb   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!   drS   r:   r9   r8   rS   r:   r9   r8   )r   r!   )r8   r    r:   r^         )r   r    r!   r^   efrR   )rc   g)r   rD   r   r   r%   r$   )r   r4   rL   rO   s       r   test_rename_categories_dictz.TestCategoricalAPI.test_rename_categories_dicti   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   rK   r   r4   oldnewrL   s        r   test_reorder_categoriesz*TestCategoricalAPI.test_reorder_categories   s]    .=hhj _d
 $$_5
##C-
##C-r*   r   r   r    r^   r]   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   r1   r2   rU   ri   rV   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   r]   r#   r^   abcabcderb   )
r   rh   add_categoriesr   rK   r   r   rG   rH   r   )r   r4   rk   rl   rL   rO   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)Nr]   Tr   z5new categories must not include old categories: {'d'}r   r^   )r   reescaper1   r2   rU   rt   )r   r4   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 )Nr8   r9   Int64r;   rS   )r8   r9   rS   r$   r   r    r   r^   rn   )r   r   rt   r   rK   r	   )r   r4   serrN   rO   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   rg   r;   r   r   )r   r   r   rn   )r   r8   r   r   r]   )r8   r9   r:   rS   r8   rR   r#   )r   r8   r9   r:   r   r_   )r:   r9   r8   r   r:   rS   r8   F)r   r   rG   rH   rJ   set_categoriesr   r%   r$   rE   rF   codesint8_codesrI   asarrayminmaxr   r0   )	r   r4   exp_categories
exp_valuesrL   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_categoriesr}   r    )r    r   r   r   )r    r   r!   r^   rb   r   TFc                     t        ||      }t        |||      }|j                  ||      }t        j                  ||       y )Nr   )r   r   r   rK   )r   valuesr$   rQ   r   r!   rO   rN   s           r   test_set_categories_manyz+TestCategoricalAPI.test_set_categories_many  sC    6 
+v~w?!!.'!B
##FH5r*   c                     t        ddg      }|j                  dgd      }t        dt        j                  g      }t	        j
                  ||       y )NrA   rB   T)rename)r   r   rG   nanr   rK   )r   r4   rN   rO   s       r   test_set_categories_rename_lessz2TestCategoricalAPI.test_set_categories_rename_less7  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   r]   r|   )r   r!   r^   rb   )r   r!   r^   acdeT)fastpath)r   _set_categoriesr   r   rK   )r   r4   rO   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   rh   rG   r   remove_categoriesr   rK   rj   s        r   test_remove_categoriesz)TestCategoricalAPI.test_remove_categoriesJ  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)Nr}   z-removals must all be in old categories: {'c'}r   )r   rw   rx   r1   r2   rU   r   )r   r   r4   messages       r   test_remove_categories_raisesz0TestCategoricalAPI.test_remove_categories_raisesW  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!   r^   r   )r   r    r!   r^   rb   r|   r]   r   r    r!   r   )r   r8   r9   r   r;   FDrB   ABCDEFG)r   r$   )rB   r   r   )r9   r   r8   r   r8   r9   r   abcdefghijklmnopqrstuvwxyzr9   i'  objectd   )r   r   r   r%   r$   remove_unused_categoriesrG   r   rH   r   rE   r   r   tolistrandomdefault_rngchoiceastypelen)
r   r!   exp_categories_allexp_categories_droppedrL   	exp_codesvalr4   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)   r5   rP   r1   markparametrizerW   r[   re   rm   rp   ru   ry   r   r   r   r   r   r   rG   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 [[Yu66 734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   )r:   r9   r:   )      ?      ?r   )countsfreqsrY   r]   abcd)r   r   )r:   r9   r:   r   )r   r   r   r   )r8   r9   r:   r8   r9   r:   r:   r9   r8   r8   r8   r7   )r`   r:   r:   )g]tE?tE]t?r   r8   r9   )r8   r9   r8   )r   g      ?r   )r$   r   )r   describer   r   r   r   assert_frame_equalrh   r   r   rG   r   )r   factordesc	exp_indexrO   r4   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                  |       t        rdn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   r8   r9   r   r   r   r;   z6property 'codes' of 'Categorical' object has no setterr-   r   )r   r8   r9   r   r8   z#assignment destination is read-onlyr8   rS   )r   r8   r9   r   r   r9   )r   r8   r9   r   r9   )r   rG   r   rH   r   rE   r   r   r1   r2   r3   rU   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
  E& 	
 ]]>5 	>hhf=AG	> ]]:-RS 	E!H	
 !hhf5
##AGGS1hhf5
##AGGS1!	> 	>	 	s   =E E, E),E5zcodes, old, new, expectedr   r8   r   r    )r   r8   r   r8   r   )r   r8   r9   r9   )r   r8   r   r   )r   r8   r   )r   r   r   r^   )r   r   r   r   c                 
   t        j                  |t         j                        }t        j                  |t         j                        }t        |      }t        |      }t	        |||      }t        j                  ||       y )Nr;   )rG   
asanyarrayr   r   r
   r   rE   )r   r   rk   rl   rO   rN   s         r   test_recode_to_categoriesz3TestPrivateCategoricalAPI.test_recode_to_categories  s\    $ e2773==9CjCj&uc37
##FH5r*   c                     d}t        j                  |      }t        |      }t        j                  |dz
  ddt         j                        }t        |      }t	        |||      }t        j                  ||       y )Ni  r8   r   r;   )rG   aranger   int16r
   r   rE   )r   Nr   rk   rO   rl   rN   s          r   test_recode_to_categories_largez9TestPrivateCategoricalAPI.test_recode_to_categories_large  s`    		!El99QUB"((;Ho&uc37
##FH5r*   N)	r   r   r   r   r1   r   r   r   r   r   r*   r   r   r     s/   2< [[#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   )rw   numpyrG   r1   pandas.compatr   pandasr   r   r   r   r   r	   pandas._testing_testingr   pandas.core.arrays.categoricalr
   r   r   r   r   r*   r   <module>r      sE    	      @l, l,^0. 0.f?6 ?6r*   