o
    h?-                  	   @  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 d dlm	Z	 d dl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 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$ erd 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- G dd deeeeddf Z.G dd  d eeeedf Z/G d!d" d"eeeef eZ0dS )#    )annotationsN)reduce)chain)TYPE_CHECKING)Literal)SequenceArrowDataFrame	ArrowExpr)ArrowSelectorNamespaceArrowSeries)align_series_full_broadcast)cast_to_comparable_string_types)CompliantThen)EagerNamespace)	EagerWhen)combine_alias_output_names)combine_evaluate_output_names)Implementation)import_dtypes_module)ArrayOrScalarAny)ArrowChunkedArray)
Incomplete)DType)NonNestedLiteral)Versionc                   @  s   e Zd 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dEd d!ZdEd"d#ZdEd$d%ZdEd&d'ZdFd+d,ZdFd-d.ZdFd/d0ZedGd2d3ZdHd6d7ZdId<d=Zd>S )JArrowNamespacereturntype[ArrowDataFrame]c                 C     t S Nr   self r%   M/var/www/vscode/kcb/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&   _series1   r(   zArrowNamespace._seriesbackend_versiontuple[int, ...]versionr   Nonec                C  s   || _ tj| _|| _d S r"   )_backend_versionr   PYARROW_implementation_version)r$   r-   r/   r%   r%   r&   __init__6   s   
zArrowNamespace.__init__r   c              	     s(    j  fdddddd d  j jdS )Nc                   s   t jt| jgd dgS )Nlen)namecontext)r   from_iterabler6   nativedfr#   r%   r&   <lambda>>   s   z$ArrowNamespace.len.<locals>.<lambda>r   r6   c                 S     dgS )Nr6   r%   _dfr%   r%   r&   r=   C       depthfunction_nameevaluate_output_namesalias_output_namesr-   r/   r*   r1   r4   r#   r%   r#   r&   r6   ;   s   
zArrowNamespace.lenvaluer   dtypeDType | type[DType] | Nonec              	     s:   dfdd j  fddd	d
dd d jjdS )N_r	   r   r   c                   s$   t jgdd} r| S |S )Nliteral)datar7   r8   )r   r9   cast)rK   arrow_series)rI   r$   rH   r%   r&   _lit_arrow_seriesL   s   
z-ArrowNamespace.lit.<locals>._lit_arrow_seriesc                   s
    | gS r"   r%   r;   )rP   r%   r&   r=   U   s   
 z$ArrowNamespace.lit.<locals>.<lambda>r   litc                 S  r>   )NrL   r%   r?   r%   r%   r&   r=   X   rA   rB   )rK   r	   r   r   rG   )r$   rH   rI   r%   )rP   rI   r$   rH   r&   rQ   I   s   
zArrowNamespace.litexprsc                   @   d fdd}| j j|tdd  D d	 d
t  t  | dS )Nr<   r	   r   list[ArrowSeries]c                   *   t  fddD }ttjt| gS )Nc                 3      | ]}| V  qd S r"   r%   .0exprr;   r%   r&   	<genexpr>`       z>ArrowNamespace.all_horizontal.<locals>.func.<locals>.<genexpr>)r   r9   r   operatorand_r   r<   seriesrR   r;   r&   func_      z+ArrowNamespace.all_horizontal.<locals>.funcc                 s      | ]}|j V  qd S r"   _depthrX   xr%   r%   r&   rZ   e       z0ArrowNamespace.all_horizontal.<locals>.<genexpr>   all_horizontalra   rC   rD   rE   rF   r8   r<   r	   r   rT   r*   _from_callablemaxr   r   r$   rR   ra   r%   r`   r&   rj   ^      zArrowNamespace.all_horizontalc                   rS   )Nr<   r	   r   rT   c                   rU   )Nc                 3  rV   r"   r%   rW   r;   r%   r&   rZ   n   r[   z>ArrowNamespace.any_horizontal.<locals>.func.<locals>.<genexpr>)r   r9   r   r\   or_r   r^   r`   r;   r&   ra   m   rb   z+ArrowNamespace.any_horizontal.<locals>.funcc                 s  rc   r"   rd   rf   r%   r%   r&   rZ   s   rh   z0ArrowNamespace.any_horizontal.<locals>.<genexpr>ri   any_horizontalrk   rl   rm   rp   r%   r`   r&   rs   l   rq   zArrowNamespace.any_horizontalc                   rS   )Nr<   r	   r   rT   c                   s8   t  fddD }dd |D }ttjt| gS )Nc                 3  rV   r"   r%   rW   r;   r%   r&   rZ   |   r[   z>ArrowNamespace.sum_horizontal.<locals>.func.<locals>.<genexpr>c                 s       | ]}|j d dddV  qdS r   N)strategylimit	fill_nullrX   sr%   r%   r&   rZ   }       )r   r9   r   r\   addr   )r<   itr_   r`   r;   r&   ra   {   s   z+ArrowNamespace.sum_horizontal.<locals>.funcc                 s  rc   r"   rd   rf   r%   r%   r&   rZ      rh   z0ArrowNamespace.sum_horizontal.<locals>.<genexpr>ri   sum_horizontalrk   rl   rm   rp   r%   r`   r&   r   z   s   zArrowNamespace.sum_horizontalc                   sL   t | j d fdd}| jj|tdd D d	 d
t t | dS )Nr<   r	   r   rT   c                   s^   t t fddD }tdd |D  }tfdd|D  }ttj|ttj| gS )Nc                 3  rV   r"   r%   rW   r;   r%   r&   rZ      r[   z?ArrowNamespace.mean_horizontal.<locals>.func.<locals>.<genexpr>c                 s  rt   ru   rx   rz   r%   r%   r&   rZ      r|   c                 3  s&    | ]}d |     V  qdS )ri   N)is_nullrN   Int64rz   )dtypesr%   r&   rZ      s   $ )listr   r9   r   r   r\   r}   )r<   expr_resultsr_   non_nar   rR   r;   r&   ra      s   z,ArrowNamespace.mean_horizontal.<locals>.funcc                 s  rc   r"   rd   rf   r%   r%   r&   rZ      rh   z1ArrowNamespace.mean_horizontal.<locals>.<genexpr>ri   mean_horizontalrk   rl   )r   r4   r*   rn   ro   r   r   rp   r%   r   r&   r      s   

zArrowNamespace.mean_horizontalc                   B   d fdd}j j|tdd  D d	 d
t  t  dS )Nr<   r	   r   rT   c                   f   t t fddD ^}}t|g|R  ^}}ttjdd |D |j}t||j	j
jdgS )Nc                 3  rV   r"   r%   rW   r;   r%   r&   rZ      r[   z>ArrowNamespace.min_horizontal.<locals>.func.<locals>.<genexpr>c                 S     g | ]}|j qS r%   r:   rz   r%   r%   r&   
<listcomp>       z?ArrowNamespace.min_horizontal.<locals>.func.<locals>.<listcomp>r7   r-   r/   )r   r   r9   r   r   pcmin_element_wiser:   r   r7   r1   r4   r<   init_seriesr_   native_seriesrR   r$   r;   r&   ra          z+ArrowNamespace.min_horizontal.<locals>.funcc                 s  rc   r"   rd   rf   r%   r%   r&   rZ      rh   z0ArrowNamespace.min_horizontal.<locals>.<genexpr>ri   min_horizontalrk   rl   rm   rp   r%   r   r&   r         zArrowNamespace.min_horizontalc                   r   )Nr<   r	   r   rT   c                   r   )Nc                 3  rV   r"   r%   rW   r;   r%   r&   rZ      r[   z>ArrowNamespace.max_horizontal.<locals>.func.<locals>.<genexpr>c                 S  r   r%   r   rz   r%   r%   r&   r      r   z?ArrowNamespace.max_horizontal.<locals>.func.<locals>.<listcomp>r   )r   r   r9   r   r   r   max_element_wiser:   r   r7   r1   r4   r   r   r;   r&   ra      r   z+ArrowNamespace.max_horizontal.<locals>.funcc                 s  rc   r"   rd   rf   r%   r%   r&   rZ      rh   z0ArrowNamespace.max_horizontal.<locals>.<genexpr>ri   max_horizontalrk   rl   rm   rp   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)r1   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  rc   r"   )column_namesrX   r<   r%   r%   r&   rZ      rh   z4ArrowNamespace._concat_horizontal.<locals>.<genexpr>c                 s  s    | ]}|  V  qd S r"   )itercolumnsr   r%   r%   r&   rZ      r[   )names)r   r   r9   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   ri   )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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)r8   )r   	from_expr)r$   r   r%   r%   r&   when   s   zArrowNamespace.when	separatorstrignore_nullsboolc                  sF   d fdd}j j|tdd  D d	 d
t  t  dS )Nr<   r	   r   rT   c                   s   t 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j	d	}|gS )
Nc                 3  rV   r"   r%   rW   r;   r%   r&   rZ      r[   z:ArrowNamespace.concat_str.<locals>.func.<locals>.<genexpr>r   skip	emit_nullc                 s  rc   r"   r   rz   r%   r%   r&   rZ      rh   r   null_handlingr   )
r   r   r9   r7   r   r   binary_join_element_wiser,   r1   r4   )r<   compliant_series_listr7   r   r~   separator_scalar
concat_str	compliantrR   r   r$   r   r;   r&   ra      s&   


z'ArrowNamespace.concat_str.<locals>.funcc                 s  rc   r"   rd   rf   r%   r%   r&   rZ     rh   z,ArrowNamespace.concat_str.<locals>.<genexpr>ri   r   rk   rl   rm   )r$   r   r   rR   ra   r%   r   r&   r      s   zArrowNamespace.concat_strN)r   r    )r   r)   )r   r+   )r-   r.   r/   r   r   r0   )r   r   )rH   r   rI   rJ   r   r   )rR   r   r   r   )r   r   r   r   )r   r   )r   r   r   r   )rR   r   r   r   r   r   r   r   )__name__
__module____qualname__propertyr'   r*   r,   r5   r6   rQ   rj   rs   r   r   r   r   r   r   r   r   r   r   r%   r%   r%   r&   r   $   s.    












r   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#ArrayOrScalarAny | NonNestedLiteralc                C  s,   |d u rt t||jn|}t|||S r"   )r   nullsr6   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                   @  s   e Zd ZdS )r   N)r   r   r   r%   r%   r%   r&   r   &  s    r   )1
__future__r   r\   	functoolsr   	itertoolsr   typingr   r   r   pyarrowr   pyarrow.computecomputer   narwhals._arrow.dataframer	   narwhals._arrow.exprr   narwhals._arrow.selectorsr   narwhals._arrow.seriesr   narwhals._arrow.utilsr   r   narwhals._compliantr   r   r   narwhals._expression_parsingr   r   narwhals.utilsr   r   narwhals._arrow.typingr   r   r   narwhals.dtypesr   narwhals.typingr   r   r   r   r   r%   r%   r%   r&   <module>   sJ    
 s 