
    hh}`                    @   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mZ d dlmZmZmZmZ d dl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*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7  ed          Z8e9e:e;e<e=e>efZ?ej@        ejA        ejB        fZCedddbd            ZDedddcd            ZDeddddd            ZDeded            ZDd ddfd#ZDedgd'            ZEedhd)            ZEedid+            ZEedd,djd3            ZEedddd4dkd6            ZEedddd4dld7            ZEeddd8dmd9            ZEeddd8dnd:            ZEeddd;dod=            ZEeddd>dpd?            ZEeddddd@dqdA            ZEeddddd@drdB            ZEeddddCdsdD            ZEeddddEdtdH            ZEeddddIdudJ            ZEedvdL            ZEd d d dd@dwdOZEd d d d ddPdxdTZFdydWZGdydXZH	 dzdYd d dYd@d{d]ZId|d_ZJd}d`ZKg daZLdS )~    )annotationsN)Decimalwraps)TYPE_CHECKINGAnyCallableLiteralTypeVaroverload)EPOCHMS_PER_SECOND)is_native_arrowis_native_pandas_likeis_native_polarsis_native_spark_like)ImplementationVersionhas_native_namespace)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	DataFrame	LazyFrameSeries)	
DataFrameTFrameIntoDataFrameT	IntoFrameIntoLazyFrameT
IntoSeriesIntoSeriesT
LazyFrameTSeriesTT.pass_throughnarwhals_objectDataFrame[IntoDataFrameT]r3   Literal[False]returnr*   c                   d S N r4   r3   s     I/var/www/histauto/venv/lib/python3.11/site-packages/narwhals/translate.py	to_nativer=   5   	     S    LazyFrame[IntoLazyFrameT]r,   c                   d S r9   r:   r;   s     r<   r=   r=   9   r>   r?   Series[IntoSeriesT]r.   c                   d S r9   r:   r;   s     r<   r=   r=   =   s	     #r?   r   boolc                   d S r9   r:   r;   s     r<   r=   r=   A   s    CF3r?   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   )	BaseFramer&   zExpected Narwhals object, got .)narwhals.dataframerI   narwhals.seriesr'   
isinstance_compliant_frame_native_frame_compliant_seriesnativetype	TypeError)r4   r3   rI   r'   msgs        r<   r=   r=   E   s    & -,,,,,&&&&&&/9-- >/==/6** 8077 GtO/D/DGGGnnr?   native_objectr0   kwdsc                    d S r9   r:   rU   rV   s     r<   from_nativerY   f   s    ADr?   r(   c                    d S r9   r:   rX   s     r<   rY   rY   j       GJsr?   r/   c                    d S r9   r:   rX   s     r<   rY   rY   n   r[   r?   )series_onlyIntoDataFrameT | IntoSeriesTLiteral[True]
eager_onlyr]   allow_series/DataFrame[IntoDataFrameT] | Series[IntoSeriesT]c                   d S r9   r:   rU   r3   r`   r]   ra   s        r<   rY   rY   r   s	     7:cr?   )r`   r]   ra   Nonec                   d S r9   r:   rd   s        r<   rY   rY   }   	     !$r?   c                   d S r9   r:   rd   s        r<   rY   rY      	     	r?   )r]   ra   c                   d S r9   r:   rd   s        r<   rY   rY      rg   r?   c                   d S r9   r:   rd   s        r<   rY   rY      ri   r?   )r`   r]   -IntoDataFrameT | IntoLazyFrameT | IntoSeriesTc                   d S r9   r:   rd   s        r<   rY   rY      s     SVRUr?   )r`   ra   c                   d S r9   r:   rd   s        r<   rY   rY      	     #r?   r3   r`   r]   ra   c                   d S r9   r:   rd   s        r<   rY   rY      rg   r?   c                   d S r9   r:   rd   s        r<   rY   rY      rg   r?   )r3   r]   ra   c                   d S r9   r:   rd   s        r<   rY   rY      rg   r?   )r3   r`   r]   IntoFrame | IntoSeries-DataFrame[Any] | LazyFrame[Any] | Series[Any]c                   d S r9   r:   rd   s        r<   rY   rY      s	     58Cr?   )r3   r`   ra   c                   d S r9   r:   rd   s        r<   rY   rY      ro   r?   bool | Nonec                   d S r9   r:   rd   s        r<   rY   rY      s	     #r?   JIntoLazyFrameT | IntoDataFrameT | IntoSeriesT | IntoFrame | IntoSeries | TOLazyFrame[IntoLazyFrameT] | DataFrame[IntoDataFrameT] | Series[IntoSeriesT] | Tc          	         |r.dt          t          |                    }t          |          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.
    z1from_native() got an unexpected keyword argument F)r3   r`   eager_or_interchange_onlyr]   ra   version)nextiterrS   _from_native_implr   MAIN)rU   r3   r`   r]   ra   rV   rT   s          r<   rY   rY     sb    X  V$tDzzBRBRVVnn!"'!   r?   )r3   r`   r}   r]   ra   r}   r~   r   c                  ddl m} ddlm}m}	m}
 ddlm}m} ddl	m
} t          | ||f          r|s| S t          | |          r|s|r| S |r|du rd}t          |          d}|r|rd	}t          |           ||           rS|r|sd
}t          |          | S |                    |                                                     |          d          S  |	|           rl|r|sd}t          |          | S |s|r|sd}t          |          | S |                    |                                                     |          d          S  |
|           rS|s|sd}t          |          | S |                    |                                                     |          d          S t+          |           r|r9t-          |           s*|s&dt/          |           j         }t          |          | S |s|r$t3          |           r|sd}t          |          | S |s$t-          |           r|sd}t          |          | S |j                            |           j                            |                                           S t?          |           rtA          |           r-|r*|s&dt/          |           j         }t          |          | S n|s|sd}t          |          | S |j                            |           j                            |                                           S tC          |           rtE          |           r-|r*|s&dt/          |           j         }t          |          | S n|s|sd}t          |          | S |j                            |           j                            |                                           S tG          |           r|r|sd}t          |          | S |s|r|sd}t          |          | S tH          j%        &                                dk    rtO                      d}tQ          |          |j        )                    tH          j%                  j                            |                                           S tU          |           r]|s|r|sd}t          |          | S |j                            |           j                            |                                           S tW          |           r]|s|r|sd}t          |          | S |j                            |           j                            |                                           S tY          |           rj|j                            |           }|s|s|r|sd|j-         d}t          |          | S |j                            |                                           S  ||           rlddl m.} |s|r|sd}t          |          | S |t^          j0        ur|r| S d}t          |          t^          j0                             ||           d          S |s!dt/          |            }t          |          | S )Nr   )supports_dataframe_interchange)is_compliant_dataframeis_compliant_lazyframeis_compliant_seriesr#   r&   FzJInvalid parameter combination: `series_only=True` and `allow_series=False`TzUInvalid parameter combination: `eager_only=True` and `eager_or_interchange_only=True`z,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 lazyframez4Please set `allow_series=True` or `series_only=True`z#Cannot only use `series_only` with zQCannot only use `eager_only` or `eager_or_interchange_only` with polars.LazyFramez1Cannot 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__a+  The Dataframe Interchange Protocol is no longer supported in the main `narwhals` namespace.

You may want to:
 - Use `narwhals.stable.v1`, where it is still supported.
    - See https://narwhals-dev.github.io/narwhals/backcompat
 - Use `pass_through=True` to pass the object through without raising.interchangezLExpected pandas-like dataframe, Polars dataframe, or Polars lazyframe, got: )1narwhals._interchange.dataframer   narwhals._utilsr   r   r   rK   r$   r%   rL   r'   rM   
ValueErrorrS   	dataframe__narwhals_dataframe___with_version	lazyframe__narwhals_lazyframe__series__narwhals_series__r   r    rR   __qualname__r   	namespacefrom_native_object	compliantrY   to_narwhalsr   r   r   r"   r   r   DASK_backend_versionr   ImportErrorfrom_backendr   r   r   implementationr   r   V1)rU   r3   r`   r}   r]   ra   r~   r   r   r   r   r$   r%   r'   rT   ns_sparkr   s                    r<   r   r   =  sc    ONNNNN         
 87777777&&&&&& -)Y!788  -(( k \  5  ^CS//! / eoo m,, 
 	! %Dnn$    0022@@IIQW ! 
 
 	
 m,, 
 	! %Dnn$   	!2 	! %bnn$    0022@@IIQW ! 
 
 	
 =)) 
 	! %Lnn$  ~~--//==gFFf  
 
 	

 && 
 	!/>> 	! %^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 &%m44 ZDDDDDD 	! 	! %F   nn$  '*$$ %$$Y  C.. z##$4$4]$C$C=#YYY r]abo]p]prrnnr?   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 r:   ) _get_native_namespace_single_obj).0xs     r<   	<setcomp>z'get_native_namespace.<locals>.<setcomp>8  s!    ???a.q11???r?   r   z0Found objects with different native namespaces: rJ   )r   lenpop)r   rT   results      r<   get_native_namespacer     sk    ,  Moo??3???F
6{{aJJJJoo::<<r?   c                    t          |           r|                                 S t          j        j                            |           j                                        S r9   )r   __native_namespace__r   r   r   r   r   to_native_namespace)r   s    r<   r   r   ?  sU     C   *'')))<!44 ((**+r?   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   r7   c                H     t                     d fd            }|S )Nargsr   kwargsr7   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 rp   rY   )r   argra   r`   r3   r]   s     r<   
<listcomp>zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>  sI     	 	 	  !-) +!-  	 	 	r?   c           
     @    i | ]\  }}|t          |           S r   r   )r   namevaluera   r`   r3   r]   s      r<   
<dictcomp>zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<dictcomp>  sO     	 	 	  D% k!-) +!-  	 	 	r?   c                J    h | ]}t          |d d          x              S )r   N)getattr)r   vbs     r<   r   zAnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<setcomp>  sG        $:DAAAA  r?   r   z_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r2   )itemsvaluesr   r   r=   )r   r   backendsrT   r   r   ra   r`   r   r3   r]   s        @r<   wrapperz.narwhalify.<locals>.decorator.<locals>.wrapper  s   	 	 	 	 	 	 	  	 	 	D	 	 	 	 	 	 	 $*<<>>	 	 	F   242&--//22  H 8}}q  w oo%T4*6**FV,????r?   )r   r   r   r   r7   r   r   )r   r   ra   r`   r3   r]   s   ` r<   	decoratorznarwhalify.<locals>.decorator  sX    	t#	@ #	@ #	@ #	@ #	@ #	@ #	@ #	@ #	@ 
#	@J r?   N)r   r   r7   r   r:   )r   r3   r`   r]   ra   r   s    ```` r<   
narwhalifyr   I  sP    p' ' ' ' ' ' ' ' 'R |9T??r?   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   rM   NON_TEMPORAL_SCALAR_TYPESr   
datetime64dtypeitemr   r   dt	timedeltar   r   	Timestampto_pydatetime	Timedeltato_pytimedeltaTEMPORAL_SCALAR_TYPES_is_pandas_nar!   as_pyrR   r   )r   pdscalarnpmsrT   s         r<   to_py_scalarr     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Mr?   c                    t          t                      x}o3|j        j                            |           o|                    |                     S r9   )rD   r   apitypes	is_scalarisna)r   r   s     r<   r   r     s@    z||#U)?)?)D)DUQTVVVr?   )r   r   r=   r   )r4   r5   r3   r6   r7   r*   )r4   r@   r3   r6   r7   r,   )r4   rB   r3   r6   r7   r.   )r4   r   r3   rD   r7   r   )r4   rF   r3   rD   r7   rG   )rU   r0   rV   r   r7   r0   )rU   r(   rV   r   r7   r(   )rU   r/   rV   r   r7   r/   )rU   r^   r3   r_   r`   r_   r]   r6   ra   r_   r7   rb   )rU   r*   r3   r_   r`   r6   r]   r6   ra   re   r7   r5   )rU   r1   r3   r_   r`   r6   r]   r6   ra   re   r7   r1   )rU   r*   r3   r_   r`   r_   r]   r6   ra   re   r7   r5   )rU   r1   r3   r_   r`   r_   r]   r6   ra   re   r7   r1   )rU   rl   r3   r_   r`   r6   r]   r6   ra   r_   r7   rF   )rU   r.   r3   r_   r`   r6   r]   r_   ra   re   r7   rB   )rU   r,   r3   r6   r`   r6   r]   r6   ra   re   r7   r@   )rU   r*   r3   r6   r`   r6   r]   r6   ra   re   r7   r5   )rU   r*   r3   r6   r`   r_   r]   r6   ra   re   r7   r5   )rU   rt   r3   r6   r`   r6   r]   r6   ra   r_   r7   ru   )rU   r.   r3   r6   r`   r6   r]   r_   ra   re   r7   rB   )rU   r   r3   rD   r`   rD   r]   rD   ra   rx   r7   r   )rU   rz   r3   rD   r`   rD   r]   rD   ra   rx   rV   r   r7   r{   )rU   r   r3   rD   r`   rD   r}   rD   r]   rD   ra   rx   r~   r   r7   r   )r   r   r7   r   r9   )r   r   r3   rD   r`   rD   r]   rD   ra   rx   r7   r   )r   r   r7   r   )r   r   r7   rD   )M
__future__r   datetimer   decimalr   	functoolsr   typingr   r   r	   r
   r   r   narwhals._constantsr   r   narwhals._nativer   r   r   r   r   r   r   r   narwhals.dependenciesr   r   r   r   r   r   r   r   r   r   r    r!   r"   rK   r$   r%   rL   r'   narwhals.typingr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   rD   bytesstrintfloatcomplexr   dater   timer   r=   rY   r   r   r   r   r   r   __all__r:   r?   r<   <module>r      s   " " " " " "                 K K K K K K K K K K K K K K K K 4 4 4 4 4 4 4 4            J I I I I I I I I I                                77777777&&&&&&
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 GCLL!5#sE7GL ",8  
RU     
 
RU     
 
LO     
 
 F F F 
 F      B 
 D D D 
 D 
 J J J 
 J 
 J J J 
 J 
 #&: : : : : 
: 

 "%"%$ $ $ $ $ 
$ 

 "%"%     
 
 #&$ $ $ $ $ 
$ 
 #&     
 

 "%"%V V V V V 
V 

 "%     
 
 $'!$"%$ $ $ $ $ 
$ 
 $'!$"%$ $ $ $ $ 
$ 
 $'"%$ $ $ $ $ 
$ 
 $'!$"%8 8 8 8 8 
8 
 $'!$     
 
   
$  $8 8 8 8 8 8| &+ $_ _ _ _ _ _D   @+ + + + '+d  $d d d d d dN5 5 5 5pW W W W N
M
Mr?   