
    hh-              
         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(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j0        df                   Z1 G d deeeedf                   Z2 G d deeeee2f         e          Z3dS )    )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	ScalarAny)ScalarKwargs)Version)	IntoDTypeNonNestedLiteralc                      e Zd Zej        Zed0d            Zed1d            Zed2d            Z	d3dZ
d4dZd5dZd6dZd7dZd7dZd8dZd8dZd8dZd8dZd9d#Zd9d$Zd9d%Zed:d'            Zd;d*Zd<d-Zd8d.Zd/S )=ArrowNamespacereturntype[ArrowDataFrame]c                    t           S Nr   selfs    P/var/www/histauto/venv/lib/python3.11/site-packages/narwhals/_arrow/namespace.py
_dataframezArrowNamespace._dataframe*   s        type[ArrowExpr]c                    t           S r%   r
   r&   s    r(   _exprzArrowNamespace._expr.       r*   type[ArrowSeries]c                    t           S r%   r   r&   s    r(   _serieszArrowNamespace._series2   s    r*   versionr   Nonec                   || _         d S r%   )_version)r'   r2   s     r(   __init__zArrowNamespace.__init__6   s    r*   seriesr   %Iterator[ChunkedArrayAny | ScalarAny]c                    d |D             S )Nc              3  L   K   | ]}|j         r|j        d          n|j        V   dS )r   N)
_broadcastnative.0ss     r(   	<genexpr>z0ArrowNamespace.extract_native.<locals>.<genexpr><   s5      JJaq|9JJJJJJr*    )r'   r7   s     r(   extract_nativezArrowNamespace.extract_native9   s     KJ6JJJJr*   r   c                L                            fdddd d  j                  S )Nc                Z    t          j        t          | j                  gd          gS )Nlen)namecontext)r   from_iterablerE   r<   )dfr'   s    r(   <lambda>z$ArrowNamespace.len.<locals>.<lambda>A   s+    )3ry>>*:PTUUU r*   r   rE   c                    dgS )NrE   rA   _dfs    r(   rJ   z$ArrowNamespace.len.<locals>.<lambda>F   s    ug r*   depthfunction_nameevaluate_output_namesalias_output_namesr2   r-   r5   r&   s   `r(   rE   zArrowNamespace.len>   sH    zz    "5"5#M  	
 	
 		
r*   valuer   dtypeIntoDType | Nonec                b     d fd                      fdddd	 d  j        
          S )N_r	   r"   r   c                f    t          j        gd          }r|                              S |S )Nliteral)datarF   rG   )r   rH   cast)rX   arrow_seriesrU   r'   rT   s     r(   _lit_arrow_seriesz-ArrowNamespace.lit.<locals>._lit_arrow_seriesL   sG    &4W9d  L  0#((///r*   c                     |           gS r%   rA   )rI   r^   s    r(   rJ   z$ArrowNamespace.lit.<locals>.<lambda>U   s    ))"--. r*   r   litc                    dgS )NrZ   rA   rL   s    r(   rJ   z$ArrowNamespace.lit.<locals>.<lambda>X   s    yk r*   rN   )rX   r	   r"   r   rS   )r'   rT   rU   r^   s   ```@r(   r`   zArrowNamespace.litK   sk    	  	  	  	  	  	  	  	  zz...."9"9#M  
 
 	
r*   exprsignore_nullsboolc          	         d
fd}| j                             |t          d D                       dz   dt           t	           | 	          S )NrI   r	   r"   list[ArrowSeries]c                     t          j         fdD                       }rd |D             }t          t          j        |          gS )Nc              3  .   K   | ]} |          V  d S r%   rA   r>   erI   s     r(   r@   z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>_   +      ?U?U!"?U?U?U?U?U?Ur*   c              3  D   K   | ]}|                     d dd          V  dS )TN	fill_nullr=   s     r(   r@   z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>a   s2      HHA!++dD$77HHHHHHr*   )r   rH   r   operatorand_rI   r7   rb   rc   s   ` r(   funcz+ArrowNamespace.all_horizontal.<locals>.func^   sZ    ,1,??U?U?U?Uu?U?U?U,U,UF IHHHHH8=&1122r*   c              3  $   K   | ]}|j         V  d S r%   _depthr>   xs     r(   r@   z0ArrowNamespace.all_horizontal.<locals>.<genexpr>f   $      ..1ah......r*      all_horizontalrr   rO   rP   rQ   rR   rG   rI   r	   r"   rf   r-   _from_callablemaxr   r   r'   rc   rb   rr   s    `` r(   rz   zArrowNamespace.all_horizontal]   s    	3 	3 	3 	3 	3 	3 	3 z((.......2*"?"G95A ) 
 
 	
r*   c          	         d
fd}| j                             |t          d D                       dz   dt           t	           | 	          S )NrI   r	   r"   rf   c                     t          j         fdD                       }rd |D             }t          t          j        |          gS )Nc              3  .   K   | ]} |          V  d S r%   rA   ri   s     r(   r@   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>o   rk   r*   c              3  D   K   | ]}|                     d dd          V  dS )FNrm   r=   s     r(   r@   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>q   s2      IIQ!++eT488IIIIIIr*   )r   rH   r   ro   or_rq   s   ` r(   rr   z+ArrowNamespace.any_horizontal.<locals>.funcn   sZ    ,1,??U?U?U?Uu?U?U?U,U,UF JII&III8<0011r*   c              3  $   K   | ]}|j         V  d S r%   rt   rv   s     r(   r@   z0ArrowNamespace.any_horizontal.<locals>.<genexpr>v   rx   r*   ry   any_horizontalr{   r|   r}   r   s    `` r(   r   zArrowNamespace.any_horizontalm   s    	2 	2 	2 	2 	2 	2 	2 z((.......2*"?"G95A ) 
 
 	
r*   c           	         d
fd}| j                             |t          d D                       dz   dt           t	           | 	          S )NrI   r	   r"   rf   c                     t          j         fdD                       }d |D             }t          t          j        |          gS )Nc              3  .   K   | ]} |          V  d S r%   rA   r>   exprrI   s     r(   r@   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>   s+      $@$@$TT"XX$@$@$@$@$@$@r*   c              3  F   K   | ]}|                     d dd          V  dS r   N)strategylimitrm   r=   s     r(   r@   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>   s4      LLAakk!d$k??LLLLLLr*   )r   rH   r   ro   add)rI   itr7   rb   s   `  r(   rr   z+ArrowNamespace.sum_horizontal.<locals>.func~   sQ    $$@$@$@$@%$@$@$@@@BLLLLLF8<0011r*   c              3  $   K   | ]}|j         V  d S r%   rt   rv   s     r(   r@   z0ArrowNamespace.sum_horizontal.<locals>.<genexpr>   rx   r*   ry   sum_horizontalr{   r|   r}   r'   rb   rr   s    ` r(   r   zArrowNamespace.sum_horizontal}   sz    	2 	2 	2 	2 	2 	2
 z((.......2*"?"G95A ) 
 
 	
r*   c           	         | j         j                                        d
fd}| j                            |t          d D                       dz   dt           t           | 	          S )NrI   r	   r"   rf   c                     t          t          j         fdD                                 }d |D             }fd|D             }t          t          j        |          t          t          j        |          z  gS )Nc              3  .   K   | ]} |          V  d S r%   rA   r   s     r(   r@   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s+      4P4P$TT"XX4P4P4P4P4P4Pr*   c                >    g | ]}|                     d dd          S r   rm   r=   s     r(   
<listcomp>z@ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<listcomp>   s*    VVVAakk!d$k??VVVr*   c                d    g | ],}d |                                                               z
  -S )ry   )is_nullr\   )r>   r?   int_64s     r(   r   z@ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<listcomp>   s4    IIIqa!))++**6222IIIr*   )tupler   rH   r   ro   r   )rI   expr_resultsr7   non_narb   r   s   `   r(   rr   z,ArrowNamespace.mean_horizontal.<locals>.func   s     !44P4P4P4P%4P4P4P!P!PQQLVVVVVFIIIILIIIF8<006(,3O3OOPPr*   c              3  $   K   | ]}|j         V  d S r%   rt   rv   s     r(   r@   z1ArrowNamespace.mean_horizontal.<locals>.<genexpr>   rx   r*   ry   mean_horizontalr{   r|   )r5   dtypesInt64r-   r~   r   r   r   )r'   rb   rr   r   s    ` @r(   r   zArrowNamespace.mean_horizontal   s    %++--	Q 	Q 	Q 	Q 	Q 	Q 	Q z((.......2+"?"G95A ) 
 
 	
r*   c           	          d
 fd} j                             |t          d D                       dz   dt           t	            	          S )NrI   r	   r"   rf   c                     t          t          j         fdD                                 ^}}t          t          j        d |D             |j                  }t          ||j        j	                  gS )Nc              3  .   K   | ]} |          V  d S r%   rA   r   s     r(   r@   z>ArrowNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>   +      <X<X$TT"XX<X<X<X<X<X<Xr*   c                    g | ]	}|j         
S rA   r<   r=   s     r(   r   z?ArrowNamespace.min_horizontal.<locals>.func.<locals>.<listcomp>       %?%?%?1ah%?%?%?r*   rF   r2   )
r   r   rH   r   pcmin_element_wiser<   r   rF   r5   rI   init_seriesr7   native_seriesrb   r'   s   `   r(   rr   z+ArrowNamespace.min_horizontal.<locals>.func       #()<<X<X<X<XRW<X<X<X)X)X#Y#Y K&"#%?%?%?%?%?AS M M0@$-XXX r*   c              3  $   K   | ]}|j         V  d S r%   rt   rv   s     r(   r@   z0ArrowNamespace.min_horizontal.<locals>.<genexpr>   rx   r*   ry   min_horizontalr{   r|   r}   r   s   `` r(   r   zArrowNamespace.min_horizontal       	 	 	 	 	 	 	 z((.......2*"?"G95A ) 
 
 	
r*   c           	          d
 fd} j                             |t          d D                       dz   dt           t	            	          S )NrI   r	   r"   rf   c                     t          t          j         fdD                                 ^}}t          t          j        d |D             |j                  }t          ||j        j	                  gS )Nc              3  .   K   | ]} |          V  d S r%   rA   r   s     r(   r@   z>ArrowNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>   r   r*   c                    g | ]	}|j         
S rA   r   r=   s     r(   r   z?ArrowNamespace.max_horizontal.<locals>.func.<locals>.<listcomp>   r   r*   r   )
r   r   rH   r   r   max_element_wiser<   r   rF   r5   r   s   `   r(   rr   z+ArrowNamespace.max_horizontal.<locals>.func   r   r*   c              3  $   K   | ]}|j         V  d S r%   rt   rv   s     r(   r@   z0ArrowNamespace.max_horizontal.<locals>.<genexpr>   rx   r*   ry   max_horizontalr{   r|   r}   r   s   `` r(   r   zArrowNamespace.max_horizontal   r   r*   dfsSequence[pa.Table]pa.Tablec               p    | j         dk    rt          j        |d          S t          j        |d          S )N)   default)promote_optionsT)promote)_backend_versionpaconcat_tables)r'   r   s     r(   _concat_diagonalzArrowNamespace._concat_diagonal   s<     E))#CCCCCT2222r*   c                   t          t          j        d |D                                 }t          t          j        d |D                                 }t          j                            ||          S )Nc              3  $   K   | ]}|j         V  d S r%   )column_namesr>   rI   s     r(   r@   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>   s$      (G(GR(G(G(G(G(G(Gr*   c              3  >   K   | ]}|                                 V  d S r%   )itercolumnsr   s     r(   r@   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>   s,      *J*J2>>+;+;*J*J*J*J*J*Jr*   )names)listr   rH   r   r   Tablefrom_arrays)r'   r   r   arrayss       r(   _concat_horizontalz!ArrowNamespace._concat_horizontal   sk    U((G(G3(G(G(GGGHHu**J*Jc*J*J*JJJKKx##F%#888r*   c                   |d         j         }t          |dd          d          D ]-\  }}|j         }||k    rd| d| d| d}t          |          .t          j        |          S )Nr   ry   )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
)r   	enumerate	TypeErrorr   r   )r'   r   cols_0irI   cols_currentmsgs          r(   _concat_verticalzArrowNamespace._concat_vertical   s    Q$s122wa000 	% 	%EAr?Lv%%<)/< <&'< <+7< < < 
  nn$ & $$$r*   r   c                *    t          j        |           S r%   )r   from_namespacer&   s    r(   	selectorszArrowNamespace.selectors   s    %4T:::r*   	predicate	ArrowWhenc                :    t                               ||           S )N)rG   )r   	from_expr)r'   r   s     r(   whenzArrowNamespace.when   s    ""9d";;;r*   	separatorstrc          	          d
 fd} j                             |t          d D                       dz   dt           t	            	          S )NrI   r	   r"   rf   c                "    t          t          j         fdD                                 }|d         j        }	rdnd}t	           
j        | di\  }}t          j        }
                     |g ||R d|i|
j	                  }|gS )Nc              3  .   K   | ]} |          V  d S r%   rA   r   s     r(   r@   z:ArrowNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s+      -I-I4dd2hh-I-I-I-I-I-Ir*   r   skip	emit_nullr   null_handlingr   )
r   r   rH   rF   r   rB   r   binary_join_element_wiser1   r5   )rI   r7   rF   r   r   separator_scalar
concat_str	compliantrb   rc   r'   r   s   `       r(   rr   z'ArrowNamespace.concat_str.<locals>.func   s    %--I-I-I-I5-I-I-IIIJJF!9>D&7K  $C$$f-$9B$ $ B 
 &(%@J
NBN 0NNNNN %  I
 ;r*   c              3  $   K   | ]}|j         V  d S r%   rt   rv   s     r(   r@   z,ArrowNamespace.concat_str.<locals>.<genexpr>   rx   r*   ry   r   r{   r|   r}   )r'   r   rc   rb   rr   s   ```` r(   r   zArrowNamespace.concat_str   s    	 	 	 	 	 	 	 	 	& z((.......2&"?"G95A ) 
 
 	
r*   c           	          d
 fd} j                             |t          d D                       dz   dt           t	            	          S )NrI   r	   r"   rf   c                     j         j        } |t          j         fdD                        ^}}t	          t          j        |j        gd |D             R  |j        j	                  gS )Nc              3  .   K   | ]} |          V  d S r%   rA   r   s     r(   r@   z8ArrowNamespace.coalesce.<locals>.func.<locals>.<genexpr>  s+      =Y=Y4dd2hh=Y=Y=Y=Y=Y=Yr*   c              3  $   K   | ]}|j         V  d S r%   r   r=   s     r(   r@   z8ArrowNamespace.coalesce.<locals>.func.<locals>.<genexpr>  s$      5O5O1ah5O5O5O5O5O5Or*   r   )
r1   _align_full_broadcastr   rH   r   r   coalescer<   rF   r5   )rI   alignr   r7   rb   r'   s   `   r(   rr   z%ArrowNamespace.coalesce.<locals>.func  s    L6E#(5%*==Y=Y=Y=YSX=Y=Y=Y*Y*Y#Z K&K 2P5O5O5O5O5OPPP$) M   r*   c              3  $   K   | ]}|j         V  d S r%   rt   rv   s     r(   r@   z*ArrowNamespace.coalesce.<locals>.<genexpr>  rx   r*   ry   r   r{   r|   r}   r   s   `` r(   r   zArrowNamespace.coalesce  s    		 		 		 		 		 		 		 z((.......2$"?"G95A ) 
 
 	
r*   N)r"   r#   )r"   r+   )r"   r/   )r2   r   r"   r3   )r7   r   r"   r8   )r"   r   )rT   r   rU   rV   r"   r   )rb   r   rc   rd   r"   r   )rb   r   r"   r   )r   r   r"   r   )r"   r   )r   r   r"   r   )rb   r   r   r   rc   rd   r"   r   )__name__
__module____qualname__r   PYARROW_implementationpropertyr)   r-   r1   r6   rB   rE   r`   rz   r   r   r   r   r   r   r   r   r   r   r   r   rA   r*   r(   r!   r!   %   s        %,O   X    X    X       K K K K

 
 
 

 
 
 
$
 
 
 
 
 
 
 
 
 
 
 

 
 
 
$
 
 
 
&
 
 
 
&3 3 3 3
9 9 9 9
% % % % ; ; ; X;< < < <
 
 
 
>
 
 
 
 
 
r*   r!   r   c                  .    e Zd Zedd            Zdd	Zd
S )r   r"   type[ArrowThen]c                    t           S r%   )	ArrowThenr&   s    r(   _thenzArrowWhen._then  r.   r*   r   r   then	otherwise ArrayOrScalar | NonNestedLiteralc                   |'t          j        t          |          |j                  n|}t	          j        |||          S r%   )r   nullsrE   typer   if_else)r'   r   r  r  s       r(   _if_then_elsezArrowWhen._if_then_else  s<     7@6GBHSYY	222Y	z$i000r*   N)r"   r  )r   r   r  r   r  r	  r"   r   )r   r   r   r  r  r  rA   r*   r(   r   r     sF           X1 1 1 1 1 1r*   r   c                  :    e Zd ZU dZded<   i Zded<   dZded<   d	S )
r  r   intru   r   _scalar_kwargswhenthenr   _function_nameN)r   r   r   ru   __annotations__r  r  rA   r*   r(   r  r  '  sC          FOOOO#%N%%%%$N$$$$$$r*   r  )4
__future__r   ro   	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   r   narwhals._compliant.typingr   r   narwhals.typingr   r   r   r!   r   r  rA   r*   r(   <module>r(     s   " " " " " "              ) ) ) ) ) ) ) )           4 4 4 4 4 4 * * * * * * < < < < < < . . . . . . A A A A A A H H H H H H H H H H        + * * * * * <22222222            877777'''''';;;;;;;;o
 o
 o
 o
 o
>;	28EVVWo
 o
 o
d1 1 1 1 1	.+yBSST 1 1 1 % % % % %.+y)CDi% % % % %r*   