
    ,h%J                         / S Qr SSKrSSKJr  SSKJrJrJrJ	r	J
r
JrJrJrJrJrJrJrJr  \	(       a  SSKr " S S\5      rSS jrS rS	 rS
 rS r " S S\5      rS rSS jrS rg))Cguess_categoricalCategoricalSniffercategorical_to_int    N)
PatsyError)SortAnythingKeysafe_scalar_isnaniterablehave_pandashave_pandas_categoricalhave_pandas_categorical_dtypesafe_is_pandas_categoricalpandas_Categorical_from_codespandas_Categorical_categoriespandas_Categorical_codessafe_issubdtypeno_picklingassert_no_picklingc                       \ rS rSrS r\rSrg)_CategoricalBox<   c                 (    Xl         X l        X0l        g Ndatacontrastlevels)selfr   r   r   s       dC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\patsy/categorical.py__init___CategoricalBox.__init__=   s    	     )r   r   r   N)__name__
__module____qualname____firstlineno__r    r   __getstate____static_attributes__ r"   r   r   r   <   s    
 Lr"   r   c                     [        U [        5      (       a*  Uc  U R                  nUc  U R                  nU R                  n [        XU5      $ )a  
Marks some `data` as being categorical, and specifies how to interpret
it.

This is used for three reasons:

* To explicitly mark some data as categorical. For instance, integer data
  is by default treated as numerical. If you have data that is stored
  using an integer type, but where you want patsy to treat each different
  value as a different level of a categorical factor, you can wrap it in a
  call to `C` to accomplish this. E.g., compare::

    dmatrix("a", {"a": [1, 2, 3]})
    dmatrix("C(a)", {"a": [1, 2, 3]})

* To explicitly set the levels or override the default level ordering for
  categorical data, e.g.::

    dmatrix("C(a, levels=["a2", "a1"])", balanced(a=2))
* To override the default coding scheme for categorical data. The
  `contrast` argument can be any of:

  * A :class:`ContrastMatrix` object
  * A simple 2d ndarray (which is treated the same as a ContrastMatrix
    object except that you can't specify column names)
  * An object with methods called `code_with_intercept` and
    `code_without_intercept`, like the built-in contrasts
    (:class:`Treatment`, :class:`Diff`, :class:`Poly`, etc.). See
    :ref:`categorical-coding` for more details.
  * A callable that returns one of the above.
)
isinstancer   r   r   r   r   s      r   r   r   E   sE    @ $((}}H>[[Fyy4622r"   c                  H   [        S5      n [        U [        5      (       d   eU R                  S:X  d   eU R                  b   eU R
                  b   e[        SSS5      nUR                  S:X  d   eUR
                  S:X  d   eUR                  S:X  d   e[        USS9nUR                  S:X  d   eUR
                  S:X  d   eUR                  S:X  d   e[        US5      nUR                  S:X  d   eUR
                  S:X  d   eUR                  S:X  d   e[        U5        g )NasdfDATACONTRASTLEVELSz
NEW LEVELSr   zNEW CONTRAST)r   r+   r   r   r   r   r   )c1c2c3c4s       r   test_Cr6   n   s    	
6Bb/****77f99;;	
6:x	(B77f;;*$$$99   	
2l	#B77f;;*$$$99$$$	
2~	B77f;;.(((99   rr"   c                     [        U 5      (       a  g[        U [        5      (       a  g[        R                  " U 5      n [        U R                  [        R                  5      (       a  gg)NTF)r   r+   r   npasarrayr   dtypenumberr   s    r   r   r      sI    !$''$((::dDtzz299--r"   c                     [         (       a[  [        R                  " / SQ5      n [        U 5      (       d   e[        (       a&  [        [        R
                  " U 5      5      (       d   e[        [        / SQ5      5      (       d   e[        SS/5      (       d   e[        SS/5      (       d   e[        SS[        R                  /5      (       d   e[        / SQ5      (       d   e[        / SQ5      (       a   e[        SSS	[        R                  /5      (       a   e[        / S
Q5      (       a   e[        SSS[        R                  /5      (       a   eg )N         TFabrB   rC   Nr?   r@   rA   )      ?       @      @rE   rF   rG   )	r   pandasCategoricalr   r   Seriesr   r8   nan)cs    r   test_guess_categoricalrM      s   y) ####(($V]]1%56666Qy\****dE]++++c3Z((((c3/0000-.... ++++ !Q266!23333 1111 #sC!899999r"   c                     [        U S5      (       a  U R                  S:  a  [        S5      e[        U 5      (       a  [	        U [
        [        45      (       a  U /n U $ )Nndimr?   z)categorical data cannot be >1-dimensional)hasattrrO   r   r
   r+   strbytesr<   s    r   _categorical_shape_fixrS      sN    
 tVQDEE D>>ZsEl;;vKr"   c                   .    \ rS rSrSS jrS rS r\rSr	g)r      Nc                 V    Xl         X l        S U l        S U l        [	        5       U l        g r   )
_NA_action_origin	_contrast_levelsset
_level_set)r   	NA_actionorigins      r   r    CategoricalSniffer.__init__   s#    #%r"   c                     U R                   c.  [        U R                  5      nUR                  [        S9  Xl         [        U R                   5      U R                  4$ )N)key)rZ   listr\   sortr   tuplerY   )r   r   s     r   levels_contrast"CategoricalSniffer.levels_contrast   sE    <<$//*FKKOK,!LT\\"DNN22r"   c                 8   [        US5      (       a  UR                  U l        [        U[        5      (       a4  UR
                  b  [        UR
                  5      U l        gUR                  n[        U5      (       a  [        [        U5      5      U l        g[        US5      (       a<  [        UR                  [        R                  5      (       a  [        SS/5      U l        g[#        U5      nU Hj  nU R$                  R'                  U5      (       a  M%  USL d  USL a  U R                   R)                  SS/5        MN   U R                   R+                  U5        Ml     U R                   [        SS/5      :H  $ ! [,         a    [/        SU R0                  5      ef = f)Nr   Tr:   F?Error interpreting categorical data: all items must be hashable)rP   r   rY   r+   r   r   rd   rZ   r   r   r   r   r:   r8   bool_r[   r\   rS   rW   is_categorical_NAupdateadd	TypeErrorr   rX   )r   r   values      r   sniffCategoricalSniffer.sniff   sQ   4$$!]]DNdO,,{{&$T[[1 yy%d++ !!>t!DEDL 4!!odjj"((&K&K!4-0DO%d+E0077}&&e}5OO''.   #tUm"444 ! $5 s   ?E88!F)rW   rY   r\   rZ   rX   r   )
r#   r$   r%   r&   r    re   ro   r   r'   r(   r)   r"   r   r   r      s     3(5T Lr"   r   c                    ^ SSK Jm  S*U4S jjn [        (       a  S [        /n[        (       a  U[
        R                  S /-  nU H  nU " / U" [
        R                  " / SQ5      5      /SS5        U " / U" [        S	S/S
S/5      5      /SS5        U " / U" [        S	S/SS
/5      5      /SS5        U" [
        R                  " S
S/5      5      nSUl	        U " / U/SSS5        M     U " / [        S	S/5      [        SS/5      /SS5        U " / [        S	S// SQS9[        SS/5      /SS5        U " / [        S	S// SQS9[        SS/5      /SS5        U " SS/[        S	[        R                  /5      [        SS /5      /SS5        [        T" S/S95      nUR                  [        S	[        R                  S /5      5        UR                  5       u  pV[        U5      [        S S	/5      :X  d   eU " SS/[        S[        R                  S /5      /SS5        U " / [        SS/5      [        S/5      [        SS/5      /SS5        U " / [        R                   " SS/5      S //SS5        U " SS/[        S!S S"[        R                  S#/5      /SS$5        U " / [        SS/S%S&9/SS'S%5        U " / SS/S//SS(5        U " / SS
/S
//SS5        U " / S/SS)5        SS Kn[        T" 5       5      nUR%                  [&        UR                  0 /5        UR%                  [&        UR                  [        R                   " S//5      5        g )+Nr   NAActionc                    > [        T" U S95      nU H/  nUR                  U5      nU(       a  U(       d   e  OU(       d  M/   e   UR                  5       X44:X  d   eg )NNA_types)r   ro   re   )	rv   datasexp_finish_fast
exp_levelsexp_contrastsnifferr   doners   s	           r   t"test_CategoricalSniffer.<locals>.t   s_    $Xx%@AD==&D&&*?**  &&(Z,FFFFr"   c                     U $ r   r)   xs    r   <lambda>)test_CategoricalSniffer.<locals>.<lambda>   s    1r"   c                 @    [        [        R                  " U 5      5      $ r   )r   rH   rJ   r   s    r   r   r      s    qq1A/Br"   )r?   r@   NT)r?   r@   r?   rB   rC   rB   rC   rC   rB   r/   r@   rA   Fr>   r1      )rA   r@   r?   NoneNaN
   )r?   r   ru   FT      (   )FTr   r   r   r   foorC   r@   rB   r?   rL   N)r   r   r   FOO)r   )r   r   r   r   r   )rC   r   )patsy.missingrs   r   r   r   rH   rJ   rI   r   r   r8   rK   r   ro   re   r[   r9   pytestraisesr   )	r}   prepsprepobjr{   r   _r   rs   s	           @r   test_CategoricalSnifferr      s@   &	G  a ((fmm%BCCEDb4**<89:D&I3QFS#JGHI	 3QFS#JGHI	 v))3*56C%CLb3%z:6% ( b1aV9aAi
 %3b1aVI&1a&	
2D)Db1aVI&1a&	
2D)D vuo1bff+2t*6wG E7!;<GMM!Q%&'
 '')IFv;#tQi.((( vuo4./0$Fb1b"X;5'
Ar2hK
0%9VWb2::tUm$ug
.mD 		
HdHbffk:	;<)	 b1b"X&
'%@ bB8bT
E<0bC:u
uj1 b3% !,G
MM*gmmbT2 MM*gmmRZZ#-@Ar"   c           
      .   [        U[        5      (       d   e[        U 5      (       a8  [        [        U 5      5      nXA:X  d  [	        SU< SU< 3U5      e[        U 5      $ [        U [        5      (       aY  U R                  b@  [        U R                  5      U:w  a'  [	        SU< S[        U R                  5      < 3U5      eU R                  n [        U 5      n  [        [        U[        [        U5      5      5      5      n[        U S5      (       aZ  [!        U R"                  [$        R&                  5      (       a1  US   S:X  a(  US   S:X  a  U R)                  [$        R*                  5      $ [$        R,                  " [        U 5      [.        S	9n[1        U 5       H(  u  pxUR3                  U5      (       a  S
Xg'   M!   XX   Xg'   M*     [<        (       a=  [        U [>        R@                  5      (       a  [>        R@                  " X`RB                  S9nU$ ! [         a    [	        SU5      ef = f! [4         a    Sn	/ n
[        U5      U	::  a%  X Vs/ s H  n[7        U5      PM     Os  snf sn-  n
OfXS U	S-    Vs/ s H  n[7        U5      PM     Os  snf sn-  n
U
R9                  S5        XU	* S-  S   Vs/ s H  n[7        U5      PM     Os  snf sn-  n
SSR;                  U
5      -  n[	        SU< SU< S3U5      e[         a    [	        SU< 3U5      ef = f)Nzmismatching levels: expected z, got rh   r:   Fr   Tr?   r:   r   r@   z...z[%s]z, z=Error converting data to categorical: observation with value z6 does not match any of the expected levels (expected: )zCError converting data to categorical: encountered unhashable value index)"r+   rd   r   r   r   r   r   r   r   rS   dictziprangelenrm   rP   r   r:   r8   ri   astypeint_emptyint	enumeraterj   KeyErrorreprappendjoinr   rH   rJ   r   )r   r   r]   r^   data_levels_tuplelevel_to_intoutirn   SHOW_LEVELS
level_strslevel	level_strs                r   r   r   G  s   fe$$$$ "$''!"?"EF *=CEVW  (--$((;;"uT[['9V'C5-/ 
 yy!$'D
Cc&k(:;< tW/$**bhh"G"G!#T(:a(?;;rww''
((3t9C
(CdO&&u--CF%, $8 {z$66mmCzz2JS  
PRX
 	

"  
v;+-F"CF54;F"CCJCU[TUEU<V"W<V54;<V"WWJ%%e,K<STCTCV<W"X<W54;<W"XXJ"dii
&;<	  DI)U 	    9>A s=   'H ;H)H&)!L
I 
L5J

'L1K
ALc            	      @	   SS K n SSKJn  [        (       a  [        R
                  " / SQ/ SQS9n[        USU" 5       5      n[        R                  " U/ SQ:H  5      (       d   e[        R                  " UR                  / SQ:H  5      (       d   eU R                  [        [        [        R                  " SU05      SU" 5       5        [        (       Ga  [        /n[        (       a  S nUR!                  U5        U H  nU" / S	QS
5      n[        US
U" 5       5      n[        R                  " U/ S	Q:H  5      (       d   eU" / S	QS5      n	[        US
U" S/S95      n
[        R                  " U
/ S	Q:H  5      (       d   eU R                  [        [        U" SS/S
5      SU" 5       5        U R                  [        [        U" SS/S
5      SU" 5       5        M     U" 5       4S jnU" / SQS
/ SQ5        U" [        R"                  " / SQ5      S
/ SQ5        U" [        R"                  " / SQ[$        S9S
/ SQ5        U" / SQS/ SQ5        U" [        R"                  " / SQ5      S/ SQ5        U" [        R"                  " / SQ[&        S9S/ SQ5        U" [        R"                  " / SQ[$        S9S/ SQ5        U" / SQS/ SQ5        U" / SQS/ SQ5        U R                  [        [        / SQSU" 5       5        U" [)        / SQ5      S
/ SQ5        U" [)        / SQ5      S/ SQ5        U" [)        / SQSS/S9S/ SQ5        U R                  [        [        [)        / SQSS/S9SU" 5       5        U" SS
S/5        U" SS
S/5        U" SS S/5        U R                  [        [        [        R"                  " SS/SS//5      S
U" 5       5        U R                  [        [        SS/SS0 4U" 5       5        U R                  [        [        SS0 /S
U" 5       5        U" SS [        R*                  S/S
/ S!QU" SS"/S95        U" SS [        R*                  S/S#/ S!QU" SS"/S95        U" SS [        R*                  S/S#/ S$QU" S"/S95        U R                  [        [        / S%QS&U" 5       5        g )'Nr   rr   )rB   rC   rL   r   r   )r   r?   r@   r   c                 D    [        X5      n[        R                  " U5      $ r   )r   rH   rJ   )codes
categoriesrL   s      r   Series_from_codes2test_categorical_to_int.<locals>.Series_from_codes  s    1%D}}Q''r"   )r?   r   r   r   )rB   r   r   ru   r?   )rB   rL   r   c                 V    [        XU5      n[        R                  " XB5      (       d   eg r   )r   r8   array_equal)r   r   expectedr]   gots        r   r}   "test_categorical_to_int.<locals>.t  s#     y9~~c,,,,r"   )rB   rC   rB   )r   r?   r   r   )r?   r@   r   )r@   r   r?   )rB   dzrC   )r   rA   r   )r   rC   r   r   )r   r   )r?   r   r?   rC   rB   r1   Tr   )r?   r   r   r   r   rD   )r?   r@   r   r   )rB   rC   q)rB   rC   rL   r   efgh)r   r   rs   r   rH   rJ   r   r8   allr   r   r   	DataFramer   r   r   r   r9   objectfloatr   rK   )r   rs   sc_pandasconstructorsr   concatconvcat2conv2r}   s               r   test_categorical_to_intr     s   &{MM/>%a(*Evvh)+,,,,vvhnn45555b!W%J	
 56(((  12Cj*-C%c:xzBD66$*,----z=1D&sJ6(8STE66%:-....MM"QFJ'
 MM"QFJ'
!  0 -5J - oz9-bjj!:y9bjj/YGiI&bjjY	2bjj%()Y?bjj&)9i@o+Y7$&:IF
MM&XZ a*i0a*i0ac
+ZC
MM	/3*-
 c:sc:sdMA3 MM


S#Jc
+,
 MM&c
S#rNHJ MM&c2
HJ 	dBFFC 65/*	 	dBFFC 65/*	 	dBFFC 5'"	 MM0
r"   )NNr   )__all__numpyr8   patsyr   
patsy.utilr   r	   r
   r   r   r   r   r   r   r   r   r   r   rH   r   r   r   r6   r   rM   rS   r   r   r   r   r)   r"   r   <module>r      sy   
 Q<       
f &3R,:"9 9xYBzEPAr"   