o
    h%2                     @  s  U d 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 ddlmZ ddlmZ esddlZejdkrdd
lmZ nddlmZ n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$ 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m0Z0 dd&lm1Z1 dd'lm2Z2 g d(Z3ed)d*d+Z4ed,d-d+Z5ed.d/d+Z6ed0d1d+Z7ed2d3d+Z8ed4d5d+Z9d6Z:d7e;d8< G d9d: d:ee7e4f Z<G d;d< d<e<e8e5f ee8e5f Z=G d=d> d>e<e9e6f ee9e6f Z>G d?d@ d@ee7e4f ee7e4f Z?dIdGdHZ@dS )Jz=Almost entirely complete, generic `selectors` implementation.    )annotationsN)partial)TYPE_CHECKING)Any)
Collection)Iterable)Iterator)Protocol)Sequence)TypeVar)overload)CompliantExpr)_parse_time_unit_and_time_zone)%dtype_matches_time_unit_and_time_zone)get_column_names)import_dtypes_module)is_compliant_dataframe)   	   )Generic)timezone)Self)	TypeAlias)TypeIs)
NativeExpr)CompliantDataFrameAny)CompliantExprAny)CompliantFrameAny)CompliantLazyFrameAny)CompliantSeriesAny)CompliantSeriesOrNativeExprAny)	EvalNames)
EvalSeries)DType)TimeUnit)Implementation)Version)_FullContext)CompliantSelectorCompliantSelectorNamespaceEagerSelectorNamespaceLazySelectorNamespaceSeriesOrExprTr    )boundSeriesTr   ExprTr   FrameTr   
DataFrameTr   
LazyFrameTr   zOCompliantSelector[FrameT, SeriesOrExprT] | CompliantExpr[FrameT, SeriesOrExprT]r   SelectorOrExprc                   @  s   e Zd ZU ded< ded< ded< ed>ddZed?ddZd@ddZdAddZ	dBddZ
dCddZdDd#d$ZdEd'd(ZdFd+d,ZdGd-d.ZdGd/d0ZdGd1d2ZdGd3d4ZdGd5d6ZdHd;d<Zd=S )Ir)   r%   _implementationtuple[int, ...]_backend_versionr&   _versioncontextr'   returnr   c                C  s&   |  | }|j|_|j|_|j|_|S N)__new__r4   r6   r7   )clsr8   obj r>   Q/var/www/vscode/kcb/lib/python3.10/site-packages/narwhals/_compliant/selectors.pyfrom_namespaceS   s
   
z)CompliantSelectorNamespace.from_namespace.type[CompliantSelector[FrameT, SeriesOrExprT]]c                 C     d S r:   r>   selfr>   r>   r?   	_selector[      z$CompliantSelectorNamespace._selectordfr0   Iterator[SeriesOrExprT]c                C  rB   r:   r>   rD   rG   r>   r>   r?   _iter_columns^       z(CompliantSelectorNamespace._iter_columnsIterator[tuple[str, DType]]c                C  rB   r:   r>   rI   r>   r>   r?   _iter_schema`   rK   z'CompliantSelectorNamespace._iter_schema%Iterator[tuple[SeriesOrExprT, DType]]c                C  rB   r:   r>   rI   r>   r>   r?   _iter_columns_dtypesb   s   z/CompliantSelectorNamespace._iter_columns_dtypes#Iterator[tuple[SeriesOrExprT, str]]c                c  s    t | ||jE d H  d S r:   )ziprJ   columnsrI   r>   r>   r?   _iter_columns_namesf   s   z.CompliantSelectorNamespace._iter_columns_namesrD   1CompliantSelectorNamespace[FrameT, SeriesOrExprT]dtypetype[DType](CompliantSelector[FrameT, SeriesOrExprT]c                  2   d fdd}d fdd	}j j||d
S )NrG   r0   r9   Sequence[SeriesOrExprT]c                       fdd | D S )Nc                      g | ]\}}t | r|qS r>   
isinstance.0sertprU   r>   r?   
<listcomp>m   s
    zHCompliantSelectorNamespace._is_dtype.<locals>.series.<locals>.<listcomp>rO   rG   rU   rD   r>   r?   seriesl   s   
z4CompliantSelectorNamespace._is_dtype.<locals>.seriesSequence[str]c                   rZ   )Nc                   r[   r>   r\   r_   namera   rb   r>   r?   rc   r       zGCompliantSelectorNamespace._is_dtype.<locals>.names.<locals>.<listcomp>rM   re   rf   r>   r?   namesq      z3CompliantSelectorNamespace._is_dtype.<locals>.namesr8   rG   r0   r9   rY   rG   r0   r9   rh   rE   from_callables)rD   rU   rg   rm   r>   rf   r?   	_is_dtypei   s   z$CompliantSelectorNamespace._is_dtypedtypesCollection[DType | type[DType]]c                   rX   )NrG   r0   r9   rY   c                   rZ   )Nc                      g | ]
\}}| v r|qS r>   r>   r^   ru   r>   r?   rc   z       zGCompliantSelectorNamespace.by_dtype.<locals>.series.<locals>.<listcomp>rd   re   ru   rD   r>   r?   rg   y   rn   z3CompliantSelectorNamespace.by_dtype.<locals>.seriesrh   c                   rZ   )Nc                   rw   r>   r>   ri   rx   r>   r?   rc   }   ry   zFCompliantSelectorNamespace.by_dtype.<locals>.names.<locals>.<listcomp>rl   re   rz   r>   r?   rm   |   rn   z2CompliantSelectorNamespace.by_dtype.<locals>.namesro   rp   rq   rr   )rD   ru   rg   rm   r>   rz   r?   by_dtypev   s   z#CompliantSelectorNamespace.by_dtypepatternstrc                   s:   t | d fdd}d fdd	}jj||d
S )NrG   r0   r9   rY   c                   s@   t  rj s fdd jD S fdd D S )Nc                   s    g | ]} |r |qS r>   )search
get_columnr_   col)rG   pr>   r?   rc      s     zFCompliantSelectorNamespace.matches.<locals>.series.<locals>.<listcomp>c                   s   g | ]\}}  |r|qS r>   r~   )r_   r`   rj   r   r>   r?   rc      rk   )r   r4   	is_duckdbrR   rS   re   r   rD   re   r?   rg      s   z2CompliantSelectorNamespace.matches.<locals>.seriesrh   c                   s    fdd| j D S )Nc                   s   g | ]	}  |r|qS r>   r   r   r   r>   r?   rc      s    zECompliantSelectorNamespace.matches.<locals>.names.<locals>.<listcomp>)rR   re   r   r>   r?   rm         z1CompliantSelectorNamespace.matches.<locals>.namesro   rp   rq   )recompilerE   rs   )rD   r|   rg   rm   r>   r   r?   matches   s   
z"CompliantSelectorNamespace.matchesc                   s.   d fdd}d fdd	} j j|| d
S )NrG   r0   r9   rY   c                      dd   | D S )Nc                 S     g | ]
\}}|  r|qS r>   
is_numericr^   r>   r>   r?   rc      ry   zFCompliantSelectorNamespace.numeric.<locals>.series.<locals>.<listcomp>rd   re   rC   r>   r?   rg      r   z2CompliantSelectorNamespace.numeric.<locals>.seriesrh   c                   r   )Nc                 S  r   r>   r   ri   r>   r>   r?   rc      ry   zECompliantSelectorNamespace.numeric.<locals>.names.<locals>.<listcomp>rl   re   rC   r>   r?   rm      r   z1CompliantSelectorNamespace.numeric.<locals>.namesro   rp   rq   rr   )rD   rg   rm   r>   rC   r?   numeric   s   z"CompliantSelectorNamespace.numericc                 C     |  t| jjS r:   )rt   r   r7   CategoricalrC   r>   r>   r?   categorical      z&CompliantSelectorNamespace.categoricalc                 C  r   r:   )rt   r   r7   StringrC   r>   r>   r?   string   r   z!CompliantSelectorNamespace.stringc                 C  r   r:   )rt   r   r7   BooleanrC   r>   r>   r?   boolean   r   z"CompliantSelectorNamespace.booleanc                   s    d fdd} j j|t dS )	NrG   r0   r9   rY   c                   s   t  | S r:   )listrJ   re   rC   r>   r?   rg         z.CompliantSelectorNamespace.all.<locals>.seriesro   rp   )rE   rs   r   )rD   rg   r>   rC   r?   all   s   zCompliantSelectorNamespace.all	time_unit$TimeUnit | Iterable[TimeUnit] | None	time_zone7str | timezone | Iterable[str | timezone | None] | Nonec                   sX   t ||\}}tttjd||d d fdd}d fd
d}jj||dS )N)version)ru   
time_units
time_zonesrG   r0   r9   rY   c                   rZ   )Nc                      g | ]
\}} |r|qS r>   r>   r^   r   r>   r?   rc      ry   zGCompliantSelectorNamespace.datetime.<locals>.series.<locals>.<listcomp>rd   re   r   rD   r>   r?   rg      rn   z3CompliantSelectorNamespace.datetime.<locals>.seriesrh   c                   rZ   )Nc                   r   r>   r>   ri   r   r>   r?   rc      ry   zFCompliantSelectorNamespace.datetime.<locals>.names.<locals>.<listcomp>rl   re   r   r>   r?   rm      rn   z2CompliantSelectorNamespace.datetime.<locals>.namesro   rp   rq   )r   r   r   r   r7   rE   rs   )rD   r   r   r   r   rg   rm   r>   r   r?   datetime   s   
z#CompliantSelectorNamespace.datetimeN)r8   r'   r9   r   )r9   rA   )rG   r0   r9   rH   )rG   r0   r9   rL   )rG   r0   r9   rN   )rG   r0   r9   rP   )rD   rT   rU   rV   r9   rW   )ru   rv   r9   rW   )r|   r}   r9   rW   r9   rW   )r   r   r   r   r9   rW   )__name__
__module____qualname____annotations__classmethodr@   propertyrE   rJ   rM   rO   rS   rt   r{   r   r   r   r   r   r   r   r>   r>   r>   r?   r)   N   s*   
 








	


r)   c                   @  *   e Zd ZdddZddd	ZdddZdS )r*   rG   r1   r9   rL   c                c  s$    |  |D ]	}|j|jfV  qd S r:   )rJ   rj   rU   rD   rG   r`   r>   r>   r?   rM      s   z#EagerSelectorNamespace._iter_schemaIterator[SeriesT]c                c      |  E d H  d S r:   )iter_columnsrI   r>   r>   r?   rJ         z$EagerSelectorNamespace._iter_columnsIterator[tuple[SeriesT, DType]]c                c  s"    |  |D ]}||jfV  qd S r:   )rJ   rU   r   r>   r>   r?   rO      s   z+EagerSelectorNamespace._iter_columns_dtypesN)rG   r1   r9   rL   )rG   r1   r9   r   )rG   r1   r9   r   r   r   r   rM   rJ   rO   r>   r>   r>   r?   r*      s    

r*   c                   @  r   )r+   rG   r2   r9   rL   c                 c  s    |j  E d H  d S r:   )schemaitemsrI   r>   r>   r?   rM      s   z"LazySelectorNamespace._iter_schemaIterator[ExprT]c                 c  r   r:   )rJ   rI   r>   r>   r?   rJ      r   z#LazySelectorNamespace._iter_columnsIterator[tuple[ExprT, DType]]c                c  s"    t | ||j E d H  d S r:   )rQ   rJ   r   valuesrI   r>   r>   r?   rO      s    z*LazySelectorNamespace._iter_columns_dtypesN)rG   r2   r9   rL   )rG   r2   r9   r   )rG   r2   r9   r   r   r>   r>   r>   r?   r+      s    

r+   c                   @  s   e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< ed4ddZed5ddZd6ddZd7d!d"Z	e
d8d#d$Ze
d9d%d$Zd:d'd$Ze
d8d(d)Ze
d9d*d)Zd:d+d)Ze
d8d,d-Ze
d9d.d-Zd:d/d-Zd;d1d2Zd3S )<r(   !EvalSeries[FrameT, SeriesOrExprT]_callr}   _function_nameint_depthr%   r4   r5   r6   r&   r7   zdict[str, Any]_call_kwargscallevaluate_output_namesEvalNames[FrameT]r8   r'   r9   r   c                C  sJ   |  | }||_d|_d|_||_d |_|j|_|j|_|j|_i |_	|S )Nr   selector)
r;   r   r   r   _evaluate_output_names_alias_output_namesr4   r6   r7   r   )r<   r   r   r8   r=   r>   r>   r?   rs      s   
z CompliantSelector.from_callablesrT   c                 C  s
   |   jS r:   )__narwhals_namespace__	selectorsrC   r>   r>   r?   r      s   
zCompliantSelector.selectors$CompliantExpr[FrameT, SeriesOrExprT]c                 C  rB   r:   r>   rC   r>   r>   r?   _to_expr   rK   zCompliantSelector._to_exprother+Self | CompliantExpr[FrameT, SeriesOrExprT]0TypeIs[CompliantSelector[FrameT, SeriesOrExprT]]c                 C  s   t |t| S r:   )r]   typerD   r   r>   r>   r?   _is_selector   s   zCompliantSelector._is_selectorc                 C  rB   r:   r>   r   r>   r>   r?   __sub__  rF   zCompliantSelector.__sub__c                 C  rB   r:   r>   r   r>   r>   r?   r        %SelectorOrExpr[FrameT, SeriesOrExprT]c                   sJ     rd fdd}d fdd	}jjj||d
S    S )NrG   r0   r9   rY   c                   ,   t | \}  fddt| |D S )Nc                   s   g | ]
\}}| vr|qS r>   r>   r_   xrj   	rhs_namesr>   r?   rc     s    z=CompliantSelector.__sub__.<locals>.series.<locals>.<listcomp>_eval_lhs_rhsrQ   rG   	lhs_namesr   rD   r   r?   rg     s   
z)CompliantSelector.__sub__.<locals>.seriesrh   c                   "   t | \}  fdd|D S )Nc                   s   g | ]}| vr|qS r>   r>   r_   r   r   r>   r?   rc         z<CompliantSelector.__sub__.<locals>.names.<locals>.<listcomp>r   r   r   r   r?   rm        z(CompliantSelector.__sub__.<locals>.namesro   rp   rq   r   r   rE   rs   r   rD   r   rg   rm   r>   r   r?   r   	  s
   
c                 C  rB   r:   r>   r   r>   r>   r?   __or__  rF   zCompliantSelector.__or__c                 C  rB   r:   r>   r   r>   r>   r?   r     r   c                   sJ     rd fdd}d fdd	}jjj||d
S   B S )NrG   r0   r9   rY   c                   s8   t | \} g  fddt| |D | S )Nc                 3  s     | ]\}}| vr|V  qd S r:   r>   r   r   r>   r?   	<genexpr>)  s    z;CompliantSelector.__or__.<locals>.series.<locals>.<genexpr>r   r   r   r   r?   rg   &  s   z(CompliantSelector.__or__.<locals>.seriesrh   c                   s*   t | \} g  fdd|D  S )Nc                 3  s    | ]	}| vr|V  qd S r:   r>   r   r   r>   r?   r   /  s    z:CompliantSelector.__or__.<locals>.names.<locals>.<genexpr>r   r   r   r   r?   rm   -  s   z'CompliantSelector.__or__.<locals>.namesro   rp   rq   r   r   r>   r   r?   r   !  s
   
c                 C  rB   r:   r>   r   r>   r>   r?   __and__4  rF   zCompliantSelector.__and__c                 C  rB   r:   r>   r   r>   r>   r?   r   6  r   c                   sJ     rd fdd}d fdd	}jjj||d
S   @ S )NrG   r0   r9   rY   c                   r   )Nc                   rw   r>   r>   r   r   r>   r?   rc   A  ry   z=CompliantSelector.__and__.<locals>.series.<locals>.<listcomp>r   r   r   r   r?   rg   ?  s   z)CompliantSelector.__and__.<locals>.seriesrh   c                   r   )Nc                   s   g | ]}| v r|qS r>   r>   r   r   r>   r?   rc   E  r   z<CompliantSelector.__and__.<locals>.names.<locals>.<listcomp>r   r   r   r   r?   rm   C  r   z(CompliantSelector.__and__.<locals>.namesro   rp   rq   r   r   r>   r   r?   r   :  s
   
rW   c                 C  s   | j  |  S r:   )r   r   rC   r>   r>   r?   
__invert__J  r   zCompliantSelector.__invert__N)r   r   r   r   r8   r'   r9   r   )r9   rT   )r9   r   )r   r   r9   r   )r   r   r9   r   )r   r   r9   r   )r   r   r9   r   r   )r   r   r   r   r   rs   r   r   r   r   r   r   r   r   r   r>   r>   r>   r?   r(      s<   
 




r(   rG   lhsr   rhsr9   #tuple[Sequence[str], Sequence[str]]c                 C  s   | | | | fS r:   )r   )rG   r   r   r>   r>   r?   r   N  s   r   )rG   r   r   r   r   r   r9   r   )A__doc__
__future__r   r   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   narwhals._compliant.exprr   narwhals.utilsr   r   r   r   r   sysversion_info
Protocol38r   r   r   typing_extensionsr   r   r   r   narwhals._compliant.typingr   r   r   r   r   r    r!   r"   narwhals.dtypesr#   narwhals.typingr$   r%   r&   r'   __all__r,   r.   r/   r0   r1   r2   r3   r   r)   r*   r+   r(   r   r>   r>   r>   r?   <module>   s    


o

u