
    }ii^                       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
mZmZmZmZ d dlmZ d dlmZmZ d dl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!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- e	r:d dl.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE  ed          ZFeGeHeIeJeKeLefZMejN        ejO        ejP        fZQeddd]d            ZReddd^d            ZReddd_d            ZRed`d"            ZRd#ddad&ZRedbd+            ZSedcd-            ZSeddd0            ZSeded3            ZSedfd4            ZSedgd5            ZSedhd6            ZSedid7            ZSedjd:            ZSedkd=            ZSedld?            ZSedmdD            ZSd#d#d#ddEdndHZSd#d#dIdodNZTd#d#dIdpdOZUdqdRZVdqdSZW	 drdTd#d#dTdEdsdXZXdtdZZYdud[ZZg d\Z[dS )v    )annotationsN)Decimalwraps)TYPE_CHECKINGAnyCallableLiteralTypeVaroverload)plugins)EPOCHMS_PER_SECOND)is_native_arrowis_native_pandas_likeis_native_polarsis_native_spark_like)ImplementationVersionhas_native_namespaceis_compliant_dataframeis_compliant_lazyframeis_compliant_series)get_dask_expr	get_numpy
get_pandasis_cupy_scalaris_dask_dataframeis_duckdb_relationis_ibis_tableis_numpy_scalaris_pandas_like_dataframeis_polars_lazyframeis_polars_seriesis_pyarrow_scalaris_pyarrow_table)Unpack)AllowAny	AllowLazyAllowSeriesExcludeSeries
OnlySeriesPassThroughUnknown	DataFrame	LazyFrameSeries)	
DataFrameTFrameIntoDataFrameT	IntoFrameIntoLazyFrameT
IntoSeriesIntoSeriesT
LazyFrameTSeriesTT.pass_throughnarwhals_objectDataFrame[IntoDataFrameT]r>   Literal[False]returnr5   c                   d S N r?   r>   s     F/home/jrussi/.local/lib/python3.11/site-packages/narwhals/translate.py	to_nativerH   G   	     S    LazyFrame[IntoLazyFrameT]r7   c                   d S rD   rE   rF   s     rG   rH   rH   K   rI   rJ   Series[IntoSeriesT]r9   c                   d S rD   rE   rF   s     rG   rH   rH   O   s	     #rJ   r   boolc                   d S rD   rE   rF   s     rG   rH   rH   S   s    CF3rJ   FKDataFrame[IntoDataFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT]3IntoDataFrameT | IntoLazyFrameT | IntoSeriesT | Anyc                   ddl m} ddlm} t	          | |          r| j        j        S t	          | |          r| j        j        S |s"dt          |            d}t          |          | S )a]  Convert Narwhals object to native one.

    Arguments:
        narwhals_object: Narwhals object.
        pass_through: Determine what happens if `narwhals_object` isn't a Narwhals class

            - `False` (default): raise an error
            - `True`: pass object through as-is

    Returns:
        Object of class that user started with.
    r   )	BaseFramer1   zExpected Narwhals object, got .)narwhals.dataframerT   narwhals.seriesr2   
isinstance_compliant_frame_native_frame_compliant_seriesnativetype	TypeError)r?   r>   rT   r2   msgs        rG   rH   rH   W   s    & -,,,,,&&&&&&/9-- >/==/6** 8077 GtO/D/DGGGnnrJ   native_objectr;   kwdsUnpack[OnlySeries]c                    d S rD   rE   r`   ra   s     rG   from_nativere   x   s    PSPSrJ   Unpack[AllowSeries]c                    d S rD   rE   rd   s     rG   re   re   z   s    QTQTrJ   r3   Unpack[ExcludeSeries]c                    d S rD   rE   rd   s     rG   re   re   |   s	     rJ   r:   Unpack[AllowLazy]c                    d S rD   rE   rd   s     rG   re   re      s    UXUXrJ   c                    d S rD   rE   rd   s     rG   re   re      	     !$rJ   c                    d S rD   rE   rd   s     rG   re   re      	     #rJ   c                    d S rD   rE   rd   s     rG   re   re      ro   rJ   c                    d S rD   rE   rd   s     rG   re   re      rm   rJ   IntoDataFrameT | IntoSeriesT/DataFrame[IntoDataFrameT] | Series[IntoSeriesT]c                    d S rD   rE   rd   s     rG   re   re      s	     7:crJ   -IntoDataFrameT | IntoLazyFrameT | IntoSeriesTUnpack[AllowAny]c                    d S rD   rE   rd   s     rG   re   re      s     SVRUrJ   Unpack[PassThroughUnknown]c                    d S rD   rE   rd   s     rG   re   re      s    LOCrJ   
eager_onlyseries_onlyallow_seriesbool | Nonec                   d S rD   rE   r`   r>   rz   r{   r|   s        rG   re   re      s	     #rJ   r>   rz   r{   r|   JIntoLazyFrameT | IntoDataFrameT | IntoSeriesT | IntoFrame | IntoSeries | TOLazyFrame[IntoLazyFrameT] | DataFrame[IntoDataFrameT] | Series[IntoSeriesT] | Tc          	     B    t          | ||d||t          j                  S )a  Convert `native_object` to Narwhals Dataframe, Lazyframe, or Series.

    Arguments:
        native_object: Raw object from user.
            Depending on the other arguments, input object can be

            - a Dataframe / Lazyframe / Series supported by Narwhals (pandas, Polars, PyArrow, ...)
            - an object which implements `__narwhals_dataframe__`, `__narwhals_lazyframe__`,
              or `__narwhals_series__`
        pass_through: Determine what happens if the object can't be converted to Narwhals

            - `False` (default): raise an error
            - `True`: pass object through as-is
        eager_only: Whether to only allow eager objects

            - `False` (default): don't require `native_object` to be eager
            - `True`: only convert to Narwhals if `native_object` is eager
        series_only: Whether to only allow Series

            - `False` (default): don't require `native_object` to be a Series
            - `True`: only convert to Narwhals if `native_object` is a Series
        allow_series: Whether to allow Series (default is only Dataframe / Lazyframe)

            - `False` or `None` (default): don't convert to Narwhals if `native_object` is a Series
            - `True`: allow `native_object` to be a Series

    Returns:
        DataFrame, LazyFrame, Series, or original object, depending
            on which combination of parameters was passed.
    Fr>   rz   eager_or_interchange_onlyr{   r|   version)_from_native_implr   MAINr   s        rG   re   re      s3    V !"'!   rJ   )r>   rz   compliant_objectr   r   r   c                  t          |           rS|r|sd}t          |          | S |                    |                                                     |          d          S t          |           rl|r|sd}t          |          | S |s|r|sd}t          |          | S |                    |                                                     |          d          S t          |           rS|s|sd}t          |          | S |	                    | 
                                                    |          d          S d S )Nz,Cannot only use `series_only` with dataframefulllevelz,Cannot only use `series_only` with lazyframezJCannot only use `eager_only` or `eager_or_interchange_only` with lazyframe4Please set `allow_series=True` or `series_only=True`)r   r^   	dataframe__narwhals_dataframe___with_versionr   	lazyframe__narwhals_lazyframe__r   series__narwhals_series__)r   r>   rz   r   r{   r|   r   r_   s           rG   _translate_if_compliantr      s    .// 
 	$ %Dnn$##  3355CCGLLTZ ! 
 
 	
 .// 
 	$ %Dnn$## 	$2 	$ %bnn$##  3355CCGLLTZ ! 
 
 	
 +,, 
 	$ %Lnn$##~~0022@@IIQW  
 
 	
 4rJ   c          	        ddl m} ddlm}m}	 ddlm}
 t          | ||	f          re|sc| j        |u r| S | 	                                }|j
                            |          j                            |                                          S t          | |
          rg|s|rc| j        |u r| S | 	                                }|j
                            |          j                            |                                          S |r|du rd}t          |          d}|r|rd}t          |          t!          | ||||||	          x}	 |S t#          |           r|r9t%          |           s*|s&d
t'          |           j         }t+          |          | S |s|r$t-          |           r|sd}t+          |          | S |s$t%          |           r|sd}t+          |          | S |j
                            |           j                            |                                           S t/          |           rt1          |           r-|r*|s&d
t'          |           j         }t+          |          | S n|s|sd}t+          |          | S |j
                            |           j                            |                                           S t3          |           rt5          |           r-|r*|s&d
t'          |           j         }t+          |          | S n|s|sd}t+          |          | S |j
                            |           j                            |                                           S t7          |           r|r|sd}t+          |          | S |s|r|sd}t+          |          | S t8          j                                        dk    rt?                      d}tA          |          |j
        !                    t8          j                  j                            |                                           S tE          |           r]|s|r|sd}t+          |          | S |j
                            |           j                            |                                           S tG          |           r]|s|r|sd}t+          |          | S |j
                            |           j                            |                                           S tI          |           rj|j
                            |           }|s|s|r|sd|j%         d}t+          |          | S |j                            |                                           S |tL          j'        u rT ||           rIddl m(} |s|r|sd}t+          |          | S tL          j'        )                     ||           d          S tU          j        | |          x}t!          |||||||	          S |sLdt'          |            }tU          j+        t'          |                     x}r|d|z   z  }t+          |          | S )Nr   )supports_dataframe_interchanger.   r1   FzJInvalid parameter combination: `series_only=True` and `allow_series=False`TzUInvalid parameter combination: `eager_only=True` and `eager_or_interchange_only=True`r   z#Cannot only use `series_only` with zQCannot only use `eager_only` or `eager_or_interchange_only` with polars.LazyFramer   z1Cannot only use `series_only` with dask DataFramezOCannot only use `eager_only` or `eager_or_interchange_only` with dask DataFrame)i        zPlease install dask-exprzNCannot only use `series_only=True` or `eager_only=False` with DuckDBPyRelationzHCannot only use `series_only=True` or `eager_only=False` with ibis.TablezPCannot only use `series_only`, `eager_only` or `eager_or_interchange_only` with z
 DataFrame)InterchangeFramezhCannot only use `series_only=True` or `eager_only=False` with object which only implements __dataframe__interchanger   z!Unsupported dataframe type, got: z

),narwhals._interchange.dataframer   rV   r/   r0   rW   r2   rX   _versionrH   	namespacefrom_native_object	compliantre   to_narwhals
ValueErrorr   r   r$   r]   __qualname__r^   r#   r   r"   r   r&   r   r   DASK_backend_versionr   ImportErrorfrom_backendr   r    r   implementationr   V1r   r   r   _show_suggestions)r`   r>   rz   r   r{   r|   r   r   r/   r0   r2   real_native_objectr_   
translatedns_sparkr   r   hints                     rG   r   r     s    ONNNNN77777777&&&&&& -)Y!788 	
 	
!W,,  *4466001CDD{{#566[]]	

 -(( 	
k 	
\ 	
!W,,  *4466001CDD{{#566[]]	
  5  ^CS//! / eoo .%!&?#%
 
 
 	

 
  && 
 	!/>> 	! %^D<O<O<\^^nn$   	!3 	!9L:
 :
 	!   %inn$   	!"2="A"A 	! %Lnn$  00??{{=11[]]	
 ]++ 
#M22 
	! %# )b]@S@S@`bbC#C..($$	%
  	! %Lnn$  00??{{=11[]]	
 }%% 
M** 
	! %# )b]@S@S@`bbC#C..($$	%
  	! %Lnn$  00??{{=11[]]	
 '' 
 	! %Inn$   	!2 	! %gnn$  0022mCC',Cc"""**>+>??{{=11[]]	
 -(( 

 	! 	! %fnn$  00??{{=11[]]	
 ]## 

 	! 	! %`nn$  00??{{=11[]]	
 M** 
K$77FF 	!* 	!(A 	! %@$3@ @ @   nn$  !--m<<HHJJJ '*!?!?!N!NDDDDDD 	! 	! %F   nn$  z##$4$4]$C$C=#YYY#/wGGGT&%!&?#%
 
 
 	
  G$}2E2EGG,T--@-@AAA4 	!6D= CnnrJ   obj,Frame | Series[Any] | IntoFrame | IntoSeriesc                     | sd}t          |          d | D             }t          |          dk    rd| d}t          |          |                                S )a  Get native namespace from object.

    Arguments:
        obj: Dataframe, Lazyframe, or Series. Multiple objects can be
            passed positionally, in which case they must all have the
            same native namespace (else an error is raised).

    Returns:
        Native module.

    Examples:
        >>> import polars as pl
        >>> import pandas as pd
        >>> import narwhals as nw
        >>> df = nw.from_native(pd.DataFrame({"a": [1, 2, 3]}))
        >>> nw.get_native_namespace(df)
        <module 'pandas'...>
        >>> df = nw.from_native(pl.DataFrame({"a": [1, 2, 3]}))
        >>> nw.get_native_namespace(df)
        <module 'polars'...>
    z=At least one object must be passed to `get_native_namespace`.c                ,    h | ]}t          |          S rE   ) _get_native_namespace_single_obj).0xs     rG   	<setcomp>z'get_native_namespace.<locals>.<setcomp>   s!    ???a.q11???rJ   r   z0Found objects with different native namespaces: rU   )r   lenpop)r   r_   results      rG   get_native_namespacer     sk    ,  Moo??3???F
6{{aJJJJoo::<<rJ   c                    t          |           r|                                 S t          j        j                            |           j                                        S rD   )r   __native_namespace__r   r   r   r   r   to_native_namespace)r   s    rG   r   r     sU     C   *'')))<!44 ((**+rJ   TfuncCallable[..., Any] | NoneCallable[..., Any]c               :    dfd}| |S  ||           S )a  Decorate function so it becomes dataframe-agnostic.

    This will try to convert any dataframe/series-like object into the Narwhals
    respective DataFrame/Series, while leaving the other parameters as they are.
    Similarly, if the output of the function is a Narwhals DataFrame or Series, it will be
    converted back to the original dataframe/series type, while if the output is another
    type it will be left as is.
    By setting `pass_through=False`, then every input and every output will be required to be a
    dataframe/series-like object.

    Arguments:
        func: Function to wrap in a `from_native`-`to_native` block.
        pass_through: Determine what happens if the object can't be converted to Narwhals

            - `False`: raise an error
            - `True` (default): pass object through as-is
        eager_only: Whether to only allow eager objects

            - `False` (default): don't require `native_object` to be eager
            - `True`: only convert to Narwhals if `native_object` is eager
        series_only: Whether to only allow Series

            - `False` (default): don't require `native_object` to be a Series
            - `True`: only convert to Narwhals if `native_object` is a Series
        allow_series: Whether to allow Series (default is only Dataframe / Lazyframe)

            - `False` or `None`: don't convert to Narwhals if `native_object` is a Series
            - `True` (default): allow `native_object` to be a Series

    Returns:
        Decorated function.

    Examples:
        Instead of writing

        >>> import narwhals as nw
        >>> def agnostic_group_by_sum(df):
        ...     df = nw.from_native(df, pass_through=True)
        ...     df = df.group_by("a").agg(nw.col("b").sum())
        ...     return nw.to_native(df)

        you can just write

        >>> @nw.narwhalify
        ... def agnostic_group_by_sum(df):
        ...     return df.group_by("a").agg(nw.col("b").sum())
    r   r   rB   c                H     t                     d fd            }|S )Nargsr   kwargsrB   c                 2   	fd| D             }	fd|                                 D             }fdg ||                                R D             }t          |          dk    rd}t          |           
|i |}t	          |          S )Nc           	     8    g | ]}t          |           S r   re   )r   argr|   rz   r>   r{   s     rG   
<listcomp>zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>L  sI     	 	 	  !-) +!-  	 	 	rJ   c           
     @    i | ]\  }}|t          |           S r   r   )r   namevaluer|   rz   r>   r{   s      rG   
<dictcomp>zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<dictcomp>W  sO     	 	 	  D% k!-) +!-  	 	 	rJ   c                J    h | ]}t          |d d          x              S )r   N)getattr)r   vbs     rG   r   zAnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<setcomp>b  sG        $:DAAAA  rJ   r   z_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r=   )itemsvaluesr   r   rH   )r   r   args_nw	kwargs_nwbackendsr_   r   r   r|   rz   r   r>   r{   s          @rG   wrapperz.narwhalify.<locals>.decorator.<locals>.wrapperJ  s
   	 	 	 	 	 	 	  	 	 	G	 	 	 	 	 	 	 $*<<>>	 	 	I   878Y%5%5%7%788  H 8}}q  w oo%T70i00FV,????rJ   )r   r   r   r   rB   r   r   )r   r   r|   rz   r>   r{   s   ` rG   	decoratorznarwhalify.<locals>.decoratorI  sX    	t#	@ #	@ #	@ #	@ #	@ #	@ #	@ #	@ #	@ 
#	@J rJ   N)r   r   rB   r   rE   )r   r>   rz   r{   r|   r   s    ```` rG   
narwhalifyr     sP    p' ' ' ' ' ' ' ' 'R |9T??rJ   scalar_likec                &   t                      }| t          | t                    r| }ngt                      x}rZt          | |j                  rE| j        dk    r:|                                 t          z  }t          t          j
        |          z   }nt          |           st          |           r|                                 }n|r*t          | |j                  r|                                 }n|r*t          | |j                  r|                                 }nrt          | t"                    r| }nZt%          |           rd}nHt'          |           r|                                 }n$dt+          |            d| }t-          |          |S )aP  If a scalar is not Python native, converts it to Python native.

    Arguments:
        scalar_like: Scalar-like value.

    Raises:
        ValueError: If the object is not convertible to a scalar.

    Examples:
        >>> import narwhals as nw
        >>> import pandas as pd
        >>> df = nw.from_native(pd.DataFrame({"a": [1, 2, 3]}))
        >>> nw.to_py_scalar(df["a"].item(0))
        1
        >>> import pyarrow as pa
        >>> df = nw.from_native(pa.table({"a": [1, 2, 3]}))
        >>> nw.to_py_scalar(df["a"].item(0))
        1
        >>> nw.to_py_scalar(1)
        1
    Nzdatetime64[ns])microsecondsz/Expected object convertible to a scalar, found z.
)r   rX   NON_TEMPORAL_SCALAR_TYPESr   
datetime64dtypeitemr   r   dt	timedeltar!   r   	Timestampto_pydatetime	Timedeltato_pytimedeltaTEMPORAL_SCALAR_TYPES_is_pandas_nar%   as_pyr]   r   )r   pdscalarnpmsr_   s         rG   to_py_scalarr   x  s   . 
Bj6OPP{{	{BM22 !111=026666		%	% )D)D !!##	 
;55 **,,	 
;55 ++-- 
K!6	7	7 	{	#	# 		;	'	' ""$$d;>O>O    	 ooMrJ   c                    t          t                      x}o3|j        j                            |           o|                    |                     S rD   )rO   r   apitypes	is_scalarisna)r   r   s     rG   r   r     s@    z||#U)?)?)D)DUQTVVVrJ   )r   r   rH   r   )r?   r@   r>   rA   rB   r5   )r?   rK   r>   rA   rB   r7   )r?   rM   r>   rA   rB   r9   )r?   r   r>   rO   rB   r   )r?   rQ   r>   rO   rB   rR   )r`   r;   ra   rb   rB   r;   )r`   r;   ra   rf   rB   r;   )r`   r3   ra   rh   rB   r3   )r`   r:   ra   rj   rB   r:   )r`   r5   ra   rh   rB   r@   )r`   r9   ra   rb   rB   rM   )r`   r9   ra   rf   rB   rM   )r`   r7   ra   rj   rB   rK   )r`   rr   ra   rf   rB   rs   )r`   ru   ra   rv   rB   rQ   )r`   r<   ra   rx   rB   r<   )r`   r   r>   rO   rz   rO   r{   rO   r|   r}   rB   r   )r`   r   r>   rO   rz   rO   r{   rO   r|   r}   rB   r   )r   r   r>   rO   rz   rO   r   rO   r{   rO   r|   r}   r   r   rB   r   )r`   r   r>   rO   rz   rO   r   rO   r{   rO   r|   r}   r   r   rB   r   )r   r   rB   r   rD   )r   r   r>   rO   rz   rO   r{   rO   r|   r}   rB   r   )r   r   rB   r   )r   r   rB   rO   )\
__future__r   datetimer   decimalr   	functoolsr   typingr   r   r	   r
   r   r   narwhalsr   narwhals._constantsr   r   narwhals._nativer   r   r   r   narwhals._utilsr   r   r   r   r   r   narwhals.dependenciesr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   typing_extensionsr'   narwhals._translater(   r)   r*   r+   r,   r-   rV   r/   r0   rW   r2   narwhals.typingr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   rO   bytesstrintfloatcomplexr   dater   timer   rH   re   r   r   r   r   r   r   r   __all__rE   rJ   rG   <module>r     sF   " " " " " "                 K K K K K K K K K K K K K K K K       4 4 4 4 4 4 4 4                                                          ((((((                87777777&&&&&&
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 GCLL!5#sE7GL ",8  
RU     
 
RU     
 
LO     
 
 F F F 
 F      B 
 S S S 
 S	 T T T 
 T	   
 
 X X X 
 X	$ $ $ 
$ 
   
 
   
 
$ $ $ 
$ 
: : : 
: 
V V V 
V 
 O O O 
 O	   
   $3 3 3 3 3 3r 	, , , , , ,d 	X X X X X Xv   @+ + + + '+d  $d d d d d dN5 5 5 5pW W W W N
M
MrJ   