
    Wh.              
         d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
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mZmZ d dlmZmZ d dl m!Z! er&d dl"m#Z#m$Z$ d dl%m&Z&m'Z'm(Z( d dl)m*Z* d dl m+Z+ d dl,m-Z-m.Z.  G d deeeeej^                  df         Z0 G d deeeedf         Z1 G d deeeee1f   e      Z2y)    )annotationsN)reduce)chain)TYPE_CHECKINGLiteralArrowDataFrame	ArrowExpr)ArrowSelectorNamespaceArrowSeries)cast_to_comparable_string_types)CompliantThenEagerNamespace	EagerWhen)combine_alias_output_namescombine_evaluate_output_names)Implementation)IteratorSequence)ArrayOrScalarChunkedArrayAny
Incomplete)ScalarKwargs)Version)	IntoDTypeNonNestedLiteralc                      e Zd Zej                  Zedd       Zed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edd       ZddZ	 	 	 	 	 	 	 	 d dZddZy)!ArrowNamespacec                    t         S Nr   selfs    S/var/www/html/jupyter_env/lib/python3.12/site-packages/narwhals/_arrow/namespace.py
_dataframezArrowNamespace._dataframe%   s        c                    t         S r"   r
   r#   s    r%   _exprzArrowNamespace._expr)       r'   c                    t         S r"   r   r#   s    r%   _serieszArrowNamespace._series-   s    r'   c                   || _         y r"   )_version)r$   versions     r%   __init__zArrowNamespace.__init__1   s	    r'   c                N      j                   fdddd d  j                        S )Nc                ^    t        j                  t        | j                        gd      gS )Nlen)namecontext)r   from_iterabler3   native)dfr$   s    r%   <lambda>z$ArrowNamespace.len.<locals>.<lambda>7   s(    ))3ryy>*:PTU r'   r   r3   c                    dgS )Nr3    _dfs    r%   r9   z$ArrowNamespace.len.<locals>.<lambda><   s    ug r'   depthfunction_nameevaluate_output_namesalias_output_namesr/   r)   r.   r#   s   `r%   r3   zArrowNamespace.len4   s3    zz "5#MM  	
 		
r'   c                d     d fd j                  fdddd d  j                        S )Nc                `    t        j                  gd      }r|j                        S |S )Nliteral)datar4   r5   )r   r6   cast)_arrow_seriesdtyper$   values     r%   _lit_arrow_seriesz-ArrowNamespace.lit.<locals>._lit_arrow_seriesB   s8    &44W9dL #((//r'   c                     |       gS r"   r;   )r8   rM   s    r%   r9   z$ArrowNamespace.lit.<locals>.<lambda>K   s    )"-. r'   r   litc                    dgS )NrF   r;   r<   s    r%   r9   z$ArrowNamespace.lit.<locals>.<lambda>N   s    yk r'   r>   )rI   r	   returnr   rC   )r$   rL   rK   rM   s   ```@r%   rO   zArrowNamespace.litA   s6    	  zz."9#MM  
 	
r'   c          	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                     t        j                   fdD              }j                  j                  }r	d |D        }t	        t
        j                   ||       gS )Nc              3  .   K   | ]  } |        y wr"   r;   .0er8   s     r%   	<genexpr>z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>U        ?U!"?U   c              3  B   K   | ]  }|j                  d dd        yw)TN	fill_nullrV   ss     r%   rX   z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>X   s     HA!++dD$7H   )r   r6   r,   _align_full_broadcastr   operatorand_r8   seriesalignexprsignore_nullsr$   s   `  r%   funcz+ArrowNamespace.all_horizontal.<locals>.funcT   sM    ,1,?,??Uu?U,UFLL66EHH8==%.9::r'   c              3  4   K   | ]  }|j                     y wr"   _depthrV   xs     r%   rX   z0ArrowNamespace.all_horizontal.<locals>.<genexpr>]        .1ahh.      all_horizontalri   r?   r@   rA   rB   r5   r8   r	   rQ   zlist[ArrowSeries]r)   _from_callablemaxr   r   r$   rh   rg   ri   s   ``` r%   rr   zArrowNamespace.all_horizontalS   sR    	; zz((...2*"?"G95A ) 
 	
r'   c          	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                     t        j                   fdD              }j                  j                  }r	d |D        }t	        t
        j                   ||       gS )Nc              3  .   K   | ]  } |        y wr"   r;   rU   s     r%   rX   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>f   rY   rZ   c              3  B   K   | ]  }|j                  d dd        yw)FNr\   r^   s     r%   rX   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>i   s     IQ!++eT48Ir`   )r   r6   r,   ra   r   rb   or_rd   s   `  r%   ri   z+ArrowNamespace.any_horizontal.<locals>.funce   sM    ,1,?,??Uu?U,UFLL66EI&I8<<899r'   c              3  4   K   | ]  }|j                     y wr"   rk   rm   s     r%   rX   z0ArrowNamespace.any_horizontal.<locals>.<genexpr>n   ro   rp   rq   any_horizontalrs   rt   ru   rx   s   ``` r%   r   zArrowNamespace.any_horizontald   sR    	: zz((...2*"?"G95A ) 
 	
r'   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                     t        j                   fdD              }d |D        }j                  j                  }t	        t
        j                   ||       gS )Nc              3  .   K   | ]  } |        y wr"   r;   rV   exprr8   s     r%   rX   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>w   s     $@$T"X$@rZ   c              3  D   K   | ]  }|j                  d dd        ywr   N)strategylimitr\   r^   s     r%   rX   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>x   s     LAakk!d$k?L    )r   r6   r,   ra   r   rb   add)r8   itre   rf   rg   r$   s   `   r%   ri   z+ArrowNamespace.sum_horizontal.<locals>.funcv   sJ    $$$@%$@@BLLFLL66E8<<899r'   c              3  4   K   | ]  }|j                     y wr"   rk   rm   s     r%   rX   z0ArrowNamespace.sum_horizontal.<locals>.<genexpr>~   ro   rp   rq   sum_horizontalrs   rt   ru   r$   rg   ri   s   `` r%   r   zArrowNamespace.sum_horizontalu   sR    	: zz((...2*"?"G95A ) 
 	
r'   c           	           j                   j                  j                         d fd} j                  j	                  |t        d D              dz   dt         t                S )Nc                     t        t        j                   fdD                    }j                  j                  } |d |D         } |fd|D         }t        t        j                  |      t        t        j                  |      z  gS )Nc              3  .   K   | ]  } |        y wr"   r;   r   s     r%   rX   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s     3ODH3OrZ   c              3  D   K   | ]  }|j                  d dd        ywr   r\   r^   s     r%   rX   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s     R!++a$d+;Rr   c              3  b   K   | ]&  }d |j                         j                        z
   ( yw)rq   N)is_nullrH   )rV   r_   int_64s     r%   rX   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s'     PaQ!1!1&!99Ps   ,/)listr   r6   r,   ra   r   rb   r   )r8   expr_resultsrf   re   non_narg   r   r$   s   `    r%   ri   z,ArrowNamespace.mean_horizontal.<locals>.func   su     3 33O3O OPLLL66ER\RF P<PQF8<<06(,,3OOPPr'   c              3  4   K   | ]  }|j                     y wr"   rk   rm   s     r%   rX   z1ArrowNamespace.mean_horizontal.<locals>.<genexpr>   ro   rp   rq   mean_horizontalrs   rt   )r.   dtypesInt64r)   rv   rw   r   r   )r$   rg   ri   r   s   `` @r%   r   zArrowNamespace.mean_horizontal   sk    %%++-	Q zz((...2+"?"G95A ) 
 	
r'   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                d    j                   j                  }t        t        j                   fdD                    ^}} ||g| ^}}t        t        j                  |D cg c]  }|j                   c}|j                        }t        ||j                  j                        gS c c}w )Nc              3  .   K   | ]  } |        y wr"   r;   r   s     r%   rX   z>ArrowNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>        ;WDH;WrZ   r4   r/   )r,   ra   r   r   r6   r   pcmin_element_wiser7   r   r4   r.   r8   rf   init_seriesre   r_   native_seriesrg   r$   s   `     r%   ri   z+ArrowNamespace.min_horizontal.<locals>.func       LL66E#'(;(;;WQV;W(W#X K&#(#>v#> K&"##%?1ahh%?ASASM M0@0@$--X  &@   %B-
c              3  4   K   | ]  }|j                     y wr"   rk   rm   s     r%   rX   z0ArrowNamespace.min_horizontal.<locals>.<genexpr>   ro   rp   rq   min_horizontalrs   rt   ru   r   s   `` r%   r   zArrowNamespace.min_horizontal   R    		 zz((...2*"?"G95A ) 
 	
r'   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                d    j                   j                  }t        t        j                   fdD                    ^}} ||g| ^}}t        t        j                  |D cg c]  }|j                   c}|j                        }t        ||j                  j                        gS c c}w )Nc              3  .   K   | ]  } |        y wr"   r;   r   s     r%   rX   z>ArrowNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>   r   rZ   r   )r,   ra   r   r   r6   r   r   max_element_wiser7   r   r4   r.   r   s   `     r%   ri   z+ArrowNamespace.max_horizontal.<locals>.func   r   r   c              3  4   K   | ]  }|j                     y wr"   rk   rm   s     r%   rX   z0ArrowNamespace.max_horizontal.<locals>.<genexpr>   ro   rp   rq   max_horizontalrs   rt   ru   r   s   `` r%   r   zArrowNamespace.max_horizontal   r   r'   c               |    | j                   dk\  rt        j                  |d      S t        j                  |d      S )N)   default)promote_optionsT)promote)_backend_versionpaconcat_tables)r$   dfss     r%   _concat_diagonalzArrowNamespace._concat_diagonal   s6      E)##CCCT22r'   c                   t        t        j                  d |D                    }t        t        j                  d |D                    }t        j                  j                  ||      S )Nc              3  4   K   | ]  }|j                     y wr"   )column_namesrV   r8   s     r%   rX   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>   s     (GR(Grp   c              3  <   K   | ]  }|j                           y wr"   )itercolumnsr   s     r%   rX   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>   s     )Ir"..*:)Is   )names)r   r   r6   r   Tablefrom_arrays)r$   r   r   arrayss       r%   _concat_horizontalz!ArrowNamespace._concat_horizontal   sR    U(((G3(GGHe)))IS)IIJxx##F%#88r'   c                   |d   j                   }t        |dd  d      D ]-  \  }}|j                   }||k7  sd| d| d| d}t        |       t        j                  |      S )Nr   rq   )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
)r   	enumerate	TypeErrorr   r   )r$   r   cols_0ir8   cols_currentmsgs          r%   _concat_verticalzArrowNamespace._concat_vertical   s    Q$$s12wa0 	%EAr??Lv%))/ 1&&'S<.< 
  n$	% $$r'   c                ,    t        j                  |       S r"   )r   from_namespacer#   s    r%   	selectorszArrowNamespace.selectors   s    %44T::r'   c                0    t         j                  ||       S )N)r5   )	ArrowWhen	from_expr)r$   	predicates     r%   whenzArrowNamespace.when   s    ""9d";;r'   c          	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                @    j                   j                  } |t        j                   fd	D               }|d   j                  }
rdnd}t        d |D        di\  }}t        j                  }j                   |g ||d|i|j                        }|gS )	Nc              3  .   K   | ]  } |        y wr"   r;   r   s     r%   rX   z:ArrowNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s     %A4d2h%ArZ   r   skip	emit_nullc              3  4   K   | ]  }|j                     y wr"   r7   r^   s     r%   rX   z:ArrowNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s     :q!((:rp   	separatornull_handlingr   )	r,   ra   r   r6   r4   r   r   binary_join_element_wiser.   )r8   rf   compliant_series_listr4   r   r   separator_scalar
concat_str	compliantrg   rh   r$   r   s   `        r%   ri   z'ArrowNamespace.concat_str.<locals>.func   s    LL66E$)%%%A5%AA%! )+00D&K  $C:$9:$FO$ B 
 &(%@%@JNBN 0NN % I
 ;r'   c              3  4   K   | ]  }|j                     y wr"   rk   rm   s     r%   rX   z,ArrowNamespace.concat_str.<locals>.<genexpr>   ro   rp   rq   r   rs   rt   ru   )r$   r   rh   rg   ri   s   ```` r%   r   zArrowNamespace.concat_str   sW    	 	, zz((...2&"?"G95A ) 
 	
r'   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                    j                   j                  } |t        j                   fdD               ^}}t	        t        j                  |j                  gd |D         |j                  j                        gS )Nc              3  .   K   | ]  } |        y wr"   r;   r   s     r%   rX   z8ArrowNamespace.coalesce.<locals>.func.<locals>.<genexpr>  s     =Y4d2h=YrZ   c              3  4   K   | ]  }|j                     y wr"   r   r^   s     r%   rX   z8ArrowNamespace.coalesce.<locals>.func.<locals>.<genexpr>
  s     5O1ahh5Orp   r   )
r,   ra   r   r6   r   r   coalescer7   r4   r.   )r8   rf   r   re   rg   r$   s   `   r%   ri   z%ArrowNamespace.coalesce.<locals>.func  sr    LL66E#(%*=*==YSX=Y*Y#Z K&KK 2 2P5O5OP$)) MM r'   c              3  4   K   | ]  }|j                     y wr"   rk   rm   s     r%   rX   z*ArrowNamespace.coalesce.<locals>.<genexpr>  ro   rp   rq   r   rs   rt   ru   r   s   `` r%   r   zArrowNamespace.coalesce  sR    		 zz((...2$"?"G95A ) 
 	
r'   N)rQ   ztype[ArrowDataFrame])rQ   ztype[ArrowExpr])rQ   ztype[ArrowSeries])r/   r   rQ   None)rQ   r   )rL   r   rK   zIntoDType | NonerQ   r   )rg   r   rh   boolrQ   r   )rg   r   rQ   r   )r   zSequence[pa.Table]rQ   zpa.Table)rQ   r   )r   r   rQ   r   )rg   r   r   strrh   r   rQ   r   )__name__
__module____qualname__r   PYARROW_implementationpropertyr&   r)   r,   r0   r3   rO   rr   r   r   r   r   r   r   r   r   r   r   r   r   r;   r'   r%   r    r        s     %,,O      

$
"
"
 
*
*
*3
9
% ; ;< 
 
,/ 
?C 
	 
D
r'   r    r   c                  6    e Zd Zedd       Z	 	 	 	 	 	 	 	 ddZy)r   c                    t         S r"   )	ArrowThenr#   s    r%   _thenzArrowWhen._then  r*   r'   c                   |)t        j                  t        |      |j                        n|}t	        j
                  |||      S r"   )r   nullsr3   typer   if_else)r$   r   then	otherwises       r%   _if_then_elsezArrowWhen._if_then_else  s7     7@6GBHHSY		2Y	zz$i00r'   N)rQ   ztype[ArrowThen])r   r   r   r   r   z ArrayOrScalar | NonNestedLiteralrQ   r   )r   r   r   r   r   r  r;   r'   r%   r   r     s?     11 1 4	1 
1r'   r   c                  8    e Zd ZU dZded<   i Zded<   dZded<   y	)
r   r   intrl   r   _scalar_kwargswhenthenr   _function_nameN)r   r   r   rl   __annotations__r  r  r;   r'   r%   r   r   *  s!     FCO#%NL%$NC$r'   r   )3
__future__r   rb   	functoolsr   	itertoolsr   typingr   r   pyarrowr   pyarrow.computecomputer   narwhals._arrow.dataframer	   narwhals._arrow.exprr   narwhals._arrow.selectorsr   narwhals._arrow.seriesr   narwhals._arrow.utilsr   narwhals._compliantr   r   r   narwhals._expression_parsingr   r   narwhals._utilsr   collections.abcr   r   narwhals._arrow.typingr   r   r   narwhals._compliant.typingr   r   narwhals.typingr   r   r   r    r   r   r;   r'   r%   <module>r     s    "    )   4 * < . A H H +2QQ7';w
>;	288EVVWw
t1	.+yBSST 1 %.+y)CDi%r'   