o
    Lh3                     @  s|  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
mZ d dl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 d d
lmZmZmZ d dlmZm Z m!Z! d dl"m#Z#m$Z$ erd dl%m&Z&m'Z'm(Z( d dl)m  m*Z+ d dl,m-Z- d dl"m.Z. d dl/m0Z0m1Z1m2Z2 G dd deeeej3f eeef Z4G dd deedef Z5G dd deedee5f eZ6dS )    )annotationsN)reduce)chain)TYPE_CHECKINGcast)CompliantThenCompliantWhenDepthTrackingNamespaceLazyNamespaceDaskLazyFrameDaskExpr)DaskSelectorNamespace)align_series_full_broadcastnarwhals_to_native_dtypevalidate_comparand)ExprKindcombine_alias_output_namescombine_evaluate_output_names)Implementation
zip_strict)IterableIteratorSequence)ScalarKwargs)Version)ConcatMethod	IntoDTypeNonNestedLiteralc                   @  s   e Zd ZU ejZded< ed<ddZed=dd	Z	ed>ddZ
d?ddZd@ddZdAddZdBddZdBd d!ZdCd"d#ZdDd)d*ZdCd+d,ZdCd-d.ZdCd/d0ZdEd3d4ZdFd7d8ZdCd9d:Zd;S )GDaskNamespacer   _implementationreturnr   c                 C  s
   t | S N)r   from_namespaceself r'   Y/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/narwhals/_dask/namespace.py	selectors0   s   
zDaskNamespace.selectorstype[DaskExpr]c                 C     t S r#   r   r%   r'   r'   r(   _expr4      zDaskNamespace._exprtype[DaskLazyFrame]c                 C  r+   r#   r   r%   r'   r'   r(   
_lazyframe8   r-   zDaskNamespace._lazyframeversionr   Nonec                C  s
   || _ d S r#   )_version)r&   r0   r'   r'   r(   __init__<   s   
zDaskNamespace.__init__valuer   dtypeIntoDType | Noner   c                   s.   d fdd}j |ddd	d
 d jdS )Ndfr   r"   list[dx.Series]c                   s\    d urt  j}tjg|dd}ntjgdd}| jj}tj||d}|d  gS )Nliteral)r5   name)r:   )npartitionsr   )	r   r2   pdSeries_native_framer;   ddfrom_pandas	to_series)r7   native_dtypenative_pd_seriesr;   dask_seriesr5   r&   r4   r'   r(   func@   s   zDaskNamespace.lit.<locals>.funcr   litc                 S     dgS Nr9   r'   _dfr'   r'   r(   <lambda>N       z#DaskNamespace.lit.<locals>.<lambda>depthfunction_nameevaluate_output_namesalias_output_namesr0   r7   r   r"   r8   r,   r2   )r&   r4   r5   rF   r'   rE   r(   rG   ?   s   
zDaskNamespace.litc                 C  s&   ddd}| j |ddd	d
 d | jdS )Nr7   r   r"   r8   c                 S  s   | j | jd  j gS Nr   )r>   columnssizerA   r7   r'   r'   r(   rF   T   s   zDaskNamespace.len.<locals>.funcr   lenc                 S  rH   )NrY   r'   rJ   r'   r'   r(   rL   \   rM   z#DaskNamespace.len.<locals>.<lambda>rN   rS   rT   )r&   rF   r'   r'   r(   rY   S   s   
zDaskNamespace.lenexprsignore_nullsboolc                  B   d fdd}| j |tdd  D d	 d
t  t  | jdS )Nr7   r   r"   r8   c                   D   t  fddD }rdd |D }ttjt g|R  gS )Nc                 3      | ]}| V  qd S r#   r'   .0erX   r'   r(   	<genexpr>c       z=DaskNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>c                 s  (    | ]}|j d kr|n|dV  qdS )r\   TNr5   fillnara   sr'   r'   r(   rc   h      & )r   from_iterabler   operatorand_r   r7   seriesrZ   r[   rX   r(   rF   b   s   z*DaskNamespace.all_horizontal.<locals>.funcc                 s      | ]}|j V  qd S r#   _depthra   xr'   r'   r(   rc   m       z/DaskNamespace.all_horizontal.<locals>.<genexpr>   all_horizontalcallrO   rP   rQ   rR   r0   rS   r,   maxr   r   r2   r&   r[   rZ   rF   r'   rp   r(   rx   a   s   	zDaskNamespace.all_horizontalc                  r]   )Nr7   r   r"   r8   c                   r^   )Nc                 3  r_   r#   r'   r`   rX   r'   r(   rc   v   rd   z=DaskNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>c                 s  re   )r\   FNrf   rh   r'   r'   r(   rc   x   rj   )r   rk   r   rl   or_r   rn   rp   rX   r(   rF   u   s   z*DaskNamespace.any_horizontal.<locals>.funcc                 s  rq   r#   rr   rt   r'   r'   r(   rc   }   rv   z/DaskNamespace.any_horizontal.<locals>.<genexpr>rw   any_horizontalry   rS   r{   r}   r'   rp   r(   r   t   s   zDaskNamespace.any_horizontalc                   @   d fdd}| j |tdd  D d	 d
t  t  | jdS )Nr7   r   r"   r8   c                   6   t  g fddD R  }tj|ddjddgS )Nc                 3  "    | ]}| D ]}|V  qqd S r#   r'   ra   r,   ri   rX   r'   r(   rc           z=DaskNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>rw   axis)r   r?   concatsumrn   rZ   rX   r(   rF      s   z*DaskNamespace.sum_horizontal.<locals>.funcc                 s  rq   r#   rr   rt   r'   r'   r(   rc      rv   z/DaskNamespace.sum_horizontal.<locals>.<genexpr>rw   sum_horizontalry   rS   r{   r&   rZ   rF   r'   r   r(   r         zDaskNamespace.sum_horizontalitemsIterable[DaskLazyFrame]howr   r   c          	      C  s   |sd}t |dd |D }|d j}|dkrYt|dd  ddD ])\}}|j}t|t|kr7||k sKd|  d	| d
|  d}t|q"ttj	|ddd| j
dS |dkrjttj	|ddd| j
dS t)NzNo items to concatenatec                 S  s   g | ]}|j qS r'   )r>   )ra   ir'   r'   r(   
<listcomp>   s    z(DaskNamespace.concat.<locals>.<listcomp>r   verticalrw   )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
inner)r   join)r0   diagonalouter)AssertionErrorrV   	enumeraterY   allto_list	TypeErrorr   r?   r   r2   NotImplementedError)	r&   r   r   msgdfscols_0r   r7   cols_currentr'   r'   r(   r      s6   
zDaskNamespace.concatc                   r   )Nr7   r   r"   r8   c                   sr    fddD }t  gdd |D R  }t  gdd |D R  }tdd |}tdd |}td	|| gS )
Nc                      g | ]}| D ]}|qqS r'   r'   r   rX   r'   r(   r          z?DaskNamespace.mean_horizontal.<locals>.func.<locals>.<listcomp>c                 s  s    | ]}| d V  qdS )r   Nrg   rh   r'   r'   r(   rc      s    z>DaskNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>c                 s  s    | ]	}d |   V  qdS )rw   Nisnarh   r'   r'   r(   rc          c                 S     | | S r#   r'   ru   yr'   r'   r(   rL          z=DaskNamespace.mean_horizontal.<locals>.func.<locals>.<lambda>c                 S  r   r#   r'   r   r'   r'   r(   rL      r   	dx.Series)r   r   r   )r7   expr_resultsro   non_nanumdenr   rX   r(   rF      s   z+DaskNamespace.mean_horizontal.<locals>.funcc                 s  rq   r#   rr   rt   r'   r'   r(   rc      rv   z0DaskNamespace.mean_horizontal.<locals>.<genexpr>rw   mean_horizontalry   rS   r{   r   r'   r   r(   r      s   
zDaskNamespace.mean_horizontalc                   r   )Nr7   r   r"   r8   c                   r   )Nc                 3  r   r#   r'   r   rX   r'   r(   rc      r   z=DaskNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>rw   r   )r   r?   r   minrn   r   rX   r(   rF         z*DaskNamespace.min_horizontal.<locals>.funcc                 s  rq   r#   rr   rt   r'   r'   r(   rc      rv   z/DaskNamespace.min_horizontal.<locals>.<genexpr>rw   min_horizontalry   rS   r{   r   r'   r   r(   r         zDaskNamespace.min_horizontalc                   r   )Nr7   r   r"   r8   c                   r   )Nc                 3  r   r#   r'   r   rX   r'   r(   rc      r   z=DaskNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>rw   r   )r   r?   r   r|   rn   r   rX   r(   rF      r   z*DaskNamespace.max_horizontal.<locals>.funcc                 s  rq   r#   rr   rt   r'   r'   r(   rc      rv   z/DaskNamespace.max_horizontal.<locals>.<genexpr>rw   max_horizontalry   rS   r{   r   r'   r   r(   r      r   zDaskNamespace.max_horizontal	predicateDaskWhenc                 C  s   t j|| dS )N)context)r   	from_expr)r&   r   r'   r'   r(   when   s   zDaskNamespace.when	separatorstrc             	     sX   d fdd}| j |tdd  D d	 d
t d ddd t d dd | jdS )Nr7   r   r"   r8   c           	        s    fddD }dd t  g|R  D }dd t  g|R  D }s<ttj|}tfdd|| d }|gS dd t||D ^}}fd	d|d d
 D }ttjdd t||D |}|gS )Nc                   r   r'   r'   r   rX   r'   r(   r      r   z:DaskNamespace.concat_str.<locals>.func.<locals>.<listcomp>c                 s  s    | ]}| tV  qd S r#   )astyper   rh   r'   r'   r(   rc      s    

z9DaskNamespace.concat_str.<locals>.func.<locals>.<genexpr>c                 S  s   g | ]}|  qS r'   r   rh   r'   r'   r(   r      s    c                   s   |   | S r#   r'   r   r   r'   r(   rL      s    z8DaskNamespace.concat_str.<locals>.func.<locals>.<lambda>c                 S  s   g | ]\}}| | d qS ) )where)ra   ri   nmr'   r'   r(   r      s    c                 3  s$    | ]}|j d  dtdV  qdS )r   )TF)metaN)mapr   )ra   r   r   r'   r(   rc      s
    
c                 s  s    | ]	\}}|| V  qd S r#   r'   )ra   ri   vr'   r'   r(   rc     r   )r   r   rl   r~   r   r   add)	r7   r   ro   	null_masknull_mask_resultresult
init_valuevalues
separatorsrZ   r[   r   rX   r(   rF      s.   


z&DaskNamespace.concat_str.<locals>.funcc                 s  rq   r#   rr   rt   r'   r'   r(   rc     rv   z+DaskNamespace.concat_str.<locals>.<genexpr>rw   
concat_strr   _evaluate_output_namesc                 S  rH   rI   r'   rJ   r'   r'   r(   rL     rM   z*DaskNamespace.concat_str.<locals>.<lambda>_alias_output_namesry   rS   )r,   r|   getattrr2   )r&   r   r[   rZ   rF   r'   r   r(   r      s   zDaskNamespace.concat_strc                   r   )Nr7   r   r"   r8   c                   s.   t  g fddD R  }tdd |gS )Nc                 3  r   r#   r'   r   rX   r'   r(   rc     r   z7DaskNamespace.coalesce.<locals>.func.<locals>.<genexpr>c                 S  s
   |  |S r#   r   r   r'   r'   r(   rL     s   
 z6DaskNamespace.coalesce.<locals>.func.<locals>.<lambda>)r   r   rn   r   rX   r(   rF     s   z$DaskNamespace.coalesce.<locals>.funcc                 s  rq   r#   rr   rt   r'   r'   r(   rc     rv   z)DaskNamespace.coalesce.<locals>.<genexpr>rw   coalescery   rS   r{   r   r'   r   r(   r     r   zDaskNamespace.coalesceN)r"   r   )r"   r*   )r"   r.   )r0   r   r"   r1   )r4   r   r5   r6   r"   r   )r"   r   )rZ   r   r[   r\   r"   r   )rZ   r   r"   r   )r   r   r   r   r"   r   )r   r   r"   r   )rZ   r   r   r   r[   r\   r"   r   )__name__
__module____qualname__r   DASKr!   __annotations__propertyr)   r,   r/   r3   rG   rY   rx   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r    *   s*   
 











+r    c                   @  s$   e Zd ZedddZddd	Zd
S )r   r"   type[DaskThen]c                 C  r+   r#   )DaskThenr%   r'   r'   r(   _then(  r-   zDaskWhen._thenr7   r   Sequence[dx.Series]c                 C  s   t | jtr| |d n| j}t | jtr| |d n| j}| |d }| jjd us/J | jjjrH|| }| j	t
j|d }|}| jd u r`t|||\}}t|| ||gS t||||\}}}t|| t|| |||gS rU   )
isinstance_then_valuer   _otherwise_value
_condition	_metadatais_scalar_like_with_nativeto_frame	broadcastr   AGGREGATIONr   r   r   )r&   r7   
then_valueotherwise_value	conditionnew_dfthen_seriesotherwise_seriesr'   r'   r(   __call__,  s4   







zDaskWhen.__call__N)r"   r   )r7   r   r"   r   )r   r   r   r   r   r   r'   r'   r'   r(   r   '  s    r   r   c                   @  s2   e Zd ZU dZded< i Zded< dZded< d	S )
r   r   intrs   r   _scalar_kwargswhenthenr   _function_nameN)r   r   r   rs   r   r   r   r'   r'   r'   r(   r   O  s   
 r   )7
__future__r   rl   	functoolsr   	itertoolsr   typingr   r   dask.dataframe	dataframer?   pandasr<   narwhals._compliantr   r   r	   r
   narwhals._dask.dataframer   narwhals._dask.exprr   narwhals._dask.selectorsr   narwhals._dask.utilsr   r   r   narwhals._expression_parsingr   r   r   narwhals._utilsr   r   collections.abcr   r   r   dask.dataframe.dask_expr	dask_exprdxnarwhals._compliant.typingr   r   narwhals.typingr   r   r   	DataFramer    r   r   r'   r'   r'   r(   <module>   s6    

 ~"(