
    gO                       d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  erd dl!m"Z" d dl#m$Z$ d dlm%Z% d dlm&Z&  G d dee         Z' G d d      Z( G d d e      Z)y!)"    )annotationsreduce)TYPE_CHECKING)Any)Callable)Iterable)Literal)Sequence)combine_root_names)parse_into_exprs)reduce_output_names)PandasLikeDataFrame)PandasLikeExpr)PandasSelectorNamespace)PandasLikeSeries)create_compliant_series)diagonal_concat)horizontal_concat)rename)vertical_concat)CompliantNamespace)import_dtypes_module)IntoPandasLikeExpr)DType)Implementation)Versionc                     e Zd Zedd       Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZddZddZ	ddZ
d dZd!d	Zd"d
Zd!dZd#dZd#dZd#dZd#dZd#dZd#dZ	 	 	 	 	 	 d$dZ	 	 	 	 d%dZddd	 	 	 	 	 	 	 	 	 d&dZy)'PandasLikeNamespacec                Z    t        | j                  | j                  | j                        S Nimplementationbackend_versionversion)r   _implementation_backend_version_versionselfs    T/var/www/openai/venv/lib/python3.12/site-packages/narwhals/_pandas_like/namespace.py	selectorszPandasLikeNamespace.selectors"   s(    &// 11MM
 	
    c                .    || _         || _        || _        y N)r&   r'   r(   )r*   r#   r$   r%   s       r+   __init__zPandasLikeNamespace.__init__+   s      . /r-   c               f    t        |||||| j                  | j                  | j                  |	      S )Ndepthfunction_name
root_namesoutput_namesr#   r$   r%   kwargsr   r&   r'   r(   )r*   funcr3   r4   r5   r6   r7   s          r+   _create_expr_from_callablez.PandasLikeNamespace._create_expr_from_callable5   s<     '!%// 11MM

 
	
r-   c                   t        j                  |g|j                  j                  |j                  j                  dd | j
                  | j                  | j                        S )Nr      nameindexr#   r$   r%   )r   _from_iterable_native_seriesr>   r?   r&   r'   r(   )r*   valuereference_seriess      r+   _create_series_from_scalarz.PandasLikeNamespace._create_series_from_scalarK   sZ      ..G!0055"1177!<// 11MM
 	
r-   c                n    t        fdddd d | j                  | j                  | j                  i 	      S )Nc                
    gS r/    )_dfseriess    r+   <lambda>z>PandasLikeNamespace._create_expr_from_series.<locals>.<lambda>Y   s    r-   r   rI   r2   r8   )r*   rI   s    `r+   _create_expr_from_seriesz,PandasLikeNamespace._create_expr_from_seriesW   s;     "// 11MM

 
	
r-   c                \    t        || j                  | j                  | j                        S r!   )r   r&   r'   r(   r*   rB   s     r+   _create_compliant_seriesz,PandasLikeNamespace._create_compliant_seriesd   s+    &// 11MM	
 	
r-   c                l    t        j                  || j                  | j                  | j                  dS r!   )r   from_column_namesr&   r'   r(   )r*   column_namess     r+   colzPandasLikeNamespace.colm   s1    //// 11MM	
 	
r-   c                l    t        j                  || j                  | j                  | j                  dS r!   )r   from_column_indicesr&   r'   r(   )r*   column_indicess     r+   nthzPandasLikeNamespace.nthu   s1    11// 11MM	
 	
r-   c                n     t         fdddd d  j                   j                   j                  i 	      S )Nc           	         | j                   D cg c]<  }t        | j                  |   j                  j                  j
                        > c}S c c}w r!   )columnsr   _native_framer&   r'   r(   )dfcolumn_namer*   s     r+   rJ   z)PandasLikeNamespace.all.<locals>.<lambda>   sY     $&:: $.K !$$[1#'#7#7$($9$9 MM	 $. s   AAr   allr2   r8   r)   s   `r+   r]   zPandasLikeNamespace.all}   s?     // 11MM#
 	
r-   c                     d fdt        fdddd dg j                   j                   j                  i 	      S )Nc                    t        j                  gd| j                  j                  dd j                  j
                  j                        }r|j                        S |S )Nliteralr   r<   datar>   r?   r#   r$   r%   )r   r@   rZ   r?   r&   r'   r(   cast)r[   pandas_seriesdtyper*   rB   s     r+   _lit_pandas_seriesz3PandasLikeNamespace.lit.<locals>._lit_pandas_series   sf    ,;;W&&,,Qq1#33 $ 5 5M $))%00  r-   c                     |       gS r/   rG   )r[   rf   s    r+   rJ   z)PandasLikeNamespace.lit.<locals>.<lambda>   s    *2./r-   r   litr`   r2   )r[   r   returnr   r8   )r*   rB   re   rf   s   ```@r+   rh   zPandasLikeNamespace.lit   sD    	! /#// 11MM

 
	
r-   c                p     t         fdddd dg j                   j                   j                  i 	      S )Nc                    t        j                  t        | j                        gddgj                  j
                  j                        gS )Nlenr   r=   )r   r@   rl   rZ   r&   r'   r(   )r[   r*   s    r+   rJ   z)PandasLikeNamespace.len.<locals>.<lambda>   sH     //))*+##'#7#7$($9$9 MM	r-   r   rl   r2   r8   r)   s   `r+   rl   zPandasLikeNamespace.len   sA    	 // 11MM%
 	
r-   c           	         t        |d| idfd}| j                  |t        d D              dz   dt              t	              d|i      S )	N	namespacec                8      fdD        }t        d |      gS )Nc              3  Z   K   | ]"  } |      D ]  }|j                  d         $ ywr   N	fill_null.0_exprsr[   s      r+   	<genexpr>zCPandasLikeNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>   &     R,b	1akk!n	n,   (+c                    | |z   S r/   rG   xys     r+   rJ   zBPandasLikeNamespace.sum_horizontal.<locals>.func.<locals>.<lambda>       Ar-   r   r[   rI   parsed_exprss   ` r+   r9   z0PandasLikeNamespace.sum_horizontal.<locals>.func   s    R,RF-v677r-   c              3  4   K   | ]  }|j                     y wr/   _depthru   r}   s     r+   rx   z5PandasLikeNamespace.sum_horizontal.<locals>.<genexpr>        51ahh   r<   sum_horizontalexprsr9   r3   r4   r5   r6   r7   r[   r   ri   zlist[PandasLikeSeries]r   r:   maxr   r   r*   r   r9   r   s      @r+   r   z"PandasLikeNamespace.sum_horizontal   b    '?$?	8 ..5559*),7,\:U# / 
 	
r-   c           	         t        |d| idfd}| j                  |t        d D              dz   dt              t	              d|i      S )	Nrn   c                8      fdD        }t        d |      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr/   rG   rt   s      r+   rx   zCPandasLikeNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>        E\E59aa9a\   c                    | |z  S r/   rG   r|   s     r+   rJ   zBPandasLikeNamespace.all_horizontal.<locals>.func.<locals>.<lambda>   r   r-   r   r   s   ` r+   r9   z0PandasLikeNamespace.all_horizontal.<locals>.func       E\EF-v677r-   c              3  4   K   | ]  }|j                     y wr/   r   r   s     r+   rx   z5PandasLikeNamespace.all_horizontal.<locals>.<genexpr>   r   r   r<   all_horizontalr   r   r   r   r   s      @r+   r   z"PandasLikeNamespace.all_horizontal   r   r-   c           	         t        |d| idfd}| j                  |t        d D              dz   dt              t	              d|i      S )	Nrn   c                8      fdD        }t        d |      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr/   rG   rt   s      r+   rx   zCPandasLikeNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>   r   r   c                    | |z  S r/   rG   r|   s     r+   rJ   zBPandasLikeNamespace.any_horizontal.<locals>.func.<locals>.<lambda>   r   r-   r   r   s   ` r+   r9   z0PandasLikeNamespace.any_horizontal.<locals>.func   r   r-   c              3  4   K   | ]  }|j                     y wr/   r   r   s     r+   rx   z5PandasLikeNamespace.any_horizontal.<locals>.<genexpr>   r   r   r<   any_horizontalr   r   r   r   r   s      @r+   r   z"PandasLikeNamespace.any_horizontal   r   r-   c           	         t        |d| idfd}| j                  |t        d D              dz   dt              t	              d|i      S )	Nrn   c                j      fdD        } fdD        }t        d |      t        d |      z  gS )Nc              3  Z   K   | ]"  } |      D ]  }|j                  d         $ ywrq   rr   rt   s      r+   rx   zDPandasLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   ry   rz   c              3  ^   K   | ]$  } |      D ]  }d |j                         z
    & yw)r<   N)is_nullrt   s      r+   rx   zDPandasLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s(     S<%rAa!))+oo<s   *-c                    | |z   S r/   rG   r|   s     r+   rJ   zCPandasLikeNamespace.mean_horizontal.<locals>.func.<locals>.<lambda>   s    AEr-   c                    | |z   S r/   rG   r|   s     r+   rJ   zCPandasLikeNamespace.mean_horizontal.<locals>.func.<locals>.<lambda>   s    Qr-   r   )r[   rI   non_nar   s   `  r+   r9   z1PandasLikeNamespace.mean_horizontal.<locals>.func   s:    R,RFS<SF)62V<NPV5WW r-   c              3  4   K   | ]  }|j                     y wr/   r   r   s     r+   rx   z6PandasLikeNamespace.mean_horizontal.<locals>.<genexpr>   r   r   r<   mean_horizontalr   r   r   r   r   s      @r+   r   z#PandasLikeNamespace.mean_horizontal   sb    '?$?	 ..5559+),7,\:U# / 
 	
r-   c           	          t        |d id fd} j                  |t        d D              dz   dt              t	              d|i      S )	Nrn   c                h   D cg c]  } ||       D ]  }|  }}}t        t        j                  d |D        d      j                  j	                  d      |d   j
                  j                  j                        j                  j                  j                        gS c c}}w )	Nc              3  <   K   | ]  }|j                           y wr/   to_frameru   rw   s     r+   rx   zCPandasLikeNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>       :6aQZZ\6   
horizontalhowr<   axisr   r#   r$   )native_seriesr#   r$   r%   )	r   r   concatrZ   minr>   r&   r'   r(   r[   rv   rw   rI   r   r*   s       r+   r9   z0PandasLikeNamespace.min_horizontal.<locals>.func	  s    %1E\E59aa9a\FE !"(:6: $ '-q	'+';';(,(=(=# $(#7#7$($9$9 MM  F   B.c              3  4   K   | ]  }|j                     y wr/   r   r   s     r+   rx   z5PandasLikeNamespace.min_horizontal.<locals>.<genexpr>  r   r   r<   min_horizontalr   r   r   r   r   s   `  @r+   r   z"PandasLikeNamespace.min_horizontal  b    '?$?	& ..5559*),7,\:U# / 
 	
r-   c           	          t        |d id fd} j                  |t        d D              dz   dt              t	              d|i      S )	Nrn   c                h   D cg c]  } ||       D ]  }|  }}}t        t        j                  d |D        d      j                  j	                  d      |d   j
                  j                  j                        j                  j                  j                        gS c c}}w )	Nc              3  <   K   | ]  }|j                           y wr/   r   r   s     r+   rx   zCPandasLikeNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>/  r   r   r   r   r<   r   r   r   r"   )	r   r   r   rZ   r   r>   r&   r'   r(   r   s       r+   r9   z0PandasLikeNamespace.max_horizontal.<locals>.func(  s    %1E\E59aa9a\FE !:6: $ '-q	'+';';(,(=(= $(#7#7$($9$9 MM  Fr   c              3  4   K   | ]  }|j                     y wr/   r   r   s     r+   rx   z5PandasLikeNamespace.max_horizontal.<locals>.<genexpr>=  r   r   r<   max_horizontalr   r   r   r   r   s   `  @r+   r   z"PandasLikeNamespace.max_horizontal%  r   r-   c               6   |D cg c]  }|j                    }}|dk(  rMt        t        || j                  | j                        | j                  | j                  | j
                        S |dk(  rMt        t        || j                  | j                        | j                  | j                  | j
                        S |dk(  rMt        t        || j                  | j                        | j                  | j                  | j
                        S t        c c}w )Nr   r   r"   verticaldiagonal)	rZ   r   r   r&   r'   r(   r   r   NotImplementedError)r*   itemsr   itemdfss        r+   r   zPandasLikeNamespace.concatD  s    :??$,,?,&!#'#7#7$($9$9
  $33 $ 5 5	 	 *&#'#7#7$($9$9
  $33 $ 5 5	 	 *&#'#7#7$($9$9
  $33 $ 5 5	 	 "!G @s   Dc                    | j                  | j                  | j                  | j                        }|r |j                  | }nd}t        |      t        || j                  | j                  | j                        S )N)r%   z+at least one predicate needs to be provided)	__class__r&   r'   r(   r   	TypeError
PandasWhen)r*   
predicatesplx	conditionmsgs        r+   whenzPandasLikeNamespace.wheno  sz     nn  $"7"7  
 ***J7I?CC. t++T-B-BDMM
 	
r-    F)	separatorignore_nullsc                    g t        |d it        |d it         j                        d fd} j                  |t	        d D              dz   dt              t              ||d      S )	Nrn   c                x     fdD        }D cg c]  } |j                                D ]  }| ! }}}s0t        d |      }t        fd|      j                  | d       }|gS t        ||      D cg c]  \  }}|j                  | d       c}}^}}	|j                  j                  gt        |      z  d|j                  j                  j                  j                  j                        fd|d d D        }
t        d	 d
 t        |
|	      D        |      }|gS c c}}w c c}}w )Nc              3  v   K   | ]0  } |j                  j                                     D ]  }|  2 y wr/   )rc   String)ru   rv   rw   r[   dtypess      r+   rx   z?PandasLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>  s6      +e5PUZZ5PQS5T5T|s   69c                    | |z  S r/   rG   r|   s     r+   rJ   z>PandasLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>  s    q1ur-   c                    | z   |z   S r/   rG   )r}   r~   r   s     r+   rJ   z>PandasLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>  s    Q]Q->r-   r   sepra   c              3  D   K   | ]  }j                  | d         yw)r   N)zip_with)ru   nm	sep_arrays     r+   rx   z?PandasLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>  s!     SNbi00"b9Ns    c                    | |z   S r/   rG   r|   s     r+   rJ   z>PandasLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>  s    Qr-   c              3  ,   K   | ]  \  }}||z     y wr/   rG   )ru   rw   vs      r+   rx   z?PandasLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>  s     ?'>tq!QU'>s   )r   r   r   zipr   r@   rl   rA   r?   r&   r'   r(   )r[   rI   rv   rw   	null_masknull_mask_resultresultr   
init_valuevalues
separatorsr   r   r   r   r*   r   s   `          @r+   r9   z,PandasLikeNamespace.concat_str.<locals>.func  sN   +F )5Ruoemmob>Q>QIR#)*<i#H  >GPP%%t. 8O% 695K'5KEArAJJsB'5K'#
V '00??#s:6$3399#'#7#7$($9$9 MM @ 	 TIcrNS
&?s:v'>? 8O7 S's   $D0=D6c              3  4   K   | ]  }|j                     y wr/   r   r   s     r+   rx   z1PandasLikeNamespace.concat_str.<locals>.<genexpr>  r   r   r<   
concat_str)r   
more_exprsr   r   r   r   )r   r   r(   r:   r   r   r   )r*   r   r   r   r   r9   r   r   s   ` ``  @@r+   r   zPandasLikeNamespace.concat_str  s    
u55
z:T:
 &dmm4	 	B ..5559&),7,\:(& ,	 / 
 	
r-   N)ri   r   )r#   r   r$   tuple[int, ...]r%   r   ri   None)r9   z;Callable[[PandasLikeDataFrame], Sequence[PandasLikeSeries]]r3   intr4   strr5   list[str] | Noner6   r   r7   dict[str, Any]ri   r   )rB   r   rC   r   ri   r   )rI   r   ri   r   )rB   r   ri   r   )rQ   r   ri   r   )rU   r   ri   r   )ri   r   )rB   r   re   zDType | Noneri   r   )r   r   ri   r   )r   zIterable[PandasLikeDataFrame]r   z-Literal['horizontal', 'vertical', 'diagonal']ri   r   )r   r   ri   r   )
r   zIterable[IntoPandasLikeExpr]r   r   r   r   r   boolri   r   )__name__
__module____qualname__propertyr,   r0   r:   rD   rK   rN   rR   rV   r]   rh   rl   r   r   r   r   r   r   r   r   r   rG   r-   r+   r   r   !   s\   
 
 &  )  	 
 
 
I
 	

 
 %
 '
 
 

,



/?

	






*
4
.
 
 
 
&
>
>)",)" ;	)"
 
)"V
'
 

* ":
+:
 (:
 	:

 :
 
:
r-   r   c                  D    e Zd Z	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZddZy)	r   Nc               X    || _         || _        || _        || _        || _        || _        y r/   )r&   r'   
_condition_then_value_otherwise_valuer(   )r*   r   r#   r$   
then_valueotherwise_valuer%   s          r+   r0   zPandasWhen.__init__  s2      . /#% /r-   c           	        ddl m} ddlm} |j	                         }  || j
                  |      |      d   }	   || j                  |      |      d   } |||      \  }}| j                   !|j#                  |j%                  |            gS 	  || j                   |      }	 |	|      d   }
|j'                  ||
      gS # t        $ rm |j                  j                  | j                  gt        |      z  d|j                  j                  | j                  | j                  | j                        }Y w xY w# t        $ r/ |j#                  |j%                  || j                               gcY S w xY w)Nr   )parse_into_expr)"broadcast_align_and_extract_native)rn   r`   r=   )narwhals._expression_parsingr  narwhals._pandas_like.utilsr  __narwhals_namespace__r   r   r   r   r@   rl   rA   r?   r&   r'   r(   r   _from_native_serieswherer   )r*   r[   r  r  r   r   value_seriesvalue_series_nativecondition_nativeotherwise_exprotherwise_seriess              r+   __call__zPandasWhen.__call__  s   @R'')CODOOsCBGJ		K?4+;+;sKBOPQRL 1S)1
--   (00'--.>? 
	H,T-B-BcRN  .b1!4 )))5EFGG?  		$..==!!"S^3..44#33 $ 5 5 > L		,  	 00'--.>@U@UV 	s$   C D= A3D:9D:=5E54E5c                x    || _         t        | ddd d | j                  | j                  | j                  d|i	      S )Nr   whenthenrB   r2   )r   
PandasThenr&   r'   r(   rM   s     r+   thenzPandasWhen.then  sG     $// 11MMU#

 
	
r-   )NN)r   r   r#   r   r$   r   r  r   r  r   r%   r   ri   r   )r[   r   ri   zSequence[PandasLikeSeries])rB   'PandasLikeExpr | PandasLikeSeries | Anyri   r  )r   r   r   r0   r  r  rG   r-   r+   r   r     s_     # !  '  )	 
       
 "'HR
r-   r   c                  D    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZy)r  c                   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        y r/   )	r&   r'   r(   _callr   _function_name_root_names_output_names_kwargs)
r*   callr3   r4   r5   r6   r#   r$   r%   r7   s
             r+   r0   zPandasThen.__init__	  sH      . /
+%)r-   c                6    || j                   _        d| _        | S )Nwhenotherwise)r  r   r  rM   s     r+   	otherwisezPandasThen.otherwise   s     ',

#-r-   N)r  r   r3   r   r4   r   r5   r   r6   r   r#   r   r$   r   r%   r   r7   r   ri   r   )rB   r  ri   r   )r   r   r   r0   r   rG   r-   r+   r  r    sn     	
  % ' ' )   
.r-   r  N)*
__future__r   	functoolsr   typingr   r   r   r	   r
   r   r  r   r   r   narwhals._pandas_like.dataframer   narwhals._pandas_like.exprr   narwhals._pandas_like.selectorsr   narwhals._pandas_like.seriesr   r  r   r   r   r   r   narwhals.typingr   narwhals.utilsr   narwhals._pandas_like.typingr   narwhals.dtypesr   r   r   r   r   r  rG   r-   r+   <module>r,     s    "         ; 9 < ? 5 C 9 ? 7 9 . 7 . /?%-&Y
,-=> Y
xH
 H
V r-   