
    Wh`                    |   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j                  ej                  fZCedd	 	 	 	 	 d9d       ZDedd	 	 	 	 	 d:d       ZDedd	 	 	 	 	 d;d       ZDed<d       ZDdd	 	 	 	 	 d=dZDed>d       ZEed?d       ZEed@d       ZEedd	 	 	 	 	 	 	 	 	 	 	 dAd       ZEedddd	 	 	 	 	 	 	 	 	 	 	 dBd       ZEedddd	 	 	 	 	 	 	 	 	 	 	 dCd       ZEeddd	 	 	 	 	 	 	 	 	 	 	 dDd       ZEeddd	 	 	 	 	 	 	 	 	 	 	 dEd        ZEeddd!	 	 	 	 	 	 	 	 	 	 	 dFd"       ZEeddd#	 	 	 	 	 	 	 	 	 	 	 dGd$       ZEeddddd%	 	 	 	 	 	 	 	 	 	 	 dHd&       ZEeddddd%	 	 	 	 	 	 	 	 	 	 	 dId'       ZEedddd(	 	 	 	 	 	 	 	 	 	 	 dJd)       ZEedddd*	 	 	 	 	 	 	 	 	 	 	 dKd+       ZEedddd,	 	 	 	 	 	 	 	 	 	 	 dLd-       ZEe	 	 	 	 	 	 	 	 	 	 	 	 dMd.       ZEddddd%	 	 	 	 	 	 	 	 	 	 	 	 	 dNd/ZEdddddd0	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dOd1ZFdPd2ZG	 	 	 	 dPd3ZH	 dQd4ddd4d%	 	 	 	 	 	 	 	 	 	 	 dRd5ZIdSd6ZJdTd7ZKg d8ZLy)U    )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_throughc                    y N narwhals_objectr3   s     L/var/www/html/jupyter_env/lib/python3.12/site-packages/narwhals/translate.py	to_nativer:   5            c                    y r5   r6   r7   s     r9   r:   r:   9   r;   r<   c                    y r5   r6   r7   s     r9   r:   r:   =   s     r<   c                    y r5   r6   r7   s     r9   r:   r:   A   s    CFr<   Fc                   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.dataframerA   narwhals.seriesr'   
isinstance_compliant_frame_native_frame_compliant_seriesnativetype	TypeError)r8   r3   rA   r'   msgs        r9   r:   r:   E   si    & -&/9-//===/6*00777.tO/D.EQGnr<   c                     y r5   r6   native_objectkwdss     r9   from_nativerQ   f   s    ADr<   c                     y r5   r6   rN   s     r9   rQ   rQ   j       GJr<   c                     y r5   r6   rN   s     r9   rQ   rQ   n   rS   r<   )series_onlyc                    y r5   r6   rO   r3   
eager_onlyrU   allow_seriess        r9   rQ   rQ   r   s     7:r<   )rX   rU   rY   c                    y r5   r6   rW   s        r9   rQ   rQ   }        !$r<   c                    y r5   r6   rW   s        r9   rQ   rQ           	r<   )rU   rY   c                    y r5   r6   rW   s        r9   rQ   rQ      r[   r<   c                    y r5   r6   rW   s        r9   rQ   rQ      r]   r<   )rX   rU   c                    y r5   r6   rW   s        r9   rQ   rQ      s	     SVr<   )rX   rY   c                    y r5   r6   rW   s        r9   rQ   rQ           r<   r3   rX   rU   rY   c                    y r5   r6   rW   s        r9   rQ   rQ      r[   r<   c                    y r5   r6   rW   s        r9   rQ   rQ      r[   r<   )r3   rU   rY   c                    y r5   r6   rW   s        r9   rQ   rQ      r[   r<   )r3   rX   rU   c                    y r5   r6   rW   s        r9   rQ   rQ      s     58r<   )r3   rX   rY   c                    y r5   r6   rW   s        r9   rQ   rQ      rb   r<   c                    y r5   r6   rW   s        r9   rQ   rQ      s     r<   c          	         |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   rX   eager_or_interchange_onlyrU   rY   version)nextiterrK   _from_native_implr   MAIN)rO   r3   rX   rU   rY   rP   rL   s          r9   rQ   rQ     sN    X A$tDzBRAUVn!"'! r<   )r3   rX   rk   rU   rY   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        |       ||       rC|r|sd
}t        |      | S |j                  | j                         j!                  |      d      S  |	|       rX|r|sd}t        |      | S |s|r|sd}t        |      | S |j#                  | j%                         j!                  |      d      S  |
|       rC|s|sd}t        |      | S |j'                  | j)                         j!                  |      d      S t+        |       r|r2t-        |       s'|s#dt/        |       j0                   }t        |      | S |s|rt3        |       r|sd}t        |      | S |st-        |       r|sd}t        |      | S |j4                  j7                  |       j8                  j;                  |       j=                         S t?        |       rtA        |       r)|r:|s#dt/        |       j0                   }t        |      | S |s|sd}t        |      | S |j4                  j7                  |       j8                  j;                  |       j=                         S tC        |       rtE        |       r)|r:|s#dt/        |       j0                   }t        |      | S |s|sd}t        |      | S |j4                  j7                  |       j8                  j;                  |       j=                         S tG        |       r|r|sd}t        |      | S |s|r|sd}t        |      | S tH        jJ                  jM                         dk  rtO               d}tQ        |      |j4                  jS                  tH        jJ                        j8                  j;                  |       j=                         S tU        |       rW|s|r|sd}t        |      | S |j4                  j7                  |       j8                  j;                  |       j=                         S tW        |       rW|s|r|sd}t        |      | S |j4                  j7                  |       j8                  j;                  |       j=                         S tY        |       ri|j4                  j7                  |       }|s|s|r|sd|jZ                   d}t        |      | S |j8                  j;                  |       j=                         S  ||       reddl m.} |s|r|sd}t        |      | S |t^        j`                  ur|r| S d}t        |      t^        j`                  j                   ||       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.dataframerr   narwhals._utilsrs   rt   ru   rC   r$   r%   rD   r'   rE   
ValueErrorrK   	dataframe__narwhals_dataframe___with_version	lazyframe__narwhals_lazyframe__series__narwhals_series__r   r    rJ   __qualname__r   	namespacefrom_native_object	compliantrQ   to_narwhalsr   r   r   r"   r   r   DASK_backend_versionr   ImportErrorfrom_backendr   r   r   implementationrz   r   V1)rO   r3   rX   rk   rU   rY   rl   rr   rs   rt   ru   r$   r%   r'   rL   ns_sparkrz   s                    r9   ro   ro   =  s    O 
 8& -)Y!78-(k\5 ^CS/!/eo m,Dn$    002@@IQW ! 
 	
 m,Dn$  2bn$    002@@IQW ! 
 	
 =)Ln$  ~~--/==gFf  
 	

 &/>;D<O<\<\;]^n$  39L:
  in$  "2="ALn$  00?Y{{=1[]	
 ]+#M2#?]@S@`@`?abC#C.($$Ln$  00?Y{{=1[]	
 }%M*#?]@S@`@`?abC#C.($$Ln$  00?Y{{=1[]	
 'In$  2gn$  002mC',Cc""**>+>+>?Y{{=1[]	
 -(fn$  00?Y{{=1[]	
 ]#`n$  00?Y{{=1[]	
 M*$$77F*(A$334J@   n$  !!--m<HHJJ &m4DF   n$  '**$$$Y  C. zz##$4]$C=#YY\]abo]p\qrnr<   c                     | sd}t        |      | D ch c]  }t        |       }}t        |      dk7  rd| d}t        |      |j                         S c c}w )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`.ry   z0Found objects with different native namespaces: rB   )r~    _get_native_namespace_single_objlenpop)objrL   xresults       r9   get_native_namespacer     se    , Mo;>?a.q1?F?
6{a@Jo::<	 @s   Ac                    t        |       r| j                         S t        j                  j                  j                  |       j                  j                         S r5   )r   __native_namespace__r   rp   r   r   r   to_native_namespace)r   s    r9   r   r   ?  sI     C ''))<<!!44n((*+r<   Tc               4    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())
    c                :     t               d fd       }|S )Nc                    | D cg c]  }t        |
       } }|j                         D ci c]  \  }}|t        |
       }}}g | |j                         D ch c]  }t        |dd       x}r |        }}t	        |      dkD  rd}t        |       | i |}	t        |	      S c c}w c c}}w c c}w )Nrc   r   ry   z_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r2   )rQ   itemsvaluesgetattrr   r~   r:   )argskwargsargnamevaluevbbackendsrL   r   rY   rX   funcr3   rU   s             r9   wrapperz.narwhalify.<locals>.decorator.<locals>.wrapper  s     	  !-) +!-	D 	& $*<<>	  D% k!-) +!- 	F 	 342&--/2 $:DAAAA H  8}q w o%4*6*FV,??E		s   B6B;'C)r   r   r   r   returnr   r   )r   r   rY   rX   r3   rU   s   ` r9   	decoratorznarwhalify.<locals>.decorator  s)    	t#	@ #	@ 
#	@J r<   )r   Callable[..., Any]r   r   r6   )r   r3   rX   rU   rY   r   s    ```` r9   
narwhalifyr   I  s&    p' 'R |T?r<   c                   t               }| t        | t              r| }|S t               x}r[t        | |j                        rE| j
                  dk(  r6| j                         t        z  }t        t        j                  |      z   }|S t        |       st        |       r| j                         }|S |r(t        | |j                        r| j                         }|S |r(t        | |j                        r| j!                         }|S t        | t"              r| }|S t%        |       rd}|S t'        |       r| j)                         }|S dt+        |        d| }t-        |      )au  If a scalar is not Python native, converts it to Python native.

    Arguments:
        scalar_like: Scalar-like value.

    Returns:
        Python scalar.

    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   rE   NON_TEMPORAL_SCALAR_TYPESr   
datetime64dtypeitemr   r   dt	timedeltar   r   	Timestampto_pydatetime	Timedeltato_pytimedeltaTEMPORAL_SCALAR_TYPES_is_pandas_nar!   as_pyrJ   r~   )scalar_likepdscalarnpmsrL   s         r9   to_py_scalarr     so   4 
Bj6OP8 M5 {		{BMM2!11=0266* M) 
	%)D!!#& M% 

;5**," M! 

;5++- M 
K!6	7 M 
{	# M 
;	'""$ M	 >d;>O=PPSo 	 or<   c                    t        t               x}xr8 |j                  j                  j	                  |       xr |j                  |             S r5   )boolr   apitypes	is_scalarisna)r   r   s     r9   r   r     s:    z|#U)?)?)DUQTVVr<   )r   r   r:   r   )r8   DataFrame[IntoDataFrameT]r3   Literal[False]r   r*   )r8   LazyFrame[IntoLazyFrameT]r3   r   r   r,   )r8   Series[IntoSeriesT]r3   r   r   r.   )r8   r   r3   r   r   r   )r8   KDataFrame[IntoDataFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT]r3   r   r   z3IntoDataFrameT | IntoLazyFrameT | IntoSeriesT | Any)rO   r0   rP   r   r   r0   )rO   r(   rP   r   r   r(   )rO   r/   rP   r   r   r/   )rO   zIntoDataFrameT | IntoSeriesTr3   Literal[True]rX   r   rU   r   rY   r   r   z/DataFrame[IntoDataFrameT] | Series[IntoSeriesT])rO   r*   r3   r   rX   r   rU   r   rY   Noner   r   )rO   r1   r3   r   rX   r   rU   r   rY   r   r   r1   )rO   r*   r3   r   rX   r   rU   r   rY   r   r   r   )rO   r1   r3   r   rX   r   rU   r   rY   r   r   r1   )rO   z-IntoDataFrameT | IntoLazyFrameT | IntoSeriesTr3   r   rX   r   rU   r   rY   r   r   r   )rO   r.   r3   r   rX   r   rU   r   rY   r   r   r   )rO   r,   r3   r   rX   r   rU   r   rY   r   r   r   )rO   r*   r3   r   rX   r   rU   r   rY   r   r   r   )rO   r*   r3   r   rX   r   rU   r   rY   r   r   r   )rO   zIntoFrame | IntoSeriesr3   r   rX   r   rU   r   rY   r   r   z-DataFrame[Any] | LazyFrame[Any] | Series[Any])rO   r.   r3   r   rX   r   rU   r   rY   r   r   r   )rO   r   r3   r   rX   r   rU   r   rY   bool | Noner   r   )rO   zJIntoLazyFrameT | IntoDataFrameT | IntoSeriesT | IntoFrame | IntoSeries | Tr3   r   rX   r   rU   r   rY   r   rP   r   r   zOLazyFrame[IntoLazyFrameT] | DataFrame[IntoDataFrameT] | Series[IntoSeriesT] | T)rO   r   r3   r   rX   r   rk   r   rU   r   rY   r   rl   r   r   r   )r   z,Frame | Series[Any] | IntoFrame | IntoSeriesr   r   r5   )r   zCallable[..., Any] | Noner3   r   rX   r   rU   r   rY   r   r   r   )r   r   r   r   )r   r   r   r   )M
__future__r   datetimer   decimalr   	functoolsr   typingr   r   r	   r
   r   r   narwhals._constantsr   r   narwhals._namespacer   r   r   r   r}   r   r   r   narwhals.dependenciesr   r   r   r   r   r   r   r   r   r   r    r!   r"   rC   r$   r%   rD   r'   narwhals.typingr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   bytesstrintfloatcomplexr   dater   timer   r:   rQ   ro   r   r   r   r   r   __all__r6   r<   r9   <module>r      sY   "    K K 4  J I     7&
 
 
 CL!5#sE7GL ",,8  
RU.AO 
 
RU.AO 
 
LO(;I 
 
 F 
 F 
  9B 
 D 
 D 
 J 
 J 
 J 
 J 
 #&:/:  : 	:
  :  : 5: 
: 

 "%"%$!$  $ 	$
  $ $ $ 
$ 

 "%"%   	
     
 
 #&$!$  $ 	$
  $ $ $ 
$ 
 #&   	
     
 

 "%"%V@V  V 	V
  V  V QV 
V 

 "%   	
    
 
 $'!$"%$!$ !$ 	$
  $ $ $ 
$ 
 $'!$"%$!$ !$ 	$
  $ $ $ 
$ 
 $'"%$!$ !$ 	$
  $ $ $ 
$ 
 $'!$"%8)8 !8 	8
  8  8 38 
8 
 $'!$ ! 	
    
 
  	
   	 
$  $88 8 8 8 8 8 U8| &+ $__ _ 	_  $_ _ _ _ 	_D@+	5++ '+d  $d
#d d 	d
 d d dN8vW Nr<   