
    gH                       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& 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)Iterable)Literal)Sequence)ArrowDataFrame	ArrowExpr)ArrowSelectorNamespaceArrowSeries)broadcast_series)diagonal_concat)horizontal_concat)vertical_concat)combine_root_names)parse_into_exprs)reduce_output_names)CompliantNamespace)Implementation)import_dtypes_module)Callable)Self)IntoArrowExpr)DType)Versionc                  *   e Z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ed"d       Z	 	 	 	 	 	 d#dZ	 	 	 	 	 	 	 	 	 	 	 	 d$dZy)%ArrowNamespacec          
     V    ddl m}  ||||||| j                  | j                  |      S )Nr   r   depthfunction_name
root_namesoutput_namesbackend_versionversionkwargsnarwhals._arrow.exprr   _backend_version_version)selffuncr$   r%   r&   r'   r*   r   s           N/var/www/openai/venv/lib/python3.12/site-packages/narwhals/_arrow/namespace.py_create_expr_from_callablez)ArrowNamespace._create_expr_from_callable$   s6     	3'!% 11MM	
 		
    c           
     ^    ddl m}  |fdddd d | j                  | j                  i       S )Nr   r   c                
    gS N )_dfseriess    r1   <lambda>z9ArrowNamespace._create_expr_from_series.<locals>.<lambda>?   s    r3   r9   r#   r+   )r/   r9   r   s    ` r1   _create_expr_from_seriesz'ArrowNamespace._create_expr_from_series;   s5    2 " 11MM	
 		
r3   c                   ddl m} | j                  dk  rt        |d      r|j	                         } |j
                  |g|j                  | j                  | j                        S )Nr   r   )   as_pynamer(   r)   )narwhals._arrow.seriesr   r-   hasattrr>   _from_iterabler@   r.   )r/   valuereference_seriesr   s       r1   _create_series_from_scalarz)ArrowNamespace._create_series_from_scalarI   s\     	7  5(WUG-DKKME){))G!&& 11MM	
 	
r3   c                v    dd l }ddlm}  ||j                  |g      d| j                  | j
                        S )Nr   r    native_seriesr@   r(   r)   )pyarrowrA   r   chunked_arrayr-   r.   )r/   rD   par   s       r1   _create_compliant_seriesz'ArrowNamespace._create_compliant_seriesW   s8    6**E73 11MM	
 	
r3   c               J    || _         t        j                  | _        || _        y r6   )r-   r   PYARROW_implementationr.   )r/   r(   r)   s      r1   __init__zArrowNamespace.__init__d   s!     !0-55r3   c                \    ddl m}  |j                  || j                  | j                  dS Nr   r   r(   r)   )r,   r   from_column_namesr-   r.   )r/   column_namesr   s      r1   colzArrowNamespace.coll   s+    2*y**4+@+@$--
 	
r3   c                \    ddl m}  |j                  || j                  | j                  dS rT   )r,   r   from_column_indicesr-   r.   )r/   column_indicesr   s      r1   nthzArrowNamespace.nths   s+    2,y,,T-B-BDMM
 	
r3   c           
     Z     t         fdddd dg j                   j                  i       S )Nc                    t        j                  t        | j                        gdj                  j
                        gS )Nlenr?   )r   rC   r_   _native_framer-   r.   )dfr/   s    r1   r:   z$ArrowNamespace.len.<locals>.<lambda>}   s:    **))*+$($9$9 MM	r3   r   r_   r#   r   r-   r.   r/   s   `r1   r_   zArrowNamespace.lenz   s8      11MM
 	
r3   c           
     j    ddl m} ddlm  |fdddd d | j                  | j
                  i       S )Nr   r   r   c           	         | j                   D cg c]/  } | j                  |   || j                  | j                        1 c}S c c}w )Nr?   )columnsr`   r-   r.   )ra   column_namer   s     r1   r:   z$ArrowNamespace.all.<locals>.<lambda>   sS     $&:: $.K $$[1$$&$7$7KK	 $. s   4Aallr#   )r,   r   rA   r   r-   r.   )r/   r   r   s     @r1   rh   zArrowNamespace.all   s<    26  11MM!
 	
r3   c           
     p     d fdt        fdddd dg j                   j                  i       S )Nc                    t        j                  gdj                  j                        }r|j	                        S |S )Nliteral)datar@   r(   r)   )r   rC   r-   r.   cast)_arrow_seriesdtyper/   rD   s     r1   _lit_arrow_seriesz-ArrowNamespace.lit.<locals>._lit_arrow_series   sG    &55W $ 5 5	L #((//r3   c                     |       gS r6   r7   )ra   rq   s    r1   r:   z$ArrowNamespace.lit.<locals>.<lambda>   s    )"-.r3   r   litrk   r#   )rn   r   returnr   rb   )r/   rD   rp   rq   s   ```@r1   rs   zArrowNamespace.lit   s;    		  .# 11MM	
 		
r3   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  <   K   | ]  } |      D ]  }|   y wr6   r7   .0_exprsra   s      r1   	<genexpr>z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>        E\E59aa9a\   c                    | |z  S r6   r7   xys     r1   r:   z=ArrowNamespace.all_horizontal.<locals>.func.<locals>.<lambda>       Ar3   r   ra   r9   parsed_exprss   ` r1   r0   z+ArrowNamespace.all_horizontal.<locals>.func       E\EF-v677r3   c              3  4   K   | ]  }|j                     y wr6   _depthrz   r   s     r1   r}   z0ArrowNamespace.all_horizontal.<locals>.<genexpr>        51ahh      all_horizontalexprsr0   r$   r%   r&   r'   r*   ra   r   rt   zlist[ArrowSeries]r   r2   maxr   r   r/   r   r0   r   s      @r1   r   zArrowNamespace.all_horizontal   b    '?$?	8 ..5559*),7,\:U# / 
 	
r3   c           	         t        |d| idfd}| j                  |t        d D              dz   dt              t	              d|i      S )	Nrv   c                8      fdD        }t        d |      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr6   r7   ry   s      r1   r}   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>   r~   r   c                    | |z  S r6   r7   r   s     r1   r:   z=ArrowNamespace.any_horizontal.<locals>.func.<locals>.<lambda>   r   r3   r   r   s   ` r1   r0   z+ArrowNamespace.any_horizontal.<locals>.func   r   r3   c              3  4   K   | ]  }|j                     y wr6   r   r   s     r1   r}   z0ArrowNamespace.any_horizontal.<locals>.<genexpr>   r   r   r   any_horizontalr   r   r   r   r   s      @r1   r   zArrowNamespace.any_horizontal   r   r3   c           	         t        |d| idfd}| j                  |t        d D              dz   dt              t	              d|i      S )	Nrv   c                8      fdD        }t        d |      gS )Nc              3  `   K   | ]%  } |      D ]  }|j                  d dd        ' ywr   N)strategylimit	fill_nullry   s      r1   r}   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>   9      )ErA AD9" :)   +.c                    | |z   S r6   r7   r   s     r1   r:   z=ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<lambda>   r   r3   r   r   s   ` r1   r0   z+ArrowNamespace.sum_horizontal.<locals>.func   s%    )F
 -v677r3   c              3  4   K   | ]  }|j                     y wr6   r   r   s     r1   r}   z0ArrowNamespace.sum_horizontal.<locals>.<genexpr>   r   r   r   sum_horizontalr   r   r   r   r   s      @r1   r   zArrowNamespace.sum_horizontal   sb    '?$?	8 ..5559*),7,\:U# / 
 	
r3   c           	         t        |d| it        | j                        dfd}| j                  |t	        d D              dz   dt              t              d|i      S )	Nrv   c                l      fdD        } fdD        }t        d |      t        d |      z  gS )Nc              3  `   K   | ]%  } |      D ]  }|j                  d dd        ' ywr   r   ry   s      r1   r}   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   r   r   c              3     K   | ]A  } |      D ]4  }d |j                         j                  j                               z
   6 C yw)r   N)is_nullrm   Int64rz   r{   r|   ra   dtypess      r1   r}   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   sF      )ErA AIIK$$V\\^44" 5)s   AA
c                    | |z   S r6   r7   r   s     r1   r:   z>ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<lambda>   s    AEr3   c                    | |z   S r6   r7   r   s     r1   r:   z>ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<lambda>   s    Qr3   r   )ra   r9   non_nar   r   s   `  r1   r0   z,ArrowNamespace.mean_horizontal.<locals>.func   sF    )F
)F )62V<NPV5WW r3   c              3  4   K   | ]  }|j                     y wr6   r   r   s     r1   r}   z1ArrowNamespace.mean_horizontal.<locals>.<genexpr>  r   r   r   mean_horizontalr   r   r   )r   r   r.   r2   r   r   r   )r/   r   r0   r   r   s      @@r1   r   zArrowNamespace.mean_horizontal   sp    '?$?%dmm4	 ..5559+),7,\:U# / 
 	
r3   c           	          dd l m t        |d id	 fd} j                  |t	        d D              dz   dt              t              d|i      S )
Nr   rv   c           	        D cg c]  } ||       D ]  }|  c}}^}}t        t        fd|D cg c]  }|j                   c}|j                        |j                  j                  j
                        gS c c}}w c c}w )Nc                (    j                  | |      S r6   )min_element_wiser   r   pcs     r1   r:   z=ArrowNamespace.min_horizontal.<locals>.func.<locals>.<lambda>      R%8%8A%>r3   rI   r   r   _native_seriesr@   r-   r.   ra   r{   r|   init_seriesr9   r   r   r/   s        r1   r0   z+ArrowNamespace.min_horizontal.<locals>.func      3?#S<%rAAA<#S K&"(>39:6a))6:#22#
 %))$($9$9 MM	  $T
 ;
   BBc              3  4   K   | ]  }|j                     y wr6   r   r   s     r1   r}   z0ArrowNamespace.min_horizontal.<locals>.<genexpr>"  r   r   r   min_horizontalr   r   r   pyarrow.computecomputer   r2   r   r   r   r/   r   r0   r   r   s   `  @@r1   r   zArrowNamespace.min_horizontal  e    $'?$?	 ..5559*),7,\:U# / 
 	
r3   c           	          dd l m t        |d id	 fd} j                  |t	        d D              dz   dt              t              d|i      S )
Nr   rv   c           	        D cg c]  } ||       D ]  }|  c}}^}}t        t        fd|D cg c]  }|j                   c}|j                        |j                  j                  j
                        gS c c}}w c c}w )Nc                (    j                  | |      S r6   )max_element_wiser   s     r1   r:   z=ArrowNamespace.max_horizontal.<locals>.func.<locals>.<lambda>3  r   r3   rI   r   r   s        r1   r0   z+ArrowNamespace.max_horizontal.<locals>.func.  r   r   c              3  4   K   | ]  }|j                     y wr6   r   r   s     r1   r}   z0ArrowNamespace.max_horizontal.<locals>.<genexpr>?  r   r   r   max_horizontalr   r   r   r   r   s   `  @@r1   r   zArrowNamespace.max_horizontal)  r   r3   c               (   |D cg c]  }|j                    }}|sd}t        |      |dk(  rt        |      }n3|dk(  rt        |      }n"|dk(  rt	        || j
                        }nt        t        || j
                  | j                        S c c}w )NzNo dataframes to concatenate
horizontalverticaldiagonalrU   )	r`   AssertionErrorr   r   r   r-   NotImplementedErrorr   r.   )r/   itemshowitemdfsmsgresult_tables          r1   concatzArrowNamespace.concatF  s     /44edt!!e40C %%,,S1LJ*3/LJ*30E0EFL%%$*?*?
 	
 5s   Bc                D    t        | j                  | j                        S )NrU   )r   r-   r.   rc   s    r1   	selectorszArrowNamespace.selectors_  s    % 114==
 	
r3   c                    | j                  | j                  | j                        }|r |j                  | }nd}t	        |      t        || j                  | j                        S )NrU   z+at least one predicate needs to be provided)r)   )	__class__r-   r.   r   	TypeError	ArrowWhen)r/   
predicatesplx	conditionr   s        r1   whenzArrowNamespace.whene  s]     nnT-B-BDMMnZ***J7I?CC. D$9$94==QQr3   c                   dd l m g t        |d it        |d it         j                        d	 fd} j                  |t        d D              dz   dt              t              ||d      S )
Nr   rv   c                      fdD        }rdnd} j                   g |	d|i}t        |dj                  j                        gS )Nc              3     K   | ]:  } |j                  j                                     D ]  }|j                    < y wr6   )rm   Stringr   r   s      r1   r}   z:ArrowNamespace.concat_str.<locals>.func.<locals>.<genexpr>  sB      )E4FMMO4R8A   8 !)s   A Askip	emit_nullnull_handlingrH   rI   )binary_join_element_wiser   r-   r.   )
ra   r9   r   result_seriesr   ignore_nullsr   r   r/   	separators
   `   r1   r0   z'ArrowNamespace.concat_str.<locals>.func  sq    )F
 '3FM7B77 "2?M "/$($9$9 MM	 r3   c              3  4   K   | ]  }|j                     y wr6   r   r   s     r1   r}   z,ArrowNamespace.concat_str.<locals>.<genexpr>  r   r   r   
concat_str)r   
more_exprsr   r   r   r   )	r   r   r   r   r.   r2   r   r   r   )	r/   r   r   r   r   r0   r   r   r   s	   ` ``  @@@r1   r   zArrowNamespace.concat_strr  s     	%
u55
z:T:
 &dmm4	 	& ..5559&),7,\:(& ,	 / 
 	
r3   N)r/   r   r0   z1Callable[[ArrowDataFrame], Sequence[ArrowSeries]]r$   intr%   strr&   list[str] | Noner'   r   r*   dict[str, Any]rt   r   )r/   r   r9   r   rt   r   )r/   r   rD   r   rE   r   rt   r   )r/   r   rD   r   rt   r   )r/   r   r(   tuple[int, ...]r)   r   rt   None)r/   r   rW   r   rt   r   )r/   r   r[   r   rt   r   )r/   r   rt   r   )r/   r   rD   r   rp   zDType | Nonert   r   )r/   r   r   r   rt   r   )r/   r   r   r   rt   r   )r/   r   r   zIterable[ArrowDataFrame]r   z-Literal['horizontal', 'vertical', 'diagonal']rt   r   )r/   r   rt   r   )r/   r   r   r   rt   r   )r/   r   r   zIterable[IntoArrowExpr]r   r   r   r   r   boolrt   r   )__name__
__module____qualname__r2   r;   rF   rN   rR   rX   r\   r_   rh   rs   r   r   r   r   r   r   r   propertyr   r   r   r7   r3   r1   r!   r!   #   s{   

?
 	

 
 %
 '
 
 

.



5@
	


  (7 BI 	 


(
.
.
 
 
(
8
:
:

'
 ;	

 

2 
 

RR"R 
R.
.
&.
 #.
 	.

 .
 
.
r3   r!   c                  @    e Zd Z	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZddZy)	r   Nc               J    || _         || _        || _        || _        || _        y r6   )r-   
_condition_then_value_otherwise_valuer.   )r/   r   r(   
then_valueotherwise_valuer)   s         r1   rR   zArrowWhen.__init__  s*     !0#% /r3   c           	        dd l }dd lm} ddlm} |j                         }  || j                  |      |      d   }	   || j                  |      |      d   }|j                   }|j                   }	| j"                  Y|j                  |j                  d |j$                        t        |	            }
|j'                  |j)                  |	||
            gS 	  || j"                  |      } ||      d   }t+        ||g      \  }	}
|j'                  |j)                  |	||
            gS # t        $ ri |j                  j                  |j                  |j                  | j                        t        |            d| j                  | j                        }Y Aw xY w# t        $ r0 |j'                  |j)                  |	|| j"                              gcY S w xY w)Nr   )parse_into_expr)rv   rk   r?   )type)rK   r   r   narwhals._expression_parsingr	  __narwhals_namespace__r  r  r   r   rC   repeatscalarr_   r-   r.   r   r  r
  _from_native_seriesif_elser   )r/   ra   rM   r   r	  r   r   value_seriesvalue_series_nativecondition_nativeotherwise_nativeotherwise_exprotherwise_seriess                r1   __call__zArrowWhen.__call__  s   $@'')CODOOsCBGJ			K?4+;+;sKBOPQRL +99$33  (!yy		$%8%=%=	>DT@U  00JJ/1DFVW 
	,T-B-BcRN  .b1!41A,-2.. 00JJ/1DFVW M  	$..==		"))D$4$45s9~F $ 5 5	 > L	.  		 00JJ(*=t?T?T 		s$   D, F! ,A.FF!6GGc                b    || _         t        | ddd d | j                  | j                  d|i      S )Nr   whenthenrD   r#   )r  	ArrowThenr-   r.   r/   rD   s     r1   thenzArrowWhen.then  s>     $ 11MMU#	
 		
r3   )NN)r   r   r(   r   r  r   r  r   r)   r   rt   r   )r/   r   ra   r   rt   zSequence[ArrowSeries])r/   r   rD   ArrowExpr | ArrowSeries | Anyrt   r  )r   r   r   rR   r  r  r7   r3   r1   r   r     sT    
 #   )  	 
     
 4l
r3   r   c                  D    e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZy)r  c               t    || _         || _        || _        || _        || _        || _        || _        || _        y r6   )r-   r.   _callr   _function_name_root_names_output_names_kwargs)	r/   callr$   r%   r&   r'   r(   r)   r*   s	            r1   rR   zArrowThen.__init__  s@     !0
+%)r3   c                6    || j                   _        d| _        | S )Nwhenotherwise)r   r  r!  r  s     r1   	otherwisezArrowThen.otherwise  s     ',

#-r3   N)r/   r   r%  r   r$   r   r%   r   r&   r   r'   r   r(   r   r)   r   r*   r   rt   r   )r/   r   rD   r  rt   r   )r   r   r   rR   r(  r7   r3   r1   r  r    sl     	
  % ' )   
*r3   r  N)+
__future__r   	functoolsr   typingr   r   r   r	   r
   narwhals._arrow.dataframer   r,   r   narwhals._arrow.selectorsr   rA   r   narwhals._arrow.utilsr   r   r   r   r  r   r   r   narwhals.typingr   narwhals.utilsr   r   r   typing_extensionsr   narwhals._arrow.typingr   narwhals.dtypesr   r   r!   r   r  r7   r3   r1   <module>r4     s    "        4 * < . 2 1 3 1 ; 9 < . ) /&4%&}
'4 }
@R
 R
j	 r3   