o
    Lh.                     @  s  U d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZmZmZmZmZ erddlmZmZmZmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' ddlm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z. g dZ/e	dddZ0e	dddZ1e	dddZ2e	dddZ3e	dddZ4e	dddZ5dZ6de7d < G d!d" d"ee3e0f Z8G d#d$ d$e8e4e1f ee4e1f Z9G d%d& d&e8e5e2f ee5e2f Z:G d'd( d(ee3e0f ee3e0f Z;d1d/d0Z<dS )2z=Almost entirely complete, generic `selectors` implementation.    )annotationsN)partial)TYPE_CHECKINGProtocolTypeVaroverload)CompliantExpr)_parse_time_unit_and_time_zone%dtype_matches_time_unit_and_time_zoneget_column_namesis_compliant_dataframe
zip_strict)
CollectionIterableIteratorSequence)timezone)Self	TypeAliasTypeIs)
NativeExpr)CompliantDataFrameAnyCompliantExprAnyCompliantFrameAnyCompliantLazyFrameAnyCompliantSeriesAnyCompliantSeriesOrNativeExprAny	EvalNames
EvalSeries)ImplementationVersion_LimitedContext)DType)TimeUnit)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< ed<d	d
Zed=ddZd>ddZd?ddZ	d@ddZ
dAddZdBd!d"ZdCd%d&ZdDd)d*ZdEd+d,ZdEd-d.ZdEd/d0ZdEd1d2ZdEd3d4ZdFd9d:Zd;S )Gr%   r   _implementationr    _versioncontextr!   returnr   c                C  s   |  | }|j|_|j|_|S N)__new__r0   r1   )clsr2   obj r8   ^/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/narwhals/_compliant/selectors.pyfrom_namespace>   s   
z)CompliantSelectorNamespace.from_namespace.type[CompliantSelector[FrameT, SeriesOrExprT]]c                 C     d S r4   r8   selfr8   r8   r9   	_selectorE      z$CompliantSelectorNamespace._selectordfr,   Iterator[SeriesOrExprT]c                C  r<   r4   r8   r>   rA   r8   r8   r9   _iter_columnsH       z(CompliantSelectorNamespace._iter_columnsIterator[tuple[str, DType]]c                C  r<   r4   r8   rC   r8   r8   r9   _iter_schemaJ   rE   z'CompliantSelectorNamespace._iter_schema%Iterator[tuple[SeriesOrExprT, DType]]c                C  r<   r4   r8   rC   r8   r8   r9   _iter_columns_dtypesL   s   z/CompliantSelectorNamespace._iter_columns_dtypes#Iterator[tuple[SeriesOrExprT, str]]c                c  s    t | ||jE d H  d S r4   )r   rD   columnsrC   r8   r8   r9   _iter_columns_namesP   s   z.CompliantSelectorNamespace._iter_columns_namesr>   1CompliantSelectorNamespace[FrameT, SeriesOrExprT]dtypetype[DType](CompliantSelector[FrameT, SeriesOrExprT]c                  2   d fdd}d fdd	}j j||d
S )NrA   r,   r3   Sequence[SeriesOrExprT]c                       fdd | D S )Nc                      g | ]\}}t | r|qS r8   
isinstance.0sertprN   r8   r9   
<listcomp>W   s
    zHCompliantSelectorNamespace._is_dtype.<locals>.series.<locals>.<listcomp>rI   rA   rN   r>   r8   r9   seriesV   s   
z4CompliantSelectorNamespace._is_dtype.<locals>.seriesSequence[str]c                   rS   )Nc                   rT   r8   rU   rX   namerZ   r[   r8   r9   r\   \       zGCompliantSelectorNamespace._is_dtype.<locals>.names.<locals>.<listcomp>rG   r^   r_   r8   r9   names[      z3CompliantSelectorNamespace._is_dtype.<locals>.namesr2   rA   r,   r3   rR   rA   r,   r3   ra   r?   from_callables)r>   rN   r`   rf   r8   r_   r9   	_is_dtypeS   s   z$CompliantSelectorNamespace._is_dtypedtypesCollection[DType | type[DType]]c                   rQ   )NrA   r,   r3   rR   c                   rS   )Nc                      g | ]
\}}| v r|qS r8   r8   rW   rn   r8   r9   r\   d       zGCompliantSelectorNamespace.by_dtype.<locals>.series.<locals>.<listcomp>r]   r^   rn   r>   r8   r9   r`   c   rg   z3CompliantSelectorNamespace.by_dtype.<locals>.seriesra   c                   rS   )Nc                   rp   r8   r8   rb   rq   r8   r9   r\   g   rr   zFCompliantSelectorNamespace.by_dtype.<locals>.names.<locals>.<listcomp>re   r^   rs   r8   r9   rf   f   rg   z2CompliantSelectorNamespace.by_dtype.<locals>.namesrh   ri   rj   rk   )r>   rn   r`   rf   r8   rs   r9   by_dtype`   s   z#CompliantSelectorNamespace.by_dtypepatternstrc                   s:   t | d fdd}d fdd	}jj||d
S )NrA   r,   r3   rR   c                   sJ   t  rj sj s fdd jD S fdd D S )Nc                   s    g | ]} |r |qS r8   )search
get_columnrX   col)rA   pr8   r9   r\   t   s     zFCompliantSelectorNamespace.matches.<locals>.series.<locals>.<listcomp>c                   s   g | ]\}}  |r|qS r8   rw   )rX   rY   rc   r{   r8   r9   r\   v   rd   )r   r0   	is_duckdbis_ibisrK   rL   r^   r{   r>   r^   r9   r`   n   s   z2CompliantSelectorNamespace.matches.<locals>.seriesra   c                   s    fdd| j D S )Nc                   s   g | ]	}  |r|qS r8   r|   ry   r}   r8   r9   r\   y   s    zECompliantSelectorNamespace.matches.<locals>.names.<locals>.<listcomp>)rK   r^   r}   r8   r9   rf   x      z1CompliantSelectorNamespace.matches.<locals>.namesrh   ri   rj   )recompiler?   rl   )r>   ru   r`   rf   r8   r   r9   matchesk   s   

z"CompliantSelectorNamespace.matchesc                   s.   d fdd}d fdd	} j j|| d
S )NrA   r,   r3   rR   c                      dd   | D S )Nc                 S     g | ]
\}}|  r|qS r8   
is_numericrW   r8   r8   r9   r\      rr   zFCompliantSelectorNamespace.numeric.<locals>.series.<locals>.<listcomp>r]   r^   r=   r8   r9   r`   ~   r   z2CompliantSelectorNamespace.numeric.<locals>.seriesra   c                   r   )Nc                 S  r   r8   r   rb   r8   r8   r9   r\      rr   zECompliantSelectorNamespace.numeric.<locals>.names.<locals>.<listcomp>re   r^   r=   r8   r9   rf      r   z1CompliantSelectorNamespace.numeric.<locals>.namesrh   ri   rj   rk   )r>   r`   rf   r8   r=   r9   numeric}   s   z"CompliantSelectorNamespace.numericc                 C     |  | jjjS r4   )rm   r1   rn   Categoricalr=   r8   r8   r9   categorical      z&CompliantSelectorNamespace.categoricalc                 C  r   r4   )rm   r1   rn   Stringr=   r8   r8   r9   string   r   z!CompliantSelectorNamespace.stringc                 C  r   r4   )rm   r1   rn   Booleanr=   r8   r8   r9   boolean   r   z"CompliantSelectorNamespace.booleanc                   s    d fdd} j j|t dS )	NrA   r,   r3   rR   c                   s   t  | S r4   )listrD   r^   r=   r8   r9   r`         z.CompliantSelectorNamespace.all.<locals>.seriesrh   ri   )r?   rl   r   )r>   r`   r8   r=   r9   all   s   zCompliantSelectorNamespace.all	time_unit$TimeUnit | Iterable[TimeUnit] | None	time_zone7str | timezone | Iterable[str | timezone | None] | Nonec                   sT   t ||\}}ttjj||d d fdd}d fd	d
}jj||dS )N)rn   
time_units
time_zonesrA   r,   r3   rR   c                   rS   )Nc                      g | ]
\}} |r|qS r8   r8   rW   r   r8   r9   r\      rr   zGCompliantSelectorNamespace.datetime.<locals>.series.<locals>.<listcomp>r]   r^   r   r>   r8   r9   r`      rg   z3CompliantSelectorNamespace.datetime.<locals>.seriesra   c                   rS   )Nc                   r   r8   r8   rb   r   r8   r9   r\      rr   zFCompliantSelectorNamespace.datetime.<locals>.names.<locals>.<listcomp>re   r^   r   r8   r9   rf      rg   z2CompliantSelectorNamespace.datetime.<locals>.namesrh   ri   rj   )r	   r   r
   r1   rn   r?   rl   )r>   r   r   r   r   r`   rf   r8   r   r9   datetime   s   z#CompliantSelectorNamespace.datetimeN)r2   r!   r3   r   )r3   r;   )rA   r,   r3   rB   )rA   r,   r3   rF   )rA   r,   r3   rH   )rA   r,   r3   rJ   )r>   rM   rN   rO   r3   rP   )rn   ro   r3   rP   )ru   rv   r3   rP   r3   rP   )r   r   r   r   r3   rP   )__name__
__module____qualname____annotations__classmethodr:   propertyr?   rD   rG   rI   rL   rm   rt   r   r   r   r   r   r   r   r8   r8   r8   r9   r%   :   s(   
 








	


r%   c                   @  *   e Zd ZdddZddd	ZdddZdS )r&   rA   r-   r3   rF   c                c  s$    |  |D ]	}|j|jfV  qd S r4   )rD   rc   rN   r>   rA   rY   r8   r8   r9   rG      s   z#EagerSelectorNamespace._iter_schemaIterator[SeriesT]c                c      |  E d H  d S r4   )iter_columnsrC   r8   r8   r9   rD         z$EagerSelectorNamespace._iter_columnsIterator[tuple[SeriesT, DType]]c                c  s"    |  |D ]}||jfV  qd S r4   )rD   rN   r   r8   r8   r9   rI      s   z+EagerSelectorNamespace._iter_columns_dtypesN)rA   r-   r3   rF   )rA   r-   r3   r   )rA   r-   r3   r   r   r   r   rG   rD   rI   r8   r8   r8   r9   r&      s    

r&   c                   @  r   )r'   rA   r.   r3   rF   c                 c  s    |j  E d H  d S r4   )schemaitemsrC   r8   r8   r9   rG      s   z"LazySelectorNamespace._iter_schemaIterator[ExprT]c                 c  r   r4   )rD   rC   r8   r8   r9   rD      r   z#LazySelectorNamespace._iter_columnsIterator[tuple[ExprT, DType]]c                c  s"    t | ||j E d H  d S r4   )r   rD   r   valuesrC   r8   r8   r9   rI      s    z*LazySelectorNamespace._iter_columns_dtypesN)rA   r.   r3   rF   )rA   r.   r3   r   )rA   r.   r3   r   r   r8   r8   r8   r9   r'      s    

r'   c                   @  s   e Zd ZU ded< ded< ded< ded< ed.ddZed/ddZd0ddZd1ddZ	e
d2ddZe
d3ddZd4d!dZe
d2d"d#Ze
d3d$d#Zd4d%d#Ze
d2d&d'Ze
d3d(d'Zd4d)d'Zd5d+d,Zd-S )6r$   !EvalSeries[FrameT, SeriesOrExprT]_callrv   _function_namer   r0   r    r1   callevaluate_output_namesEvalNames[FrameT]r2   r!   r3   r   c                C  s0   |  | }||_||_d |_|j|_|j|_|S r4   )r5   r   _evaluate_output_names_alias_output_namesr0   r1   )r6   r   r   r2   r7   r8   r8   r9   rl      s   
z CompliantSelector.from_callablesrM   c                 C  s
   |   jS r4   )__narwhals_namespace__	selectorsr=   r8   r8   r9   r      s   
zCompliantSelector.selectors$CompliantExpr[FrameT, SeriesOrExprT]c                 C  r<   r4   r8   r=   r8   r8   r9   _to_expr   rE   zCompliantSelector._to_exprother+Self | CompliantExpr[FrameT, SeriesOrExprT]0TypeIs[CompliantSelector[FrameT, SeriesOrExprT]]c                 C  s   t |t| S r4   )rV   typer>   r   r8   r8   r9   _is_selector   s   zCompliantSelector._is_selectorc                 C  r<   r4   r8   r   r8   r8   r9   __sub__   r@   zCompliantSelector.__sub__c                 C  r<   r4   r8   r   r8   r8   r9   r         %SelectorOrExpr[FrameT, SeriesOrExprT]c                   sJ     rd fdd}d fdd	}jjj||d
S    S )NrA   r,   r3   rR   c                   ,   t | \}  fddt| |D S )Nc                   s   g | ]
\}}| vr|qS r8   r8   rX   xrc   	rhs_namesr8   r9   r\      s
    z=CompliantSelector.__sub__.<locals>.series.<locals>.<listcomp>_eval_lhs_rhsr   rA   	lhs_namesr   r>   r   r9   r`      s   
z)CompliantSelector.__sub__.<locals>.seriesra   c                   "   t | \}  fdd|D S )Nc                   s   g | ]}| vr|qS r8   r8   rX   r   r   r8   r9   r\          z<CompliantSelector.__sub__.<locals>.names.<locals>.<listcomp>r   r   r   r   r9   rf         z(CompliantSelector.__sub__.<locals>.namesrh   ri   rj   r   r   r?   rl   r   r>   r   r`   rf   r8   r   r9   r      s
   
c                 C  r<   r4   r8   r   r8   r8   r9   __or__  r@   zCompliantSelector.__or__c                 C  r<   r4   r8   r   r8   r8   r9   r     r   c                   sJ     rd fdd}d fdd	}jjj||d
S   B S )NrA   r,   r3   rR   c                   s8   t | \} g  fddt| |D | S )Nc                 3  s     | ]\}}| vr|V  qd S r4   r8   r   r   r8   r9   	<genexpr>  s    z;CompliantSelector.__or__.<locals>.series.<locals>.<genexpr>r   r   r   r   r9   r`     s   
z(CompliantSelector.__or__.<locals>.seriesra   c                   s*   t | \} g  fdd|D  S )Nc                 3  s    | ]	}| vr|V  qd S r4   r8   r   r   r8   r9   r     s    z:CompliantSelector.__or__.<locals>.names.<locals>.<genexpr>r   r   r   r   r9   rf     s   z'CompliantSelector.__or__.<locals>.namesrh   ri   rj   r   r   r8   r   r9   r   
  s
   
c                 C  r<   r4   r8   r   r8   r8   r9   __and__!  r@   zCompliantSelector.__and__c                 C  r<   r4   r8   r   r8   r8   r9   r   #  r   c                   sJ     rd fdd}d fdd	}jjj||d
S   @ S )NrA   r,   r3   rR   c                   r   )Nc                   rp   r8   r8   r   r   r8   r9   r\   .  s    z=CompliantSelector.__and__.<locals>.series.<locals>.<listcomp>r   r   r   r   r9   r`   ,  s   
z)CompliantSelector.__and__.<locals>.seriesra   c                   r   )Nc                   s   g | ]}| v r|qS r8   r8   r   r   r8   r9   r\   4  r   z<CompliantSelector.__and__.<locals>.names.<locals>.<listcomp>r   r   r   r   r9   rf   2  r   z(CompliantSelector.__and__.<locals>.namesrh   ri   rj   r   r   r8   r   r9   r   '  s
   
rP   c                 C  s   | j  |  S r4   )r   r   r=   r8   r8   r9   
__invert__9  r   zCompliantSelector.__invert__N)r   r   r   r   r2   r!   r3   r   )r3   rM   )r3   r   )r   r   r3   r   )r   r   r3   r   )r   r   r3   r   )r   r   r3   r   r   )r   r   r   r   r   rl   r   r   r   r   r   r   r   r   r   r8   r8   r8   r9   r$      s6   
 




r$   rA   lhsr   rhsr3   #tuple[Sequence[str], Sequence[str]]c                 C  s   | | | | fS r4   )r   )rA   r   r   r8   r8   r9   r   =  s   r   )rA   r   r   r   r   r   r3   r   )=__doc__
__future__r   r   	functoolsr   typingr   r   r   r   narwhals._compliant.exprr   narwhals._utilsr	   r
   r   r   r   collections.abcr   r   r   r   r   r   typing_extensionsr   r   r   r   narwhals._compliant.typingr   r   r   r   r   r   r   r   r   r    r!   narwhals.dtypesr"   narwhals.typingr#   __all__r(   r*   r+   r,   r-   r.   r/   r   r%   r&   r'   r$   r   r8   r8   r8   r9   <module>   sH    (


q

v