o
    Lh.                  	   @  sj  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rd 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+ d dl,m-Z-m.Z. G dd deeeeej/df Z0G dd deeeedf Z1G dd deeeee1f eZ2d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)ScalarKwargs)Version)	IntoDTypeNonNestedLiteralc                   @  s   e Zd ZejZed?ddZed@ddZedAd	d
Z	dBddZ
dCddZdDddZdEddZdEddZdFd d!ZdFd"d#ZdFd$d%ZdFd&d'ZdGd+d,ZdGd-d.ZdGd/d0ZedHd2d3ZdId6d7ZdJd:d;ZdFd<d=Zd>S )KArrowNamespacereturntype[ArrowDataFrame]c                 C     t S Nr   self r%   Z/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/narwhals/_arrow/namespace.py
_dataframe%      zArrowNamespace._dataframetype[ArrowExpr]c                 C  r!   r"   r	   r#   r%   r%   r&   _expr)   r(   zArrowNamespace._exprtype[ArrowSeries]c                 C  r!   r"   r   r#   r%   r%   r&   _series-   r(   zArrowNamespace._seriesversionr   Nonec                C  s
   || _ d S r"   )_version)r$   r-   r%   r%   r&   __init__1   s   
zArrowNamespace.__init__r
   c                   s$    j  fdddddd d  jdS )Nc                   s   t jt| jgd dgS )Nlen)namecontext)r   from_iterabler1   nativedfr#   r%   r&   <lambda>7   s   z$ArrowNamespace.len.<locals>.<lambda>r   r1   c                 S     dgS )Nr1   r%   _dfr%   r%   r&   r8   <       depthfunction_nameevaluate_output_namesalias_output_namesr-   r*   r/   r#   r%   r#   r&   r1   4   s   
zArrowNamespace.lenvaluer   dtypeIntoDType | Nonec                   s6   dfdd j  fddd	d
dd d jdS )N_r   r   r   c                   s$   t jgdd} r| S |S )Nliteral)datar2   r3   )r   r4   cast)rF   arrow_series)rD   r$   rC   r%   r&   _lit_arrow_seriesB   s   
z-ArrowNamespace.lit.<locals>._lit_arrow_seriesc                   s
    | gS r"   r%   r6   )rK   r%   r&   r8   K   s   
 z$ArrowNamespace.lit.<locals>.<lambda>r   litc                 S  r9   )NrG   r%   r:   r%   r%   r&   r8   N   r<   r=   )rF   r   r   r   rB   )r$   rC   rD   r%   )rK   rD   r$   rC   r&   rL   A   s   
zArrowNamespace.litexprsignore_nullsboolc                  D   d fdd}j j|tdd  D d	 d
t  t  dS )Nr7   r   r   list[ArrowSeries]c                   D   t  fddD }jj}rdd |D }ttj|| gS )Nc                 3      | ]}| V  qd S r"   r%   .0er6   r%   r&   	<genexpr>U       z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>c                 s      | ]
}| d ddV  qdS )TN	fill_nullrU   sr%   r%   r&   rW   X       )r   r4   r,   _align_full_broadcastr   operatorand_r7   seriesalignrM   rN   r$   r6   r&   funcT   
   z+ArrowNamespace.all_horizontal.<locals>.funcc                 s      | ]}|j V  qd S r"   _depthrU   xr%   r%   r&   rW   ]       z0ArrowNamespace.all_horizontal.<locals>.<genexpr>   all_horizontalrf   r>   r?   r@   rA   r3   r7   r   r   rQ   r*   _from_callablemaxr   r   r$   rN   rM   rf   r%   re   r&   ro   S      zArrowNamespace.all_horizontalc                  rP   )Nr7   r   r   rQ   c                   rR   )Nc                 3  rS   r"   r%   rT   r6   r%   r&   rW   f   rX   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>c                 s  rY   )FNrZ   r\   r%   r%   r&   rW   i   r^   )r   r4   r,   r_   r   r`   or_rb   re   r6   r&   rf   e   rg   z+ArrowNamespace.any_horizontal.<locals>.funcc                 s  rh   r"   ri   rk   r%   r%   r&   rW   n   rm   z0ArrowNamespace.any_horizontal.<locals>.<genexpr>rn   any_horizontalrp   rq   rr   ru   r%   re   r&   rx   d   rv   zArrowNamespace.any_horizontalc                   B   d fdd}j j|tdd  D d	 d
t  t  dS )Nr7   r   r   rQ   c                   s@   t  fddD }dd |D }jj}ttj|| gS )Nc                 3  rS   r"   r%   rU   exprr6   r%   r&   rW   w   rX   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>c                 s       | ]}|j d dddV  qdS r   N)strategylimitrZ   r\   r%   r%   r&   rW   x       )r   r4   r,   r_   r   r`   add)r7   itrc   rd   rM   r$   r6   r&   rf   v   s   z+ArrowNamespace.sum_horizontal.<locals>.funcc                 s  rh   r"   ri   rk   r%   r%   r&   rW   ~   rm   z0ArrowNamespace.sum_horizontal.<locals>.<genexpr>rn   sum_horizontalrp   rq   rr   r$   rM   rf   r%   r   r&   r   u   s   zArrowNamespace.sum_horizontalc                   sP   j j d fdd}jj|tdd  D d	 d
t  t  dS )Nr7   r   r   rQ   c                   sf   t t fddD }jj}|dd |D  }|fdd|D  }ttj|ttj| gS )Nc                 3  rS   r"   r%   rz   r6   r%   r&   rW      rX   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>c                 s  r|   r}   rZ   r\   r%   r%   r&   rW      r   c                 3  s"    | ]}d |    V  qdS )rn   N)is_nullrI   r\   )int_64r%   r&   rW      s     )listr   r4   r,   r_   r   r`   r   )r7   expr_resultsrd   rc   non_narM   r   r$   r6   r&   rf      s   z,ArrowNamespace.mean_horizontal.<locals>.funcc                 s  rh   r"   ri   rk   r%   r%   r&   rW      rm   z1ArrowNamespace.mean_horizontal.<locals>.<genexpr>rn   mean_horizontalrp   rq   )r/   dtypesInt64r*   rs   rt   r   r   r   r%   r   r&   r      s   	zArrowNamespace.mean_horizontalc                   ry   )Nr7   r   r   rQ   c                   j   j j}tt fddD ^}}||g|R  ^}}ttjdd |D |j}t	||j
jdgS )Nc                 3  rS   r"   r%   rz   r6   r%   r&   rW      rX   z>ArrowNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>c                 S     g | ]}|j qS r%   r5   r\   r%   r%   r&   
<listcomp>       z?ArrowNamespace.min_horizontal.<locals>.func.<locals>.<listcomp>r2   r-   )r,   r_   r   r   r4   r   pcmin_element_wiser5   r   r2   r/   r7   rd   init_seriesrc   native_seriesr   r6   r&   rf          z+ArrowNamespace.min_horizontal.<locals>.funcc                 s  rh   r"   ri   rk   r%   r%   r&   rW      rm   z0ArrowNamespace.min_horizontal.<locals>.<genexpr>rn   min_horizontalrp   rq   rr   r   r%   r   r&   r         zArrowNamespace.min_horizontalc                   ry   )Nr7   r   r   rQ   c                   r   )Nc                 3  rS   r"   r%   rz   r6   r%   r&   rW      rX   z>ArrowNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>c                 S  r   r%   r   r\   r%   r%   r&   r      r   z?ArrowNamespace.max_horizontal.<locals>.func.<locals>.<listcomp>r   )r,   r_   r   r   r4   r   r   max_element_wiser5   r   r2   r/   r   r   r6   r&   rf      r   z+ArrowNamespace.max_horizontal.<locals>.funcc                 s  rh   r"   ri   rk   r%   r%   r&   rW      rm   z0ArrowNamespace.max_horizontal.<locals>.<genexpr>rn   max_horizontalrp   rq   rr   r   r%   r   r&   r      r   zArrowNamespace.max_horizontaldfsSequence[pa.Table]pa.Tablec                C  s&   | j dkrtj|ddS tj|ddS )N)   default)promote_optionsT)promote)_backend_versionpaconcat_tables)r$   r   r%   r%   r&   _concat_diagonal   s   
zArrowNamespace._concat_diagonalc                C  s@   t tdd |D }t tdd |D }tjj||dS )Nc                 s  rh   r"   )column_namesrU   r7   r%   r%   r&   rW      rm   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>c                 s  s    | ]}|  V  qd S r"   )itercolumnsr   r%   r%   r&   rW      rX   )names)r   r   r4   r   Tablefrom_arrays)r$   r   r   arraysr%   r%   r&   _concat_horizontal   s   z!ArrowNamespace._concat_horizontalc                C  s`   |d j }t|dd  ddD ]\}}|j }||kr*d| d| d| d}t|qt|S )Nr   rn   )startz>unable to vstack, column names don't match:
   - dataframe 0: z
   - dataframe z: 
)r   	enumerate	TypeErrorr   r   )r$   r   cols_0ir7   cols_currentmsgr%   r%   r&   _concat_vertical   s   

zArrowNamespace._concat_verticalr   c                 C  s
   t | S r"   )r   from_namespacer#   r%   r%   r&   	selectors   s   
zArrowNamespace.selectors	predicate	ArrowWhenc                 C  s   t j|| dS )N)r3   )r   	from_expr)r$   r   r%   r%   r&   when   s   zArrowNamespace.when	separatorstrc                  sF   d fdd}j j|tdd  D d	 d
t  t  dS )Nr7   r   r   rQ   c           	        s   j j}|t fddD  }|d j}rdnd}tdd |D di\}}tj}j |g ||R d|i|jd	}|gS )
Nc                 3  rS   r"   r%   rz   r6   r%   r&   rW      rX   z:ArrowNamespace.concat_str.<locals>.func.<locals>.<genexpr>r   skip	emit_nullc                 s  rh   r"   r   r\   r%   r%   r&   rW      rm   r   null_handlingr   )	r,   r_   r   r4   r2   r   r   binary_join_element_wiser/   )	r7   rd   compliant_series_listr2   r   r   separator_scalar
concat_str	compliantrM   rN   r$   r   r6   r&   rf      s&   


z'ArrowNamespace.concat_str.<locals>.funcc                 s  rh   r"   ri   rk   r%   r%   r&   rW      rm   z,ArrowNamespace.concat_str.<locals>.<genexpr>rn   r   rp   rq   rr   )r$   r   rN   rM   rf   r%   r   r&   r      s   zArrowNamespace.concat_strc                   ry   )Nr7   r   r   rQ   c                   sV   j j}|t fddD  ^}}ttj|jgdd |D R  |jj	dgS )Nc                 3  rS   r"   r%   rz   r6   r%   r&   rW     rX   z8ArrowNamespace.coalesce.<locals>.func.<locals>.<genexpr>c                 s  rh   r"   r   r\   r%   r%   r&   rW   
  rm   r   )
r,   r_   r   r4   r   r   coalescer5   r2   r/   )r7   rd   r   rc   r   r6   r&   rf     s    z%ArrowNamespace.coalesce.<locals>.funcc                 s  rh   r"   ri   rk   r%   r%   r&   rW     rm   z*ArrowNamespace.coalesce.<locals>.<genexpr>rn   r   rp   rq   rr   r   r%   r   r&   r     r   zArrowNamespace.coalesceN)r   r    )r   r)   )r   r+   )r-   r   r   r.   )r   r
   )rC   r   rD   rE   r   r
   )rM   r
   rN   rO   r   r
   )rM   r
   r   r
   )r   r   r   r   )r   r   )r   r
   r   r   )rM   r
   r   r   rN   rO   r   r
   )__name__
__module____qualname__r   PYARROW_implementationpropertyr'   r*   r,   r0   r1   rL   ro   rx   r   r   r   r   r   r   r   r   r   r   r   r%   r%   r%   r&   r       s2    













"r   r   c                   @  s$   e Zd ZedddZdd
dZdS )r   r   type[ArrowThen]c                 C  r!   r"   )	ArrowThenr#   r%   r%   r&   _then  r(   zArrowWhen._thenr   r   then	otherwise ArrayOrScalar | NonNestedLiteralc                C  s,   |d u rt t||jn|}t|||S r"   )r   nullsr1   typer   if_else)r$   r   r   r   r%   r%   r&   _if_then_else  s   zArrowWhen._if_then_elseN)r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r%   r%   r%   r&   r     s    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   intrj   r   _scalar_kwargswhenthenr   _function_nameN)r   r   r   rj   __annotations__r   r   r%   r%   r%   r&   r   *  s   
 r   )3
__future__r   r`   	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   narwhals._compliant.typingr   r   narwhals.typingr   r   r   r   r   r   r%   r%   r%   r&   <module>   s:    
 {
