o
    $&]iA                     @  s  U 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mZ d dlmZmZ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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,m-Z-m.Z.m/Z/m0Z0 d d	l1m2Z2 e
rd d
l3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9Z:d dl;Z<d dl=Z>d dl?m@Z@mAZA d dlBmCZCmDZD d dlEmFZF d dlGmHZH d dlmIZI d dlJmKZKmLZL d dl$mMZMmNZN d dlOmPZP d dlQmRZR d dl1mSZS d dlTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZb eZcdedd< g dZeedZfdZgdedd< edZhG d d! d!e'eh eeh e e! eeehe!f ZiG d"d# d#ed$ ed%eejef f ed& eeieee!f eeeee!f ZkG d'd( d(eieee!f eeee!f ZlG d)d* d*ekeeed+f eledd+f e%eeeeef ZmdS ),    )annotations)IteratorMappingSequenceSized)chain)TYPE_CHECKINGAnyLiteralProtocolTypeVaroverload)
CompliantDataFrameAnyCompliantExprT_contraCompliantLazyFrameAnyCompliantNamespaceAnyCompliantSeriesT
EagerExprTEagerSeriesTNativeDataFrameTNativeLazyFrameTNativeSeriesT)ArrowConvertibleDictConvertible
FromNativeNumpyConvertible
ToNarwhalsToNarwhalsT_co)assert_never)ValidateBackendVersionVersion_StoresNativecheck_columns_existis_boolean_selectoris_compliant_seriesis_index_selectoris_rangeis_sequence_likeis_sized_multi_index_selectoris_slice_indexis_slice_none)MultiOutputExpressionError)BytesIO)Path)
ModuleTypeN)Self	TypeAlias)CompliantGroupByDataFrameGroupBy)EagerNamespace)SparkSession)IntoArrowTable)_EagerAllowedImpl_LazyAllowedImpl)Implementation_LimitedContext)	DataFrame)DType)ColumnNotFoundError)AsofJoinStrategy
IntoSchemaJoinStrategyMultiColSelectorMultiIndexSelectorPivotAggSingleIndexSelectorSizedMultiIndexSelectorSizedMultiNameSelectorSizeUnitUniqueKeepStrategy_2DArray_SliceIndex
_SliceNamer0   
Incomplete)CompliantDataFrameCompliantFrameCompliantLazyFrameEagerDataFrameT2dict[str, CompliantSeriesT] | dict[str, list[Any]]_ToDict_NativeFrameTc                   @  s4  e Zd ZU dZded< ded< ded< dod
dZdpddZdqddZdrddZe	dsddZ
edtddZeduddZedvd!d"Zdvd#d$Zdwd(d)Zdxd,d-Zdyd.d/Zdzd2d3Zd{d8d9Zd|d<d=Zd}dEdFZd~dKdLZddOdPZddSdTZddVdWZdd\d]ZddadbZddgdhZddidjZddldmZdnS )rM   z)Common parts of `DataFrame`, `LazyFrame`.rS   _native_framer8   _implementationr    _versionreturnr.   c                 C     d S N selfrZ   rZ   [/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/_compliant/dataframe.py__native_namespace__g       z#CompliantFrame.__native_namespace__r	   c                 C  rX   rY   rZ   r[   rZ   rZ   r]   __narwhals_namespace__h   r_   z%CompliantFrame.__narwhals_namespace__dfr/   c                 C  rX   rY   rZ   )r\   ra   rZ   rZ   r]   _with_nativei   r_   zCompliantFrame._with_nativeversionc                 C  rX   rY   rZ   )r\   rc   rZ   rZ   r]   _with_versionj   r_   zCompliantFrame._with_versiondatacontextr9   c               C  rX   rY   rZ   clsre   rf   rZ   rZ   r]   from_nativek      zCompliantFrame.from_nativeSequence[str]c                 C  rX   rY   rZ   r[   rZ   rZ   r]   columnsm   rj   zCompliantFrame.columnsc                 C  s   | j S rY   )rT   r[   rZ   rZ   r]   nativeo   s   zCompliantFrame.nativeMapping[str, DType]c                 C  rX   rY   rZ   r[   rZ   rZ   r]   schemas   rj   zCompliantFrame.schemac                 C  rX   rY   rZ   r[   rZ   rZ   r]   collect_schemav   r_   zCompliantFrame.collect_schemarl   strictboolc                C  rX   rY   rZ   )r\   rl   rq   rZ   rZ   r]   dropw   r_   zCompliantFrame.dropsubsetSequence[str] | Nonec                 C  rX   rY   rZ   r\   rt   rZ   rZ   r]   
drop_nullsx   r_   zCompliantFrame.drop_nullsc                 C  rX   rY   rZ   r\   rl   rZ   rZ   r]   explodey   r_   zCompliantFrame.explode	predicate"CompliantExprT_contra | Incompletec                 C  rX   rY   rZ   )r\   rz   rZ   rZ   r]   filterz   r_   zCompliantFrame.filterkeys/Sequence[str] | Sequence[CompliantExprT_contra]drop_null_keys-CompliantGroupBy[Self, CompliantExprT_contra]c                C  rX   rY   rZ   r\   r}   r   rZ   rZ   r]   group_by{      zCompliantFrame.group_bynintc                 C  rX   rY   rZ   )r\   r   rZ   rZ   r]   head   r_   zCompliantFrame.headotherhowr?   left_onright_onsuffixstrc                C  rX   rY   rZ   )r\   r   r   r   r   r   rZ   rZ   r]   join      zCompliantFrame.joinby_leftby_rightstrategyr=   c                C  rX   rY   rZ   )r\   r   r   r   r   r   r   r   rZ   rZ   r]   	join_asof   s   
zCompliantFrame.join_asofmappingMapping[str, str]c                 C  rX   rY   rZ   )r\   r   rZ   rZ   r]   rename   r_   zCompliantFrame.renameexprsr   c                 G  rX   rY   rZ   r\   r   rZ   rZ   r]   select   r_   zCompliantFrame.selectcolumn_namesc                 G     dS )z)`select` where all args are column names.NrZ   )r\   r   rZ   rZ   r]   simple_select      zCompliantFrame.simple_selectby
descendingbool | Sequence[bool]
nulls_lastc                G  rX   rY   rZ   )r\   r   r   r   rZ   rZ   r]   sort   r   zCompliantFrame.sortkeeprG   order_byc                C  rX   rY   rZ   )r\   rt   r   r   rZ   rZ   r]   unique      zCompliantFrame.uniqueonindexvariable_name
value_namec                 C  rX   rY   rZ   )r\   r   r   r   r   rZ   rZ   r]   unpivot   r   zCompliantFrame.unpivotc                 G  rX   rY   rZ   r   rZ   rZ   r]   with_columns   r_   zCompliantFrame.with_columnsnamec                 C  rX   rY   rZ   r\   r   r   rZ   rZ   r]   with_row_index   r_   zCompliantFrame.with_row_indexN)rW   r.   )rW   r	   )ra   rS   rW   r/   )rc   r    rW   r/   )re   rS   rf   r9   rW   r/   )rW   rk   )rW   rS   )rW   rn   )rl   rk   rq   rr   rW   r/   )rt   ru   rW   r/   )rl   rk   rW   r/   )rz   r{   rW   r/   )r}   r~   r   rr   rW   r   )r   r   rW   r/   )r   r/   r   r?   r   ru   r   ru   r   r   rW   r/   )r   r/   r   r   r   r   r   ru   r   ru   r   r=   r   r   rW   r/   )r   r   rW   r/   r   r   rW   r/   )r   r   rW   r/   )r   r   r   r   r   rr   rW   r/   )rt   ru   r   rG   r   ru   rW   r/   )
r   ru   r   ru   r   r   r   r   rW   r/   )r   r   r   rk   rW   r/   ) __name__
__module____qualname____doc____annotations__r^   r`   rb   rd   classmethodri   propertyrl   rm   ro   rp   rs   rw   ry   r|   r   r   r   r   r   r   r   r   r   r   r   r   rZ   rZ   rZ   r]   rM   [   sD   
 












	






rM   c                   @  s  e Zd ZdddZedd	d
ZedddZedddZedddZdddZ	dd d!Z
edd#d$Zdd%d&Zdd*d+Zdd/d0Zdd4d5Zdd;d<ZddAdBZddDdEZddIdJZddKdLZddRdSZdd]d^Zdd`daZddcddZddidjZddldmZddodpZddrdsZeddwdxZedd{dxZdd}dxZd~ddddZ dddZ!edddZ"edddZ"dddZ"dddZ#d~S )rL   rW   r/   c                 C  rX   rY   rZ   r[   rZ   rZ   r]   __narwhals_dataframe__   r_   z)CompliantDataFrame.__narwhals_dataframe__re   r5   rf   r9   c               C  rX   rY   rZ   rg   rZ   rZ   r]   
from_arrow   rj   zCompliantDataFrame.from_arrowMapping[str, Any]r   ro   .IntoSchema | Mapping[str, DType | None] | Nonec               C  rX   rY   rZ   rh   re   rf   ro   rZ   rZ   r]   	from_dict   r   zCompliantDataFrame.from_dictSequence[Mapping[str, Any]]c               C  rX   rY   rZ   r   rZ   rZ   r]   
from_dicts   r   zCompliantDataFrame.from_dictsrH   !IntoSchema | Sequence[str] | Nonec               C  rX   rY   rZ   r   rZ   rZ   r]   
from_numpy   r   zCompliantDataFrame.from_numpydtyper	   copybool | Nonec                C  rX   rY   rZ   )r\   r   r   rZ   rZ   r]   	__array__   r_   zCompliantDataFrame.__array__itemetuple[SingleIndexSelector | MultiIndexSelector[CompliantSeriesT], MultiColSelector[CompliantSeriesT]]c                 C  rX   rY   rZ   )r\   r   rZ   rZ   r]   __getitem__   r   zCompliantDataFrame.__getitem__tuple[int, int]c                 C  rX   rY   rZ   r[   rZ   rZ   r]   shape   rj   zCompliantDataFrame.shapec                 C  rX   rY   rZ   r[   rZ   rZ   r]   clone   r_   zCompliantDataFrame.cloneunitrF   int | floatc                 C  rX   rY   rZ   )r\   r   rZ   rZ   r]   estimated_size   r_   z!CompliantDataFrame.estimated_sizer   r   offsetc                 C  rX   rY   rZ   )r\   r   r   rZ   rZ   r]   gather_every   r_   zCompliantDataFrame.gather_everyr   r   r   c                 C  rX   rY   rZ   )r\   r   rZ   rZ   r]   
get_column   r_   zCompliantDataFrame.get_columnr}   r~   r   rr   DataFrameGroupBy[Self, Any]c                C  rX   rY   rZ   r   rZ   rZ   r]   r      r   zCompliantDataFrame.group_byrow
int | Nonecolumnint | str | Nonec                 C  rX   rY   rZ   )r\   r   r   rZ   rZ   r]   r      r_   zCompliantDataFrame.itemIterator[CompliantSeriesT]c                 C  rX   rY   rZ   r[   rZ   rZ   r]   iter_columns   r_   zCompliantDataFrame.iter_columnsnamedbuffer_size7Iterator[tuple[Any, ...]] | Iterator[Mapping[str, Any]]c                C  rX   rY   rZ   )r\   r   r   rZ   rZ   r]   	iter_rows   r   zCompliantDataFrame.iter_rowsc                 C  rX   rY   rZ   r[   rZ   rZ   r]   	is_unique   r_   zCompliantDataFrame.is_uniquebackend_LazyAllowedImpl | NonesessionSparkSession | Noner   c                C  rX   rY   rZ   )r\   r   r   rZ   rZ   r]   lazy   r   zCompliantDataFrame.lazyr   rk   r   ru   valuesaggregate_functionPivotAgg | Nonesort_columns	separatorc                C  rX   rY   rZ   )r\   r   r   r   r   r   r   rZ   rZ   r]   pivot   s   	zCompliantDataFrame.pivottuple[Any, ...]c                 C  rX   rY   rZ   )r\   r   rZ   rZ   r]   r      r_   zCompliantDataFrame.row7Sequence[tuple[Any, ...]] | Sequence[Mapping[str, Any]]c                C  rX   rY   rZ   )r\   r   rZ   rZ   r]   rows   r   zCompliantDataFrame.rowsfractionfloat | Nonewith_replacementseedc                C  rX   rY   rZ   )r\   r   r   r   r   rZ   rZ   r]   sample     zCompliantDataFrame.samplepa.Tablec                 C  rX   rY   rZ   r[   rZ   rZ   r]   to_arrow  r_   zCompliantDataFrame.to_arrowpd.DataFramec                 C  rX   rY   rZ   r[   rZ   rZ   r]   	to_pandas  r_   zCompliantDataFrame.to_pandaspl.DataFramec                 C  rX   rY   rZ   r[   rZ   rZ   r]   	to_polars  r_   zCompliantDataFrame.to_polars	as_seriesLiteral[True]dict[str, CompliantSeriesT]c                C  rX   rY   rZ   r\   r   rZ   rZ   r]   to_dict  rj   zCompliantDataFrame.to_dictLiteral[False]dict[str, list[Any]]c                C  rX   rY   rZ   r   rZ   rZ   r]   r     rj   rQ   c                C  rX   rY   rZ   r   rZ   rZ   r]   r     r   N)maintain_orderrt   r   rG   r   r   c                C  rX   rY   rZ   )r\   rt   r   r   r   rZ   rZ   r]   r     r   zCompliantDataFrame.uniquec                 C  rX   rY   rZ   r   rZ   rZ   r]   r     r_   z!CompliantDataFrame.with_row_indexfileNonec                 C  rX   rY   rZ   r\   r   rZ   rZ   r]   	write_csv  rj   zCompliantDataFrame.write_csvstr | Path | BytesIOc                 C  rX   rY   rZ   r  rZ   rZ   r]   r     rj   str | Path | BytesIO | None
str | Nonec                 C  rX   rY   rZ   r  rZ   rZ   r]   r  "  r_   c                 C  rX   rY   rZ   r  rZ   rZ   r]   write_parquet#  r_   z CompliantDataFrame.write_parquetrW   r/   )re   r5   rf   r9   rW   r/   )re   r   rf   r   ro   r   rW   r/   )re   r   rf   r9   ro   r   rW   r/   )re   rH   rf   r9   ro   r   rW   r/   )r   r	   r   r   rW   rH   )r   r   rW   r/   )rW   r   )r   rF   rW   r   )r   r   r   r   rW   r/   )r   r   rW   r   )r}   r~   r   rr   rW   r   )r   r   r   r   rW   r	   )rW   r   )r   rr   r   r   rW   r   )rW   r   )r   r   r   r   rW   r   )r   rk   r   ru   r   ru   r   r   r   rr   r   r   rW   r/   )r   r   rW   r   )r   rr   rW   r   )
r   r   r   r   r   rr   r   r   rW   r/   )rW   r   )rW   r   )rW   r   )r   r   rW   r   )r   r   rW   r   )r   rr   rW   rQ   )
rt   ru   r   rG   r   r   r   ru   rW   r/   )r   r   r   ru   rW   r/   )r   r   rW   r   r   r  rW   r   )r   r  rW   r  )$r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  rZ   rZ   rZ   r]   rL      sZ    























rL   )rH   rH   z_ToDict[CompliantSeriesT])r   r5   c                   @  s>   e Zd ZdddZdddZdd
dZdddZdddZdS )rN   rW   r/   c                 C  rX   rY   rZ   r[   rZ   rZ   r]   __narwhals_lazyframe__*  r_   z)CompliantLazyFrame.__narwhals_lazyframe__Iterator[Any]c                 C  rX   rY   rZ   r[   rZ   rZ   r]   _iter_columns,  r_   z CompliantLazyFrame._iter_columnsr   r   c                 G  r   )zk`select` where all args are aggregations or literals.

        (so, no broadcasting is necessary).
        NrZ   r   rZ   rZ   r]   	aggregate-  r   zCompliantLazyFrame.aggregater   _EagerAllowedImpl | Nonekwargsr	   r   c                 K  rX   rY   rZ   )r\   r   r  rZ   rZ   r]   collect4  r   zCompliantLazyFrame.collectr   r  r   c                 C  rX   rY   rZ   r  rZ   rZ   r]   sink_parquet7  r_   zCompliantLazyFrame.sink_parquetNr  )rW   r
  r   )r   r  r  r	   rW   r   r  )r   r   r   r	  r  r  r  r  rZ   rZ   rZ   r]   rN   &  s    



rN   c                   @  s   e Zd ZedLddZdMddZdNd	d
ZdOddZdddPddZdQddZ	dRdd Z
dSd"d#ZdTd$d%ZdUd(d)ZedVd/d0ZdWd3d4ZdXd6d7ZdYd8d9ZdZd;d<Zd[d=d>Zd\d@dAZd]dDdEZd^dIdJZdKS )_rO   rW   tuple[int, ...]c                 C  s
   | j  S rY   )rU   _backend_versionr[   rZ   rZ   r]   r  B  s   
zEagerDataFrame._backend_versionOEagerNamespace[Self, EagerSeriesT, EagerExprT, NativeDataFrameT, NativeSeriesT]c                 C  rX   rY   rZ   r[   rZ   rZ   r]   r`   F  s   z%EagerDataFrame.__narwhals_namespace__DataFrame[NativeDataFrameT]c                 C  s   | j j| ddS )Nfull)level)rV   	dataframer[   rZ   rZ   r]   to_narwhalsL  s   zEagerDataFrame.to_narwhalsr   r   r/   c                 G  s
   | j | S rY   )r   r   rZ   rZ   r]   r  O  s   
zEagerDataFrame.aggregateT)validate_column_namesra   r   r  rr   c                C  rX   rY   rZ   )r\   ra   r  rZ   rZ   r]   rb   Y  r   zEagerDataFrame._with_nativert   rk   ColumnNotFoundError | Nonec                 C  s   t || jdS )N)	available)r"   rl   rv   rZ   rZ   r]   _check_columns_exist]  s   z#EagerDataFrame._check_columns_existexprr   c                C  s*   |  |}t|dkrd}t||d S )z6Evaluate `expr` and ensure it has a **single** output.   z4multi-output expressions not allowed in this contextr   )_evaluate_exprlenr+   )r\   r  resultmsgrZ   rZ   r]   _evaluate_single_output_expr`  s
   
z+EagerDataFrame._evaluate_single_output_exprSequence[EagerSeriesT]c                   s   t t fdd|D S )Nc                 3  s    | ]}  |V  qd S rY   )r  ).0r  r[   rZ   r]   	<genexpr>o      z1EagerDataFrame._evaluate_exprs.<locals>.<genexpr>)tupler   from_iterabler   rZ   r[   r]   _evaluate_exprsk  s   zEagerDataFrame._evaluate_exprsc                C  sH   | | }|| }t|dd |D  }kr"d| d| }t||S )a  Return list of raw columns.

        For eager backends we alias operations at each step.

        As a safety precaution, here we can check that the expected result names match those
        we were expecting from the various `evaluate_output_names` / `alias_output_names` calls.

        Note that for PySpark / DuckDB, we are less free to liberally set aliases whenever we want.
        c                 S  s   g | ]}|j qS rZ   )r   )r%  srZ   rZ   r]   
<listcomp>~  s    z1EagerDataFrame._evaluate_expr.<locals>.<listcomp>z"Safety assertion failed, expected z, got )_evaluate_aliaseslistAssertionError)r\   r  aliasesr!  result_aliasesr"  rZ   rZ   r]   r  q  s   

zEagerDataFrame._evaluate_exprr   r	   c                C  r   )z@Extract native Series, broadcasting to `len(self)` if necessary.NrZ   )r\   r   rZ   rZ   r]   _extract_comparand  r   z!EagerDataFrame._extract_comparandre   rH   rl   ru   	list[str]c                C  s    t |pdd t| jd D S )Nc                 s  s    | ]}d | V  qdS )column_NrZ   )r%  xrZ   rZ   r]   r&    r'  z5EagerDataFrame._numpy_column_names.<locals>.<genexpr>r  )r.  ranger   )re   rl   rZ   rZ   r]   _numpy_column_names  s    z"EagerDataFrame._numpy_column_namesr   &SizedMultiIndexSelector[NativeSeriesT]c                 C  rX   rY   rZ   r\   r   rZ   rZ   r]   _gather  r_   zEagerDataFrame._gather_SliceIndex | rangec                 C  rX   rY   rZ   r9  rZ   rZ   r]   _gather_slice  r_   zEagerDataFrame._gather_slicec                 C  rX   rY   rZ   rx   rZ   rZ   r]   _select_multi_index  r   z"EagerDataFrame._select_multi_index%SizedMultiNameSelector[NativeSeriesT]c                 C  rX   rY   rZ   rx   rZ   rZ   r]   _select_multi_name  r   z!EagerDataFrame._select_multi_namec                 C  rX   rY   rZ   rx   rZ   rZ   r]   _select_slice_index  r_   z"EagerDataFrame._select_slice_indexrJ   c                 C  rX   rY   rZ   rx   rZ   rZ   r]   _select_slice_name  r_   z!EagerDataFrame._select_slice_namer   ]tuple[SingleIndexSelector | MultiIndexSelector[EagerSeriesT], MultiColSelector[EagerSeriesT]]c                 C  s^  |\}}| }t |sst|trt|dkr| S t|r,|jdd t|j|D  }nGt	|rOt
|s8t|r>||}n5t|rI| |j}n*||}n$t|trZ||}nt|re| |j}nt|ro| |}nt| t |st|tr||g}|S t|ttfr||}|S t|r||j}|S t|r||}|S t| |S )Nr   c                 s  s    | ]	\}}|r|V  qd S rY   rZ   )r%  colr   rZ   rZ   r]   r&    s    z-EagerDataFrame.__getitem__.<locals>.<genexpr>)r*   
isinstancer   r   r   r#   r   ziprl   r%   r)   r&   r@  r$   r=  rm   slicerA  r?  r'   r   r   r:  r6  r<  r(   )r\   r   r   rl   	compliantrZ   rZ   r]   r     sJ   




zEagerDataFrame.__getitem__r   r  r   c                 C  s
   |  |S rY   )r  r  rZ   rZ   r]   r    s   
zEagerDataFrame.sink_parquetN)rW   r  )rW   r  )rW   r  )r   r   rW   r/   )ra   r   r  rr   rW   r/   )rt   rk   rW   r  )r  r   rW   r   )r   r   rW   r$  )r  r   rW   r$  )r   r   rW   r	   )re   rH   rl   ru   rW   r3  )r   r8  rW   r/   )r   r;  rW   r/   )rl   r8  rW   r/   )rl   r>  rW   r/   )rl   r;  rW   r/   )rl   rJ   rW   r/   )r   rB  rW   r/   r  )r   r   r   r   r  r`   r  r  rb   r  r#  r*  r  r2  staticmethodr7  r:  r<  r=  r?  r@  rA  r   r  rZ   rZ   rZ   r]   rO   :  s.    














.rO   r  )n
__future__r   collections.abcr   r   r   r   	itertoolsr   typingr   r	   r
   r   r   r   narwhals._compliant.typingr   r   r   r   r   r   r   r   r   r   narwhals._translater   r   r   r   r   r   narwhals._typing_compatr   narwhals._utilsr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   narwhals.exceptionsr+   ior,   pathlibr-   typesr.   pandaspdpolarsplpyarrowpatyping_extensionsr/   r0   narwhals._compliant.group_byr1   r2   narwhals._compliant.namespacer3   narwhals._spark_like.utilsr4   r5   narwhals._typingr6   r7   r8   r9   narwhals.dataframer:   narwhals.dtypesr;   r<   narwhals.typingr=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   r   __all__rP   rR   rS   rM   r   rL   rN   rO   rZ   rZ   rZ   r]   <module>   st     0 8@

V
u

