
    $YHh%J                         g d 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mZmZmZmZmZmZmZ e	rddlZ G d de      ZddZd Zd	 Zd
 Zd Z G d de      Zd ZddZd Zy))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                       e Zd Zd ZeZy)_CategoricalBoxc                 .    || _         || _        || _        y Ndatacontrastlevels)selfr   r   r   s       J/var/www/html/planif/env/lib/python3.12/site-packages/patsy/categorical.py__init__z_CategoricalBox.__init__=   s    	     N)__name__
__module____qualname__r   r   __getstate__ r    r   r   r   <   s    
 Lr    r   c                     t        | t              r(|| j                  }|| j                  }| j                  } t        | ||      S )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   sD    @ $(}}H>[[Fyy4622r    c                  ,   t        d      } t        | t              sJ | j                  dk(  sJ | j                  J | j
                  J t        ddd      }|j                  dk(  sJ |j
                  dk(  sJ |j                  dk(  sJ t        |d      }|j                  dk(  sJ |j
                  dk(  sJ |j                  dk(  sJ t        |d      }|j                  dk(  sJ |j
                  dk(  sJ |j                  dk(  sJ t        |       y )NasdfDATACONTRASTLEVELSz
NEW LEVELSr   zNEW CONTRAST)r   r'   r   r   r   r   r   )c1c2c3c4s       r   test_Cr2   n   s   	
6Bb/***77f99;;	
6:x	(B77f;;*$$$99   	
2l	#B77f;;*$$$99$$$	
2~	B77f;;.(((99   rr    c                     t        |       ryt        | t              ryt        j                  |       } t        | j                  t        j                        ryy)NTF)r   r'   r   npasarrayr   dtypenumberr   s    r   r   r      s@    !$'$(::dDtzz299-r    c                  "   t         rJt        j                  g d      } t        |       sJ t        r t        t        j
                  |             sJ t        t        g d            sJ t        ddg      sJ t        ddg      sJ t        ddt        j                  g      sJ t        g d      sJ t        g d      rJ t        ddd	t        j                  g      rJ t        g d
      rJ t        dddt        j                  g      rJ y )N         TFabr>   r?   Nr;   r<   r=   )      ?       @      @rA   rB   rC   )	r   pandasCategoricalr   r   Seriesr   r4   nan)cs    r   test_guess_categoricalrI      s    y) ###($V]]1%5666Qy\***dE]+++c3Z(((c3/000-... +++ !Q266!2333 111 #sC!89999r    c                     t        | d      r| j                  dkD  rt        d      t        |       rt	        | t
        t        f      r| g} | S )Nndimr;   z)categorical data cannot be >1-dimensional)hasattrrK   r   r
   r'   strbytesr8   s    r   _categorical_shape_fixrO      sF    
 tVQDEE D>ZsEl;vKr    c                   $    e Zd ZddZd Zd ZeZy)r   Nc                 Z    || _         || _        d | _        d | _        t	               | _        y r   )
_NA_action_origin	_contrast_levelsset
_level_set)r   	NA_actionorigins      r   r   zCategoricalSniffer.__init__   s'    #%r    c                     | j                   2t        | j                        }|j                  t               || _         t        | j                         | j                  fS )N)key)rU   listrW   sortr   tuplerT   )r   r   s     r   levels_contrastz"CategoricalSniffer.levels_contrast   sG    <<$//*FKKOK,!DLT\\"DNN22r    c                    t        |d      r|j                  | _        t        |t              r3|j
                  t        |j
                        | _        y|j                  }t        |      rt        t        |            | _        yt        |d      r7t        |j                  t        j                        rt        ddg      | _        yt#        |      }|D ]`  }| j$                  j'                  |      r|du s|du r| j                   j)                  ddg       E	 | j                   j+                  |       b | j                   t        ddg      k(  S # t,        $ r t/        d| j0                        w xY w)Nr   Tr6   F?Error interpreting categorical data: all items must be hashable)rL   r   rT   r'   r   r   r^   rU   r   r   r   r   r6   r4   bool_rV   rW   rO   rR   is_categorical_NAupdateadd	TypeErrorr   rS   )r   r   values      r   sniffzCategoricalSniffer.sniff   sD   4$!]]DNdO,{{&$T[[1 yy%d+ !!>t!DEDL 4!odjj"((&K!4-0DO%d+ 	E007}&&e}5OO''.	  #tUm"444 ! $5 s   E E3r   )r!   r"   r#   r   r_   rh   r   r$   r%   r    r   r   r      s     3(5T Lr    r   c                  |   ddl m d*fd	} t        rd t        g}t        r|t
        j                  d gz  }|D ]  } | g  |t        j                  g d            gdd        | g  |t        d	dgd
dg            gdd        | g  |t        d	dgdd
g            gdd        |t        j                  d
dg            }d|_	         | g |gddd         | g t        d	dg      t        ddg      gdd        | g t        d	dgg d      t        ddg      gdd        | g t        d	dgg d      t        ddg      gdd        | ddgt        d	t        j                  g      t        dd g      gdd       t         dg            }|j                  t        d	t        j                  d g             |j                         \  }}t        |      t        d d	g      k(  sJ  | ddgt        dt        j                  d g      gdd        | g t        ddg      t        dg      t        ddg      gdd        | g t        j                   ddg      d ggdd        | ddgt        d!d d"t        j                  d#g      gdd$        | g t        ddgd%&      gdd'd%        | g ddgdggdd(        | g dd
gd
ggdd        | g dgdd)       dd l}t                      }|j%                  t&        |j                  i g       |j%                  t&        |j                  t        j                   dgg             y )+Nr   NAActionc                     t         |             }|D ]  }|j                  |      }|r|sJ  n|sJ  |j                         ||fk(  sJ y )NNA_types)r   rh   r_   )	rn   datasexp_finish_fast
exp_levelsexp_contrastsnifferr   donerk   s	           r   tz"test_CategoricalSniffer.<locals>.t   sd    $Xx%@A 	+D==&D&&***	+ &&(Z,FFFFr    c                     | S r   r%   xs    r   <lambda>z)test_CategoricalSniffer.<locals>.<lambda>   s    1 r    c                 >    t        t        j                  |             S r   )r   rD   rF   rw   s    r   ry   z)test_CategoricalSniffer.<locals>.<lambda>   s    qq1A/B r    )r;   r<   NT)r;   r<   r;   r>   r?   r>   r?   r?   r>   r+   r<   r=   Fr:   r-      )r=   r<   r;   NoneNaN
   )r;   r   rm   FT      (   )FTr   r   r   r   foor?   r<   r>   r;   rH   N)r   r   r   FOO)r   )r   r   r   r   r   )r?   r   )patsy.missingrk   r   r   r   rD   rF   rE   r   r   r4   rG   r   rh   r_   rV   r5   pytestraisesr   )	ru   prepsprepobjrs   r   _r   rk   s	           @r   test_CategoricalSnifferr      sA   &	G  a (fmm%BCCE 	7D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%	7( 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%&'
 '')IFA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           
         t        |t              sJ t        |       r6t        t        |             }||k(  st	        d|d||      t        |       S t        | t              rU| j                  =t        | j                        |k7  r%t	        d|dt        | j                        |      | j                  } t        |       } 	 t        t        |t        t        |                        }t        | d      rSt!        | j"                  t$        j&                        r/|d   dk(  r'|d   dk(  r| j)                  t$        j*                        S t%        j,                  t        |       t.        	      }t1        |       D ]%  \  }}|j3                  |      rd
||<   	 ||   ||<   ' t<        r;t        | t>        j@                        r!t?        j@                  || jB                        }|S # t        $ r t	        d|      w xY w# t4        $ r d}	g }
t        |      |	k  r#|
|D cg c]  }t7        |       nc c}w c}z  }
nb|
|d |	dz   D cg c]  }t7        |       nc c}w c}z  }
|
j9                  d       |
||	 dz  d  D cg c]  }t7        |       nc c}w c}z  }
ddj;                  |
      z  }t	        d|d|d|      t        $ r t	        d||      w xY w)Nzmismatching levels: expected z, got ra   r6   Fr   Tr;   r6   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'   r^   r   r   r   r   r   r   r   rO   dictziprangelenrf   rL   r   r6   r4   rb   astypeint_emptyint	enumeraterc   KeyErrorreprappendjoinr   rD   rF   r   )r   r   rX   rY   data_levels_tuplelevel_to_intoutirg   SHOW_LEVELS
level_strslevel	level_strs                r   r   r   G  s   fe$$$ "$'!"?"EF F*=CEVW  (--$(;;"uT[['9V'C5-/ 
 yy!$'D
Cc&k(:;< tW/$**bhh"G!#T(:a(?;;rww''
((3t9C
(CdO 5&&u-CF%e,A8 z$6mmCtzz2JS  
PRX
 	

"  
v;+-F"C54;"C"CCJFCU[TUEU<V"W54;"W"WWJ%%e,FK<STCTCV<W"X54;"X"XXJ"dii
&;<	  DI)U 	    9>A s=   'G% G>%G;> KH1
0KI
(KJ
A
Kc            	      	   dd l } ddlm} t        rt	        j
                  g dg d      }t        |d |             }t        j                  |g dk(        sJ t        j                  |j                  g dk(        sJ | j                  t        t        t	        j                  d|i      d |              t        rt        g}t        rd }|j!                  |       |D ]  } |g d	d
      }t        |d
 |             }t        j                  |g d	k(        sJ  |g d	d      }	t        |d
 |dg            }
t        j                  |
g d	k(        sJ | j                  t        t         |ddgd
      d |              | j                  t        t         |ddgd
      d |                |       fd} |g dd
g d        |t        j"                  g d      d
g d        |t        j"                  g dt$              d
g d        |g ddg d        |t        j"                  g d      dg d        |t        j"                  g dt&              dg d        |t        j"                  g dt$              dg d        |g ddg d        |g ddg d       | j                  t        t        g dd |               |t)        g d      d
g d        |t)        g d      dg d        |t)        g dddg      dg d       | j                  t        t        t)        g dddg      d |               |dd
dg        |dd
dg        |dd dg       | j                  t        t        t        j"                  ddgddgg      d
 |              | j                  t        t        ddgddi f |              | j                  t        t        ddi gd
 |               |dd t        j*                  dgd
g d! |dd"g              |dd t        j*                  dgd#g d! |dd"g              |dd t        j*                  dgd#g d$ |d"g             | j                  t        t        g d%d& |              y )'Nr   rj   )r>   r?   rH   r   r   )r   r;   r<   r   c                 D    t        | |      }t        j                  |      S r   )r   rD   rF   )codes
categoriesrH   s      r   Series_from_codesz2test_categorical_to_int.<locals>.Series_from_codes  s    1%D}}Q''r    )r;   r   r   r{   )r>   r~   r~   rm   r;   )r>   rH   r|   c                 N    t        | ||      }t        j                  ||      sJ y r   )r   r4   array_equal)r   r   expectedrX   gots        r   ru   z"test_categorical_to_int.<locals>.t  s$     vy9~~c8,,,r    )r>   r?   r>   )r   r;   r   r   )r;   r<   r   )r<   r   r;   )r>   dzr?   )r   r=   r   )r   r?   r   r   )r   r   )r;   r   r;   r?   r>   r-   Tr   )r;   r   r   r   r   r@   )r;   r<   r   r   )r>   r?   q)r>   r?   rH   r   efgh)r   r   rk   r   rD   rF   r   r4   allr   r   r   	DataFramer   r   r   r   r5   objectfloatr   rG   )r   rk   sc_pandasconstructorsr   concatconvcat2conv2ru   s               r   test_categorical_to_intr     s   &MM/>%a(*Evvh)+,,,vvhnn4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__numpyr4   patsyr   
patsy.utilr   r	   r
   r   r   r   r   r   r   r   r   r   r   rD   r   r   r   r2   r   rI   rO   r   r   r   r   r%   r    r   <module>r      sy   
 Q<       
f &3R,:"9 9xYBzEPAr    