
    WhB              
         U d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZmZmZ d dlmZ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# e	r$d dl$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d dl"m+Z+m,Z, d dl-m.Z.m/Z/ e
Z0de1d<   	 ed    Z2de1d<   ed   Z3de1d<   ee2e3f   Z4de1d<   d Z5de1d<   dZ6de1d<    G d deeeeeef         Z7 G d deeef         Z8 G d  d!eeeeef         Z9 G d" d#eeeee9f   e      Z:y)$    )annotationsN)reduce)chain)TYPE_CHECKINGAnyLiteralProtocoloverload)CompliantThenEagerNamespace	EagerWhen)combine_alias_output_namescombine_evaluate_output_namesPandasLikeDataFramePandasLikeExpr)PandasSelectorNamespacePandasLikeSeries)NativeDataFrameTNativeSeriesT)is_non_nullable_boolean)
zip_strict)IterableSequence)	TypeAlias)ScalarKwargs)ImplementationVersion)	IntoDTypeNonNestedLiteralr   
Incomplete	_Vertical   _HorizontalAxisVERTICAL
HORIZONTALc                     e Zd Zedd       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edd       Zd dZ	 	 	 	 d!dZd dZd"dZ	 	 	 	 	 	 	 	 d#dZy)$PandasLikeNamespacec                    t         S Nr   selfs    Y/var/www/html/jupyter_env/lib/python3.12/site-packages/narwhals/_pandas_like/namespace.py
_dataframezPandasLikeNamespace._dataframe5   s    ""    c                    t         S r-   r   r.   s    r0   _exprzPandasLikeNamespace._expr9   s    r2   c                    t         S r-   r   r.   s    r0   _serieszPandasLikeNamespace._series=   s    r2   c                ,    t        j                  |       S r-   )r   from_namespacer.   s    r0   	selectorszPandasLikeNamespace.selectorsA   s    &55d;;r2   c                     || _         || _        y r-   )_implementation_version)r/   implementationversions      r0   __init__zPandasLikeNamespace.__init__E   s    -r2   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                j     j                   j                  } | fdD         }t        d |      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr-    ).0r4   sdfs      r0   	<genexpr>z=PandasLikeNamespace.coalesce.<locals>.func.<locals>.<genexpr>L   s!     E559EaQEQEs   c                *    | j                  |d d       S )Nstrategylimit	fill_null)xys     r0   <lambda>z<PandasLikeNamespace.coalesce.<locals>.func.<locals>.<lambda>N   s    AKKDK$M r2   )r6   _align_full_broadcastr   )rF   alignseriesexprsr/   s   `  r0   funcz*PandasLikeNamespace.coalesce.<locals>.funcJ   s7    LL66EEEEFFMvV r2   c              3  4   K   | ]  }|j                     y wr-   _depthrD   rN   s     r0   rG   z/PandasLikeNamespace.coalesce.<locals>.<genexpr>S        .1ahh.   r%   coalescerU   depthfunction_nameevaluate_output_namesalias_output_namescontextrF   r   returnzlist[PandasLikeSeries]r4   _from_callablemaxr   r   r/   rT   rU   s   `` r0   r\   zPandasLikeNamespace.coalesceI   sR    	 zz((...2$"?"G95A ) 
 	
r2   c           	     n     d fdt        fdddd d  j                   j                        S )Nc                    j                   j                  gd| j                  j                  dd       }r|j	                        S |S )Nliteralr   r%   datanameindexrb   )r6   from_iterable_native_framero   cast)rF   pandas_seriesdtyper/   values     r0   _lit_pandas_seriesz3PandasLikeNamespace.lit.<locals>._lit_pandas_series[   sY     LL66W&&,,Qq1	 7 M $))%00  r2   c                     |       gS r-   rC   )rF   rv   s    r0   rP   z)PandasLikeNamespace.lit.<locals>.<lambda>g   s    *2./ r2   r   litc                    dgS )Nrk   rC   _dfs    r0   rP   z)PandasLikeNamespace.lit.<locals>.<lambda>j   s    yk r2   r^   r_   r`   ra   r=   r>   )rF   r   rd   r   r   r;   r<   )r/   ru   rt   rv   s   ```@r0   rx   zPandasLikeNamespace.litZ   s6    		! /"9#//MM
 	
r2   c           	     X     t         fdddd d  j                   j                        S )Nc                n    j                   j                  t        | j                        gddg      gS )Nlenr   )rn   ro   rb   )r6   rp   r   rq   )rF   r/   s    r0   rP   z)PandasLikeNamespace.len.<locals>.<lambda>r   s:    **))*+%sD +  r2   r   r   c                    dgS )Nr   rC   rz   s    r0   rP   z)PandasLikeNamespace.len.<locals>.<lambda>y   s    ug r2   r|   r}   r.   s   `r0   r   zPandasLikeNamespace.lenp   s3    
 "5#//MM
 	
r2   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                     j                   j                  }t        j                   fdD              } || }d |D        }t	        t
        j                  |      gS )Nc              3  .   K   | ]  } |        y wr-   rC   rD   exprrF   s     r0   rG   zCPandasLikeNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>        $@$T"X$@   c              3  B   K   | ]  }|j                  d dd        yw)r   NrL   rD   rE   s     r0   rG   zCPandasLikeNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>   s     HAQ[[D$7H   )r6   rQ   r   rp   r   operatoradd)rF   rR   itrS   native_seriesrT   r/   s   `    r0   rU   z0PandasLikeNamespace.sum_horizontal.<locals>.func   sO    LL66E$$$@%$@@BBZFHHM8<<788r2   c              3  4   K   | ]  }|j                     y wr-   rW   rY   s     r0   rG   z5PandasLikeNamespace.sum_horizontal.<locals>.<genexpr>   rZ   r[   r%   sum_horizontalr]   rc   re   rh   s   `` r0   r   z"PandasLikeNamespace.sum_horizontal   sR    	9 zz((...2*"?"G95A ) 
 	
r2   c          	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                "   	j                   j                  }D cg c]  } ||       D ]  }|  }}}st        d |D              rd}t        |      r	d |D        n
t	        |      }t        t        j                   ||       gS c c}}w )Nc              3     K   | ]=  }|j                   j                  d k(  xr |j                         j                          ? ywobjectNnativert   is_nullanyr   s     r0   rG   zCPandasLikeNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>   7      (EF(*@qyy{/@@(   AAzwCannot use `ignore_nulls=False` in `all_horizontal` for non-nullable NumPy-backed pandas Series when nulls are present.c              3  \   K   | ]$  }t        |      r|n|j                  d dd       & yw)TNr   rM   r   s     r0   rG   zCPandasLikeNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>   s4        13AT4QU9VV   *,)r6   rQ   r   
ValueErroriterr   r   and_
rF   rR   r4   rE   rS   msgr   rT   ignore_nullsr/   s
          r0   rU   z0PandasLikeNamespace.all_horizontal.<locals>.func   s    LL66E%*>EE"I>qa>a>F>C (JP( %
 P o%   $ &\  8==%*566# ?   Bc              3  4   K   | ]  }|j                     y wr-   rW   rY   s     r0   rG   z5PandasLikeNamespace.all_horizontal.<locals>.<genexpr>   rZ   r[   r%   all_horizontalr]   rc   re   r/   r   rT   rU   s   ``` r0   r   z"PandasLikeNamespace.all_horizontal   sR    	7* zz((...2*"?"G95A ) 
 	
r2   c          	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                "   	j                   j                  }D cg c]  } ||       D ]  }|  }}}st        d |D              rd}t        |      r	d |D        n
t	        |      }t        t        j                   ||       gS c c}}w )Nc              3     K   | ]=  }|j                   j                  d k(  xr |j                         j                          ? ywr   r   r   s     r0   rG   zCPandasLikeNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>   r   r   zwCannot use `ignore_nulls=False` in `any_horizontal` for non-nullable NumPy-backed pandas Series when nulls are present.c              3  \   K   | ]$  }t        |      r|n|j                  d dd       & yw)FNr   r   s     r0   rG   zCPandasLikeNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>   s4        13AUDRV9WWr   )r6   rQ   r   r   r   r   r   or_r   s
          r0   rU   z0PandasLikeNamespace.any_horizontal.<locals>.func   s    LL66E%*>EE"I>qa>a>F>C (JP( %
 P o%   $ &\  8<<455# ?r   c              3  4   K   | ]  }|j                     y wr-   rW   rY   s     r0   rG   z5PandasLikeNamespace.any_horizontal.<locals>.<genexpr>   rZ   r[   r%   any_horizontalr]   rc   re   r   s   ``` r0   r   z"PandasLikeNamespace.any_horizontal   sR    	6* zz((...2*"?"G95A ) 
 	
r2   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                   D cg c]  } ||       D ]  }|  }}}j                   j                  } |d |D         } |d |D         }t        t        j                  |      t        t        j                  |      z  gS c c}}w )Nc              3  D   K   | ]  }|j                  d dd        yw)r   NrI   rL   r   s     r0   rG   zDPandasLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s     R!++a$d+;Rs    c              3  B   K   | ]  }d |j                         z
    yw)r%   Nr   r   s     r0   rG   zDPandasLikeNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s     CQ_Cr   )r6   rQ   r   r   r   )	rF   r4   rE   expr_resultsrR   rS   non_narT   r/   s	          r0   rU   z1PandasLikeNamespace.mean_horizontal.<locals>.func   s    +0D%%)DQADADLDLL66ER\RF ClCDF8<<06(,,3OOPP Es   Bc              3  4   K   | ]  }|j                     y wr-   rW   rY   s     r0   rG   z6PandasLikeNamespace.mean_horizontal.<locals>.<genexpr>   rZ   r[   r%   mean_horizontalr]   rc   re   rh   s   `` r0   r   z#PandasLikeNamespace.mean_horizontal   sS    	Q zz((...2+"?"G95A ) 
 	
r2   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                Z    t        j                   fdD              }j                  j                  } || }t	        j                  d |D        d      j                  j                  d      j                  j                        j                  |d   j                        gS )	Nc              3  .   K   | ]  } |        y wr-   rC   r   s     r0   rG   zCPandasLikeNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>   r   r   c              3  <   K   | ]  }|j                           y wr-   to_framer   s     r0   rG   zCPandasLikeNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>        6!6   
horizontalhowr%   axisr=   r>   r   )r   rp   r6   rQ   r   concatrq   minr;   r<   aliasrn   rF   r   rR   rS   rT   r/   s   `   r0   rU   z0PandasLikeNamespace.min_horizontal.<locals>.func   s    $$$@%$@@BLL66EBZF !KK6v6L   #mCCQCK#'#7#7 MM %q	' r2   c              3  4   K   | ]  }|j                     y wr-   rW   rY   s     r0   rG   z5PandasLikeNamespace.min_horizontal.<locals>.<genexpr>   rZ   r[   r%   min_horizontalr]   rc   re   rh   s   `` r0   r   z"PandasLikeNamespace.min_horizontal   R    	 zz((...2*"?"G95A ) 
 	
r2   c           	          d fd} j                   j                  |t        d D              dz   dt         t	                S )Nc                Z    t        j                   fdD              }j                  j                  } || }t	        j                  d |D        d      j                  j                  d      j                  j                        j                  |d   j                        gS )	Nc              3  .   K   | ]  } |        y wr-   rC   r   s     r0   rG   zCPandasLikeNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>  r   r   c              3  <   K   | ]  }|j                           y wr-   r   r   s     r0   rG   zCPandasLikeNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>  r   r   r   r   r%   r   r   r   )r   rp   r6   rQ   r   r   r   rg   r;   r<   r   rn   r   s   `   r0   rU   z0PandasLikeNamespace.max_horizontal.<locals>.func   s    $$$@%$@@BLL66EBZF !KK6v6L   fSSaS[#'#7#7 MM %q	' r2   c              3  4   K   | ]  }|j                     y wr-   rW   rY   s     r0   rG   z5PandasLikeNamespace.max_horizontal.<locals>.<genexpr>  rZ   r[   r%   max_horizontalr]   rc   re   rh   s   `` r0   r   z"PandasLikeNamespace.max_horizontal   r   r2   c                J    | j                   j                         j                  S )zvConcatenate pandas objects along a particular axis.

        Return the **native** equivalent of `pd.concat`.
        )r;   to_native_namespacer   r.   s    r0   _concatzPandasLikeNamespace._concat  s     ##779@@@r2   c                   | j                   j                         r'| j                  dk  r| j                  |t        d      S | j                  |t              S )N   Fr   copyr   )r;   	is_pandas_backend_versionr   r(   r/   dfss     r0   _concat_diagonalz$PandasLikeNamespace._concat_diagonal   sI    ))+0E0E0L<<(<??||Ch|//r2   c                  | j                   j                         rQt        j                         5  t        j                  ddt
               | j                  |t              cd d d        S | j                   j                         r'| j                  dk  r| j                  |t        d      S | j                  |t              S # 1 sw Y    xY w)NignorezDThe behavior of array concatenation with empty entries is deprecated)messagecategoryr   r   Fr   )
r;   is_cudfwarningscatch_warningsfilterwarningsFutureWarningr   r)   r   r   r   s     r0   _concat_horizontalz&PandasLikeNamespace._concat_horizontal%  s     '')((* :''b*
 ||Cj|9: : !!++-$2G2G$2N<<*5<AA||Cj|11: :s   3CCc                  |d   j                   }t        |dd  d      D ]n  \  }}|j                   }t        |      t        |      k(  r||k(  j                         r=d|j	                          d| d|j	                          d}t        |       | j                  j                         r'| j                  dk  r| j                  |t        d	
      S | j                  |t              S )Nr   r%   )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
r   Fr   r   )columns	enumerater   allto_list	TypeErrorr;   r   r   r   r(   )r/   r   cols_0irF   cols_currentr   s          r0   _concat_verticalz$PandasLikeNamespace._concat_vertical4  s    Qs12wa0 
	%EAr::L\"c&k18N7S7S7U))/)9(: ;&&'S<+?+?+A*B"F 
  n$
	% ))+0E0E0L<<(<??||Ch|//r2   c                >    t         t           j                  ||       S )N)rb   )
PandasWhenr   	from_expr)r/   	predicates     r0   whenzPandasLikeNamespace.whenE  s    -(229d2KKr2   c          	           j                   j                  j                         d fd} j                  j	                  |t        d D              dz   dt         t                S )Nc                   D cg c]  } ||       D ]  }|  }}}j                   j                  } |fd|D         } |d |D         }s=t        t        j                  |      }t        fd|      j                  | d       }|gS t        ||      D 	cg c]  \  }}	|j                  |	 d       c}	}^}
}|
j                  gt        |
      z  d|
j                  j                        fd|d d D        }t        t        j                  d	 t        ||      D        |
      }|gS c c}}w c c}	}w )
Nc              3  @   K   | ]  }|j                          y wr-   )rr   )rD   rE   strings     r0   rG   z?PandasLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>P  s     BQVVF^Bs   c              3  <   K   | ]  }|j                           y wr-   r   r   s     r0   rG   z?PandasLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>Q  s     B		Br   c                    | z   |z   S r-   rC   )rN   rO   	separators     r0   rP   z>PandasLikeNamespace.concat_str.<locals>.func.<locals>.<lambda>U  s    Q]Q-> r2    seprl   c              3  D   K   | ]  }j                  | d         yw)r  N)zip_with)rD   nm	sep_arrays     r0   rG   z?PandasLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>f  s      Sbi00"b9Ss    c              3  ,   K   | ]  \  }}||z     y wr-   rC   )rD   rE   vs      r0   rG   z?PandasLikeNamespace.concat_str.<locals>.func.<locals>.<genexpr>i  s     Ftq!QUFs   )r6   rQ   r   r   r   r  r   rp   r   r   ro   r   )rF   r4   rE   r   rR   rS   	null_masknull_mask_resultresultr  
init_valuevalues
separatorsr	  rT   r   r/   r  r   s                @r0   rU   z,PandasLikeNamespace.concat_str.<locals>.funcM  sK   +0D%%)DQADADLDLL66EB\BCFB\BCI#)(,,	#B  >GPP%%t0 8O! 6@	5R',1ArAJJsB''#
V '44#s:6$++11 	 5 	 TIcrNS
LLFz*f'EF 8O? E's   D5D;c              3  4   K   | ]  }|j                     y wr-   rW   rY   s     r0   rG   z1PandasLikeNamespace.concat_str.<locals>.<genexpr>q  rZ   r[   r%   
concat_strr]   rc   )r<   dtypesStringr4   rf   rg   r   r   )r/   r  r   rT   rU   r   s   ```` @r0   r  zPandasLikeNamespace.concat_strH  sr     %%,,. 	  	D zz((...2&"?"G95A ) 
 	
r2   N)rd   ztype[PandasLikeDataFrame])rd   ztype[PandasLikeExpr])rd   ztype[PandasLikeSeries])rd   r   )r=   r   r>   r    rd   None)rT   r   rd   r   )ru   r"   rt   zIntoDType | Nonerd   r   )rd   r   )rT   r   r   boolrd   r   )rd   z._NativeConcat[NativeDataFrameT, NativeSeriesT])r   zSequence[NativeDataFrameT]rd   r   )r   z*Sequence[NativeDataFrameT | NativeSeriesT]rd   r   )r   r   rd   zPandasWhen[NativeSeriesT])rT   r   r  strr   r  rd   r   )__name__
__module____qualname__propertyr1   r4   r6   r9   r?   r\   rx   r   r   r   r   r   r   r   r   r   r   r   r   r  rC   r2   r0   r+   r+   ,   s    # #       < < 
"
,
 
"
$
48
	
B
$
48
	
B
&
2
2 A A0
2=2	20"L.
$.
14.
DH.
	.
r2   r+   c                      e Zd Zedd	 	 	 	 	 	 	 d	d       Zedd	 	 	 	 	 	 	 d
d       Zedd	 	 	 	 	 	 	 dd       Zedd	 	 	 	 	 	 	 dd       Zdd	 	 	 	 	 	 	 ddZy)_NativeConcat.)r   c                    y r-   rC   r/   objsr   r   s       r0   __call__z_NativeConcat.__call__z       r2   c                    y r-   rC   r!  s       r0   r#  z_NativeConcat.__call__  s     r2   c                    y r-   rC   r!  s       r0   r#  z_NativeConcat.__call__  r$  r2   c                    y r-   rC   r!  s       r0   r#  z_NativeConcat.__call__  s     ,/r2   Nc                    y r-   rC   r!  s       r0   r#  z_NativeConcat.__call__  s     ,/r2   )r"  zIterable[NativeDataFrameT]r   r$   r   bool | Nonerd   r   )r"  zIterable[NativeSeriesT]r   r$   r   r)  rd   r   )r"  *Iterable[NativeDataFrameT | NativeSeriesT]r   r&   r   r)  rd   r   )r"  r*  r   r'   r   r)  rd   z NativeDataFrameT | NativeSeriesT)r  r  r  r
   r#  rC   r2   r0   r  r  y  s$     ( 	
  
  UX+6?GR	    8 	
  
    /8/ 	/
 / 
*/ / !/8/ 	/
 / 
*/r2   r  c                  6    e Zd Zedd       Z	 	 	 	 	 	 	 	 ddZy)r   c                    t         S r-   )
PandasThenr.   s    r0   _thenzPandasWhen._then  s
     r2   c                @    |j                   }| ||      S  |||      S r-   )where)r/   r   then	otherwiser0  s        r0   _if_then_elsezPandasWhen._if_then_else  s(     !JJ'/uT{KU45KKr2   N)rd   ztype[PandasThen])r   r   r1  r   r2  z NativeSeriesT | NonNestedLiteralrd   r   )r  r  r  r  r.  r3  rC   r2   r0   r   r     sF      LL L 4	L
 
Lr2   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   intrX   r   _scalar_kwargswhenthenr  _function_nameN)r  r  r  rX   __annotations__r6  r8  rC   r2   r0   r-  r-    s!     FCO#%NL%$NC$r2   r-  );
__future__r   r   r   	functoolsr   	itertoolsr   typingr   r   r   r	   r
   narwhals._compliantr   r   r   narwhals._expression_parsingr   r   narwhals._pandas_like.dataframer   narwhals._pandas_like.exprr   narwhals._pandas_like.selectorsr   narwhals._pandas_like.seriesr   narwhals._pandas_like.typingr   r   narwhals._pandas_like.utilsr   narwhals._utilsr   collections.abcr   r   typing_extensionsr   narwhals._compliant.typingr   r   r    narwhals.typingr!   r"   r#   r9  r$   r&   r'   r(   r)   r+   r  r   r-  rC   r2   r0   <module>rK     s$   "     B B H H @ 5 C 9 H ? &2+77; 
I  > qz	9 ! Y #)[01i 1) 
K J
		J
Z
$/H-}<= $/NL!#3^]RSL&%%'7ST%r2   