
    7^j7              
      >   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mZ d dl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*m+Z+m,Z,  G d deeeeejZ                  df         Z.y)    )annotationsN)reduce)chain)TYPE_CHECKINGLiteralArrowDataFrame	ArrowExpr)ArrowSelectorNamespaceArrowSeries)build_list_arraycast_to_comparable_string_types)EagerNamespace)combine_alias_output_namescombine_evaluate_output_names)Implementation)IteratorSequence)ChunkedArrayAny
Incomplete	ScalarAny)Version)CorrelationMethod	IntoDTypePythonLiteralc                  6   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d#dZed$d       Z	 	 	 	 	 	 	 	 d%dZd"dZd"dZd"dZ	 d&	 	 	 	 	 	 	 d'dZd(dZd)dZy)*ArrowNamespacec                    t         S Nr   selfs    d/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/narwhals/_arrow/namespace.py
_dataframezArrowNamespace._dataframe$   s        c                    t         S r!   r
   r"   s    r$   _exprzArrowNamespace._expr(   s    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__0   s	    r&   c                    d |D        S )Nc              3  j   K   | ]+  }|j                   r|j                  d    n|j                   - yw)r   N)
_broadcastnative.0ss     r$   	<genexpr>z0ArrowNamespace.extract_native.<locals>.<genexpr>6   s'     Jaq||9Js   13 )r#   seriess     r$   extract_nativezArrowNamespace.extract_native3   s     K6JJr&   c                J      j                   fdd d  j                        S )Nc                ^    t        j                  t        | j                        gd      gS )Nlen)namecontext)r   from_iterabler<   r2   )dfr#   s    r$   <lambda>z$ArrowNamespace.len.<locals>.<lambda>;   s(    ))3ryy>*:PTU r&   c                    dgS )Nr<   r7   _dfs    r$   rA   z$ArrowNamespace.len.<locals>.<lambda>>   s    ug r&   evaluate_output_namesalias_output_namesr-   r(   r,   r"   s   `r$   r<   zArrowNamespace.len8   s-    zz #6#MM  
 	
r&   c                `     d fd j                  fdd d  j                        S )Nc                `    t        j                  gd      }r|j                        S |S )Nliteraldatar=   r>   )r   r?   cast)_arrow_seriesdtyper#   values     r$   _lit_arrow_seriesz-ArrowNamespace.lit.<locals>._lit_arrow_seriesD   s8    &44W9dL #((//r&   c                     |       gS r!   r7   )r@   rS   s    r$   rA   z$ArrowNamespace.lit.<locals>.<lambda>M   s    )"-. r&   c                    dgS )NrK   r7   rC   s    r$   rA   z$ArrowNamespace.lit.<locals>.<lambda>N   s    yk r&   rE   )rO   r	   returnr   rH   )r#   rR   rQ   rS   s   ```@r$   litzArrowNamespace.litC   s0    	  zz."9#MM	  
 	
r&   c               j    dfd}| j                   j                  |t         t         |       S )Nc                     t        j                   fdD              }r	d |D        }t        t        j                  |      gS )Nc              3  .   K   | ]  } |        y wr!   r7   r4   er@   s     r$   r6   z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>U        ?U!"?U   c              3  B   K   | ]  }|j                  d dd        yw)TN	fill_nullr3   s     r$   r6   z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>W   s     HA!++dD$7H   )r   r?   r   operatorand_r@   r8   exprsignore_nullss   ` r$   funcz+ArrowNamespace.all_horizontal.<locals>.funcT   s:    ,1,?,??Uu?U,UFHH8==&122r&   rh   rF   rG   r>   r@   r	   rV   zlist[ArrowSeries]r(   _from_callabler   r   r#   rg   rf   rh   s    `` r$   all_horizontalzArrowNamespace.all_horizontalS   s<    	3 zz(("?"G95A	 ) 
 	
r&   c               j    dfd}| j                   j                  |t         t         |       S )Nc                     t        j                   fdD              }r	d |D        }t        t        j                  |      gS )Nc              3  .   K   | ]  } |        y wr!   r7   r[   s     r$   r6   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>c   r]   r^   c              3  B   K   | ]  }|j                  d dd        yw)FNr`   r3   s     r$   r6   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>e   s     IQ!++eT48Irb   )r   r?   r   rc   or_re   s   ` r$   rh   z+ArrowNamespace.any_horizontal.<locals>.funcb   s:    ,1,?,??Uu?U,UFI&I8<<011r&   ri   rj   rk   rm   s    `` r$   any_horizontalzArrowNamespace.any_horizontala   s<    	2 zz(("?"G95A	 ) 
 	
r&   c                f    dfd}| j                   j                  |t         t         |       S )Nc                     t        j                   fdD              }d |D        }t        t        j                  |      gS )Nc              3  .   K   | ]  } |        y wr!   r7   r4   exprr@   s     r$   r6   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>q   s     $@$T"X$@r^   c              3  D   K   | ]  }|j                  d dd        yw)r   Nstrategylimitr`   r3   s     r$   r6   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>r   s     LAakk!d$k?Ls    )r   r?   r   rc   add)r@   itr8   rf   s   `  r$   rh   z+ArrowNamespace.sum_horizontal.<locals>.funcp   s7    $$$@%$@@BLLF8<<011r&   ri   rj   rk   r#   rf   rh   s    ` r$   sum_horizontalzArrowNamespace.sum_horizontalo   s<    	2
 zz(("?"G95A	 ) 
 	
r&   c                    | j                   j                  j                         dfd}| j                  j	                  |t         t         |       S )Nc                t    t        t        j                   fdD                    }|D cg c]  }|j                  dd d        }}|D cg c]$  }d|j	                         j                        z
  & }}t        t        j                  |      t        t        j                  |      z  gS c c}w c c}w )Nc              3  .   K   | ]  } |        y wr!   r7   rx   s     r$   r6   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s     4P$T"X4Pr^   r   r{      )	tupler   r?   ra   is_nullrN   r   rc   r~   )r@   expr_resultsr5   r8   non_narf   int_64s   `    r$   rh   z,ArrowNamespace.mean_horizontal.<locals>.func   s     !4!44P%4P!PQLIUVAakk!d$k?VFV<HIqa!))+**622IFI8<<06(,,3OOPP WIs   B0)B5ri   rj   )r,   dtypesInt64r(   rl   r   r   )r#   rf   rh   r   s    ` @r$   mean_horizontalzArrowNamespace.mean_horizontal|   sU    %%++-	Q zz(("?"G95A	 ) 
 	
r&   c                j     d fd} j                   j                  |t         t                S )Nc                "    t        t        j                   fdD                    ^}}t        t        j
                  |D cg c]  }|j                   c}|j                        }t        ||j                  j                        gS c c}w )Nc              3  .   K   | ]  } |        y wr!   r7   rx   s     r$   r6   z>ArrowNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>        <X$T"X<Xr^   r=   r-   )
r   r   r?   r   pcmin_element_wiser2   r   r=   r,   r@   init_seriesr8   r5   native_seriesrf   r#   s   `    r$   rh   z+ArrowNamespace.min_horizontal.<locals>.func   x    #()<)<<XRW<X)X#Y K&"##%?1ahh%?ASASM M0@0@$--X  &@   B
ri   rj   rk   r   s   `` r$   min_horizontalzArrowNamespace.min_horizontal   <    	 zz(("?"G95A	 ) 
 	
r&   c                j     d fd} j                   j                  |t         t                S )Nc                "    t        t        j                   fdD                    ^}}t        t        j
                  |D cg c]  }|j                   c}|j                        }t        ||j                  j                        gS c c}w )Nc              3  .   K   | ]  } |        y wr!   r7   rx   s     r$   r6   z>ArrowNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>   r   r^   r   )
r   r   r?   r   r   max_element_wiser2   r   r=   r,   r   s   `    r$   rh   z+ArrowNamespace.max_horizontal.<locals>.func   r   r   ri   rj   rk   r   s   `` r$   max_horizontalz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r4   r@   s     r$   r6   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>   s     (GR(G   c              3  <   K   | ]  }|j                           y wr!   )itercolumnsr   s     r$   r6   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>   s     *J2>>+;*Js   )names)listr   r?   r   r   Tablefrom_arrays)r#   r   r   arrayss       r$   _concat_horizontalz!ArrowNamespace._concat_horizontal   sR    U(((G3(GGHu***Jc*JJKxx##F%#88r&   c                   |d   j                   }t        |dd  d      D ]-  \  }}|j                   x}|k7  sd| d| d| d}t        |       t        j                  |      S )Nr   r   )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
)r   	enumerate	TypeErrorr   r   )r#   r   cols_0ir@   cols_currentmsgs          r$   _concat_verticalzArrowNamespace._concat_vertical   s    Q$$s12wa0 	%EAr "/F:))/ 1&&'S<.< 
  n$	% $$r&   c                ,    t        j                  |       S r!   )r   from_namespacer"   s    r$   	selectorszArrowNamespace.selectors   s    %44T::r&   c               r     d fd} j                   j                  |t         t                S )Nc                ,    t        t        j                   fdD                    }|d   j                  }	rdnd}t	         
j
                  | di\  }}t        j                  }
j                   |g ||d|i|
j                        }|gS )Nc              3  .   K   | ]  } |        y wr!   r7   rx   s     r$   r6   z:ArrowNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s     -I4d2h-Ir^   r   skip	emit_null	separatornull_handlingr   )
r   r   r?   r=   r   r9   r   binary_join_element_wiser*   r,   )r@   r8   r=   r   r   separator_scalar
concat_str	compliantrf   rg   r#   r   s   `       r$   rh   z'ArrowNamespace.concat_str.<locals>.func   s    %---I5-IIJF!9>>D&K  $C$$$f-$9B$ B 
 &(%@%@JNBN 0NN % I
 ;r&   ri   rj   rk   )r#   r   rg   rf   rh   s   ```` r$   r   zArrowNamespace.concat_str   sA    	 	& zz(("?"G95A	 ) 
 	
r&   c                j     d fd} j                   j                  |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!   r7   rx   s     r$   r6   z8ArrowNamespace.coalesce.<locals>.func.<locals>.<genexpr>   s     =Y4d2h=Yr^   c              3  4   K   | ]  }|j                     y wr!   )r2   r3   s     r$   r6   z8ArrowNamespace.coalesce.<locals>.func.<locals>.<genexpr>   s     5O1ahh5Or   r   )
r*   _align_full_broadcastr   r?   r   r   coalescer2   r=   r,   )r@   alignr   r8   rf   r#   s   `   r$   rh   z%ArrowNamespace.coalesce.<locals>.func   sr    LL66E#(%*=*==YSX=Y*Y#Z K&KK 2 2P5O5OP$)) MM r&   ri   rj   rk   r   s   `` r$   r   zArrowNamespace.coalesce   s<    		 zz(("?"G95A	 ) 
 	
r&   c                j     d fd} j                   j                  |t         t                S )Nc                (    t        t        j                   fdD                    }|d   j                  }t	        j
                  d |D        dt        d |D              i}t        j                  |g      }t        ||j                        gS )Nc              3  .   K   | ]  } |        y wr!   r7   rx   s     r$   r6   z6ArrowNamespace.struct.<locals>.func.<locals>.<genexpr>        .JDtBx.Jr^   r   c              3  n   K   | ]-  }t        |      d kD  r|j                  n|j                  d    / yw)r   r   N)r<   r2   r3   s     r$   r6   z6ArrowNamespace.struct.<locals>.func.<locals>.<genexpr>   s+     Jac!fqj!((ahhqk9Js   35field_namesc              3  4   K   | ]  }|j                     y wr!   )r=   r3   s     r$   r6   z6ArrowNamespace.struct.<locals>.func.<locals>.<genexpr>   s     !9Q!&&!9r   r   )
r   r   r?   r=   r   make_structr   chunked_arrayr   r,   )r@   r8   r=   struct_arrayresultrf   r#   s   `    r$   rh   z#ArrowNamespace.struct.<locals>.func   s~    5...JE.JJKF!9>>D>>J6J!!9&!99L %%|n5FT4==IJJr&   ri   rj   rk   r   s   `` r$   structzArrowNamespace.struct   s=    		K zz(("?"G95A	 ) 
 	
r&   c                j     d fd} j                   j                  |t         t                S )Nc                    t        t        j                   fdD                    }t        |D cg c]  }|j                  j                          c}      }t        ||d   j                  j                        gS c c}w )Nc              3  .   K   | ]  } |        y wr!   r7   rx   s     r$   r6   z4ArrowNamespace.list.<locals>.func.<locals>.<genexpr>  r   r^   r   r   )	r   r   r?   r   r2   combine_chunksr   r=   r,   )r@   r8   r5   r   rf   r#   s   `   r$   rh   z!ArrowNamespace.list.<locals>.func  sd    5...JE.JJKF%&&QQqxx'>'>'@&QRFVAY^^T]]STT 'Rs   !Bri   rj   rk   r   s   `` r$   r   zArrowNamespace.list  s=    	U zz(("?"G95A	 ) 
 	
r&   Nc                    |)t        j                  t        |      |j                        n|}t	        j
                  |j                  t        j                  d            ||      S )NF)r   nullsr<   typer   if_elsera   scalar)r#   whenthen	otherwises       r$   _if_then_elsezArrowNamespace._if_then_else  sI     7@6GBHHSY		2Y	zz$..5)9:D)LLr&   c                    |dk7  rd}t        |      d fd} j                  j                  |t              t	                     S )Npearsonz4Only 'pearson' correlation is supported for Pyarrow.c                *   | j                        }|j                  }| j                        j                  }t        j                  t        j                  |      t        j                  |            }t        j
                  ||      }t        j
                  ||      }t        j                  |t        j                  |            }t        j                  |t        j                  |            }t        j                  t        j                  ||            }t        j                  |      }t        j                  |      }	t        j                  |t        j                  ||	            }
t        j                  |
g|j                        gS )NrL   )_evaluate_single_output_exprr2   r   rd   is_validfiltersubtractmeanmultiplystddevdivider   r?   r=   )r@   a_seriesarr1arr2validdev1dev2
covariancestd1std2correlationabr#   s              r$   rh   z!ArrowNamespace.corr.<locals>.func'  s   66q9H??D2215<<DGGBKK-r{{4/@AE99T5)D99T5)D;;tRWWT]3D;;tRWWT]3DT4!89J99T?D99T?D))JD$0GHK))%X]]D r&   ri   rj   )NotImplementedErrorr(   rl   r   r   )r#   r  r  methodr   rh   s   ```   r$   corrzArrowNamespace.corr"  sX    YHC%c**	. zz(("?1"E9!Q?	 ) 
 	
r&   c                    d fd} j                   j                  |t              t                     S )Nc                T   | j                  
      }|j                  }| j                        j                  }t        j                  t        j                  |      t        j                  |            }t        j
                  ||      }t        j
                  ||      }t        |      }|z
  dk  r*t        j                  d t        j                               }nt        j                  |t        j                  |            }t        j                  |t        j                  |            }t        j                  t        j                  ||            }	t        j                  |	t        j                  |z
              }t        j                   |g|j"                        gS )Nr   )r   rL   )r   r2   r   rd   r   r   r<   r   r   float64r   r   sumr   r   r   r?   r=   )r@   r   r   r   r   nr  r   r   	numeratorr  r  ddofr#   s             r$   rh   z ArrowNamespace.cov.<locals>.funcF  s+   66q9H??D2215<<DGGBKK-r{{4/@AE99T5)D99T5)DD	A4x1}  YYt"**,?
{{47{{47FF2;;tT#:;	YYy"))AH2EF
))$HMM4 r&   ri   rj   rk   )r#   r  r  r  rh   s   ```` r$   covzArrowNamespace.covE  sE    	 	. zz(("?1"E9!Q?	 ) 
 	
r&   )rV   ztype[ArrowDataFrame])rV   ztype[ArrowExpr])rV   ztype[ArrowSeries])r-   r   rV   None)r8   r   rV   z%Iterator[ChunkedArrayAny | ScalarAny])rV   r   )rR   r   rQ   zIntoDType | NonerV   r   )rf   r   rg   boolrV   r   )rf   r   rV   r   )r   zSequence[pa.Table]rV   zpa.Table)rV   r   )rf   r   r   strrg   r  rV   r   r!   )r   r   r   r   r   zChunkedArrayAny | NonerV   r   )r  r   r  r   r  r   rV   r   )r  r   r  r   r  intrV   r   )__name__
__module____qualname__r   PYARROW_implementationpropertyr%   r(   r*   r.   r9   r<   rW   rn   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  r7   r&   r$   r   r      s%    %,,O      K"K	.K
	

 



 
"
"3
9

% ; ;

,/
?C
	
:
&
&
$ -1	MM M *	M
 
M!
F
r&   r   r   )/
__future__r   rc   	functoolsr   	itertoolsr   typingr   r   pyarrowr   pyarrow.computecomputer   narwhals._arrow.dataframer	   narwhals._arrow.exprr   narwhals._arrow.selectorsr   narwhals._arrow.seriesr   narwhals._arrow.utilsr   r   narwhals._compliantr   narwhals._expression_parsingr   r   narwhals._utilsr   collections.abcr   r   narwhals._arrow.typingr   r   r   r   narwhals.typingr   r   r   r   r   r7   r&   r$   <module>r.     so    "    )   4 * < . S . +2MM'KKC
>;	288EVVWC
r&   