
    O3j4                    \   d dl mZ d dlZ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 d dl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mZmZ d dlmZm Z  d dl!m"Z"m#Z#m$Z$ e
r#d dl%m&Z&m'Z' d dl(mc m)Z* d dl!m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0  G d deeeejb                  f   eeef         Z2y)    )annotationsN)datedatetime)reduce)chain)TYPE_CHECKINGcast)DepthTrackingNamespaceLazyNamespaceDaskLazyFrameDaskExpr)DaskSelectorNamespace)align_series_full_broadcastnarwhals_to_native_dtypevalidate_comparand)combine_alias_output_namescombine_evaluate_output_names)Implementationis_nested_literalnot_implemented)IterableIterator)Version)ConcatMethodCorrelationMethod	IntoDTypeNonNestedLiteralc                     e Zd ZU ej                  Zded<   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 dZd!dZ e       Zy)"DaskNamespacer   _implementationc                ,    t        j                  |       S N)r   from_namespaceselfs    E/DATA/.local/lib/python3.12/site-packages/narwhals/_dask/namespace.py	selectorszDaskNamespace.selectors/   s    $33D99    c                    t         S r$   r   r&   s    r(   _exprzDaskNamespace._expr3   s    r*   c                    t         S r$   r   r&   s    r(   
_lazyframezDaskNamespace._lazyframe7   s    r*   c                   || _         y r$   )_version)r'   versions     r(   __init__zDaskNamespace.__init__;   s	    r*   c                     t              r#dt              j                   }t        |      d fd} j	                  |d d  j
                        S )Nz<Nested structures are not supported for Dask backend, found c                   0t        j                        }t        j                  g|d      }nTt	        t
              r,t	        t              sd}t        j                  g|d      }nt        j                  gd      }| j                  j                  }t        j                  ||      }|d   j                         gS )Nliteral)dtypenamezdate32[pyarrow])r7   )npartitionsr   )r   r0   pdSeries
isinstancer   r   _native_framer8   ddfrom_pandas	to_series)dfnative_dtypenative_pd_seriesr8   dask_seriesr6   r'   values        r(   funczDaskNamespace.lit.<locals>.funcC   s     7t}}M#%99eWLy#Y E4(x2
  1#%99eWLy#Y #%99eW9#E **66K..)9{SKN,,.//r*   c                    dgS Nr5    _dfs    r(   <lambda>z#DaskNamespace.lit.<locals>.<lambda>V   s    ykr*   evaluate_output_namesalias_output_namesr1   r@   r   returnzlist[dx.Series])r   type__name__NotImplementedErrorr,   r0   )r'   rD   r6   msgrE   s   ```  r(   litzDaskNamespace.lit>   s]    U#PQUV[Q\QeQePfgC%c**	0" zz"9#MM	  
 	
r*   c                J    dd}| j                  |d d | j                        S )Nc                l    | j                   | j                  d      j                  j                         gS )Nr   )r<   columnssizer?   )r@   s    r(   rE   zDaskNamespace.len.<locals>.func\   s-    $$RZZ]388BBDEEr*   c                    dgS )NlenrH   rI   s    r(   rK   z#DaskNamespace.len.<locals>.<lambda>b   s    ugr*   rL   rO   )r,   r0   )r'   rE   s     r(   r[   zDaskNamespace.len[   s0    	F zz"5#MM	  
 	
r*   c               j    dfd}| j                  |t         t         | j                        S )Nc                     t        j                   fdD              }r	d |D        }t        t        j                  t         g|       gS )Nc              3  .   K   | ]  } |        y wr$   rH   .0er@   s     r(   	<genexpr>z=DaskNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>i        =SUaeU   c              3  `   K   | ]&  }|j                   d k(  r|n|j                  d       ( yw)boolTNr6   fillnar`   ss     r(   rb   z=DaskNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>n   s)     Ufqww&0!ahhtnDf   ,.)r   from_iterabler   operatorand_r   r@   seriesexprsignore_nullss   ` r(   rE   z*DaskNamespace.all_horizontal.<locals>.funch   sG    */*=*==SU=S*SF UfU8==*Eb*R6*RSTTr*   callrM   rN   r1   rO   r,   r   r   r0   r'   rr   rq   rE   s    `` r(   all_horizontalzDaskNamespace.all_horizontalg   s;    	U zz"?"G95AMM	  
 	
r*   c               j    dfd}| j                  |t         t         | j                        S )Nc                     t        j                   fdD              }r	d |D        }t        t        j                  t         g|       gS )Nc              3  .   K   | ]  } |        y wr$   rH   r_   s     r(   rb   z=DaskNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>z   rc   rd   c              3  `   K   | ]&  }|j                   d k(  r|n|j                  d       ( yw)rf   FNrg   ri   s     r(   rb   z=DaskNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>|   s)     Vv!qww&0!ahhuoEvrk   )r   rl   r   rm   or_r   ro   s   ` r(   rE   z*DaskNamespace.any_horizontal.<locals>.funcy   sE    */*=*==SU=S*SFVvV8<<)DR)Q&)QRSSr*   rs   rO   ru   rv   s    `` r(   any_horizontalzDaskNamespace.any_horizontalx   s;    	T zz"?"G95AMM	  
 	
r*   c                f    dfd}| j                  |t         t         | j                        S )Nc                     t         g fdD         }t        j                  |d      j                  d      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr$   rH   r`   r,   rj   r@   s      r(   rb   z=DaskNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>        >UEE"IqaIaU      axis)r   r=   concatsumr@   rp   rq   s   ` r(   rE   z*DaskNamespace.sum_horizontal.<locals>.func   sA    0>U>F IIf1-11q19::r*   rs   rO   ru   r'   rq   rE   s    ` r(   sum_horizontalzDaskNamespace.sum_horizontal   s:    	; zz"?"G95AMM	  
 	
r*   c               P   |sd}t        |      |D cg c]  }|j                   }}|d   j                  }|dk(  rt        |dd  d      D ]n  \  }}|j                  }t	        |      t	        |      k(  r||k(  j                         r=d|j                          d| d|j                          d	}t        |       t        t        j                  |dd
      | j                        S |dk(  r-t        t        j                  |dd      | j                        S t        c c}w )NzNo items to concatenater   verticalr   )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
inner)r   join)r1   diagonalouter)AssertionErrorr<   rX   	enumerater[   allto_list	TypeErrorr   r=   r   r0   rS   )	r'   itemshowrT   idfscols_0r@   cols_currents	            r(   r   zDaskNamespace.concat   s-    +C %%(-.1q.Q*"3qr7!42!zz&#f+5LF<R;W;W;Y--3^^-=,> ?**+B|/C/C/E.FbJ 
 $C.( 5 !		#AG4dmm  * 		#AG4dmm  "!- /s   D#c                f    dfd}| j                  |t         t         | j                        S )Nc                    D cg c]  } ||       D ]  }|  }}}t        | gd |D         }t        | gd |D         }t        d |      }t        d |      }t        d||z        gS c c}}w )Nc              3  >   K   | ]  }|j                  d         yw)r   Nrh   ri   s     r(   rb   z>DaskNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s     6YLqqxx{Ls   c              3  B   K   | ]  }d |j                         z
    yw)r   N)isnari   s     r(   rb   z>DaskNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>   s     9Lqa!&&(lLs   c                    | |z   S r$   rH   xys     r(   rK   z=DaskNamespace.mean_horizontal.<locals>.func.<locals>.<lambda>       a!er*   c                    | |z   S r$   rH   r   s     r(   rK   z=DaskNamespace.mean_horizontal.<locals>.func.<locals>.<lambda>   r   r*   z	dx.Series)r   r   r	   )	r@   r,   rj   expr_resultsrp   non_nanumdenrq   s	           r(   rE   z+DaskNamespace.mean_horizontal.<locals>.func   s    +0D5%%)QA)A5LD0Z6YL6YZF09L9F +V4C+V4CcCi011 Es   A0rs   rO   ru   r   s    ` r(   mean_horizontalzDaskNamespace.mean_horizontal   s:    	2 zz"?"G95AMM	  
 	
r*   c                f    dfd}| j                  |t         t         | j                        S )Nc                     t         g fdD         }t        j                  |d      j                  d      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr$   rH   r   s      r(   rb   z=DaskNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>   r   r   r   r   )r   r=   r   minr   s   ` r(   rE   z*DaskNamespace.min_horizontal.<locals>.func   A    0>U>F IIf1-11q19::r*   rs   rO   ru   r   s    ` r(   min_horizontalzDaskNamespace.min_horizontal   :    	; zz"?"G95AMM	  
 	
r*   c                f    dfd}| j                  |t         t         | j                        S )Nc                     t         g fdD         }t        j                  |d      j                  d      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr$   rH   r   s      r(   rb   z=DaskNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>   r   r   r   r   )r   r=   r   maxr   s   ` r(   rE   z*DaskNamespace.max_horizontal.<locals>.func   r   r*   rs   rO   ru   r   s    ` r(   max_horizontalzDaskNamespace.max_horizontal   r   r*   c          	         dfd}| j                  |t        d   dd       t        d   dd       | j                        S )Nc           	     :   D cg c]  } ||       D ]  }|  }}}d t        | g| D        }t        | g| D cg c]  }|j                          }}s=t        t        j                  |      }t        fd|      j                  | d       }|gS t        ||d      D cg c]  \  }}|j                  | d       c}}^}	}
fd|d d D        }t        t        j                  d t        ||
d      D        |	      }|gS c c}}w c c}w c c}}w )	Nc              3  F   K   | ]  }|j                  t                y wr$   )astypestrri   s     r(   rb   z9DaskNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s      'U!'Us   !c                    | z   |z   S r$   rH   )r   r   	separators     r(   rK   z8DaskNamespace.concat_str.<locals>.func.<locals>.<lambda>   s    Q]Q->r*   T)strict c              3  R   K   | ]  }|j                  d dt                 yw)r   )TF)metaN)mapr   )r`   nmr   s     r(   rb   z9DaskNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s+      , FF"Y7cFB,s   $'c              3  ,   K   | ]  \  }}||z     y wr$   rH   )r`   rj   vs      r(   rb   z9DaskNamespace.concat_str.<locals>.func.<locals>.<genexpr>   s     L'Ktq!QU'Ks   )r   r   r   rm   r|   wherezipadd)r@   r,   rj   r   rp   	null_masknull_mask_resultresultr   
init_valuevalues
separatorsrq   rr   r   s               r(   rE   z&DaskNamespace.concat_str.<locals>.func   s4   +0D5%%)QA)A5LD'B2'U'UF ,Gr+YL+YZ+Ya+YIZ#)(,,	#B  >GMM%%t$ 8O 36fiPT2U'2UBAGGRC$2U'#
V'n
  LLLs:vd'KL 8O5 E ['s   DD*Dr   _evaluate_output_namesc                    dgS rG   rH   rI   s    r(   rK   z*DaskNamespace.concat_str.<locals>.<lambda>  s    r*   _alias_output_namesrs   rO   r,   getattrr0   )r'   r   rr   rq   rE   s    ``` r(   
concat_strzDaskNamespace.concat_str   sR    	: zz")a24K#  'uQx1FMMM  
 	
r*   c                f    dfd}| j                  |t         t         | j                        S )Nc                L     t         g fdD         }t        d |      gS )Nc              3  <   K   | ]  } |      D ]  }|   y wr$   rH   r   s      r(   rb   z7DaskNamespace.coalesce.<locals>.func.<locals>.<genexpr>  r   r   c                $    | j                  |      S r$   r   r   s     r(   rK   z6DaskNamespace.coalesce.<locals>.func.<locals>.<lambda>  s    r*   )r   r   r   s   ` r(   rE   z$DaskNamespace.coalesce.<locals>.func  s0    0>U>F 3V<==r*   rs   rO   ru   r   s    ` r(   coalescezDaskNamespace.coalesce  s:    	> zz"?"G95AMM	  
 	
r*   Nc           	         dfd}| j                  |t        dd       t        dd       | j                        S )Nc                   | j                  	      }| j                        n}| j                        }t        d 	fn	fD              r@| j                  |j                               }| j                  j	                               }|} .t        | ||      \  }}t        ||       |j                  |      gS t        | |||      \  }}}t        ||       t        ||       |j                  ||      gS )Nc              3  H   K   | ]  }|j                   j                    y wr$   )	_metadatais_scalar_like)r`   r   s     r(   rb   z8DaskNamespace.when_then.<locals>.func.<locals>.<genexpr>'  s)      6  **6s    ")_evaluate_single_output_exprr   _with_nativeto_frame	broadcastr   r   r   )
r@   
then_valueotherwise_value	conditionnew_dfthen_seriesotherwise_series	otherwise	predicatethens
          r(   rE   z%DaskNamespace.when_then.<locals>.func  s6   88>J ( //	:  77	BI   !( %#T956  ););)=>;;I<O<O<QR	 +F	:,(K #9k:#)))4559TIz?:6Y%5 y+6y*:;%%i1ABCCr*   r   c                    dgS rG   rH   rI   s    r(   rK   z)DaskNamespace.when_then.<locals>.<lambda>C  s    YKr*   r   rs   rO   r   )r'   r   r   r   rE   s    ``` r(   	when_thenzDaskNamespace.when_then  sL    "	DH zz").0G#  't-BDIMM  
 	
r*   c               ~    dfd}| j                  |t              t              | j                        S )Nc                    | j                        }| j                        }|j                  |      j                         gS )N)method)r   corrr?   )r@   a_b_abr   s      r(   rE   z DaskNamespace.corr.<locals>.funcJ  sB    003B003BGGBvG.88:;;r*   rs   rO   ru   )r'   r   r   r   rE   s    ``` r(   r   zDaskNamespace.corrI  s>    	<
 zz"?1"E9!Q?MM	  
 	
r*   )rP   r   )rP   ztype[DaskExpr])rP   ztype[DaskLazyFrame])r1   r   rP   None)rD   r   r6   zIntoDType | NonerP   r   )rP   r   )rq   r   rr   rf   rP   r   )rq   r   rP   r   )r   zIterable[DaskLazyFrame]r   r   rP   r   )rq   r   r   r   rr   rf   rP   r   r$   )r   r   r   r   r   zDaskExpr | NonerP   r   )r   r   r   r   r   r   rP   r   )rR   
__module____qualname__r   DASKr"   __annotations__propertyr)   r,   r.   r2   rU   r[   rw   r}   r   r   r   r   r   r   r   r   r   r   structrH   r*   r(   r!   r!   )   s     '5&9&9O^9: :     
:


"

","6B"	"<
$

'
'
+.'
>B'
	'
R
 QU.
!.
)1.
>M.
	.
`
 Fr*   r!   )3
__future__r   rm   r   r   	functoolsr   	itertoolsr   typingr   r	   dask.dataframe	dataframer=   pandasr9   narwhals._compliantr
   r   narwhals._dask.dataframer   narwhals._dask.exprr   narwhals._dask.selectorsr   narwhals._dask.utilsr   r   r   narwhals._expression_parsingr   r   narwhals._utilsr   r   r   collections.abcr   r   dask.dataframe.dask_expr	dask_exprdxr   narwhals.typingr   r   r   r   	DataFramer!   rH   r*   r(   <module>r     s    "  #   &   E 2 ( : 
 O N2))' m-2<<78=(23mr*   