
    /_im^                    x   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j                  ej                  ej                  fZQedd	 	 	 	 	 d1d       ZRedd	 	 	 	 	 d2d       ZRedd	 	 	 	 	 d3d       ZRed4d       ZRdd	 	 	 	 	 d5dZRed6d       ZSed7d       ZSe	 	 	 	 	 	 d8d       ZSed9d       ZSe	 	 	 	 	 	 d:d       ZSe	 	 	 	 	 	 d;d       ZSe	 	 	 	 	 	 d<d       ZSe	 	 	 	 	 	 d=d        ZSe	 	 	 	 	 	 d>d!       ZSe	 	 	 	 	 	 d?d"       ZSed@d#       ZSe	 	 	 	 	 	 	 	 	 	 	 	 dAd$       ZSddddd%	 	 	 	 	 	 	 	 	 	 	 dBd&ZSddd'	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dCd(ZTddd'	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dDd)ZUdEd*ZV	 	 	 	 dEd+ZW	 dFd,ddd,d%	 	 	 	 	 	 	 	 	 	 	 dGd-ZXdHd.ZYdId/ZZg d0Z[y)J    )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_throughc                    y N narwhals_objectr>   s     Q/var/www/html/land_sniper/venv/lib/python3.12/site-packages/narwhals/translate.py	to_nativerE   G            c                    y r@   rA   rB   s     rD   rE   rE   K   rF   rG   c                    y r@   rA   rB   s     rD   rE   rE   O   s     rG   c                    y r@   rA   rB   s     rD   rE   rE   S   s    CFrG   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   )	BaseFramer1   zExpected Narwhals object, got .)narwhals.dataframerL   narwhals.seriesr2   
isinstance_compliant_frame_native_frame_compliant_seriesnativetype	TypeError)rC   r>   rL   r2   msgs        rD   rE   rE   W   si    & -&/9-//===/6*00777.tO/D.EQGnrG   c                     y r@   rA   native_objectkwdss     rD   from_nativer\   x   s    PSrG   c                     y r@   rA   rY   s     rD   r\   r\   z   s    QTrG   c                     y r@   rA   rY   s     rD   r\   r\   |   s     rG   c                     y r@   rA   rY   s     rD   r\   r\      s    UXrG   c                     y r@   rA   rY   s     rD   r\   r\           !$rG   c                     y r@   rA   rY   s     rD   r\   r\           rG   c                     y r@   rA   rY   s     rD   r\   r\      rc   rG   c                     y r@   rA   rY   s     rD   r\   r\      ra   rG   c                     y r@   rA   rY   s     rD   r\   r\      s     7:rG   c                     y r@   rA   rY   s     rD   r\   r\      s	     SVrG   c                     y r@   rA   rY   s     rD   r\   r\      s    LOrG   c                    y r@   rA   rZ   r>   
eager_onlyseries_onlyallow_seriess        rD   r\   r\      s     rG   r>   rk   rl   rm   c          	     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>   rk   eager_or_interchange_onlyrl   rm   version)_from_native_implr   MAINrj   s        rD   r\   r\      s+    V !"'! rG   )r>   rk   c                  t        |       rC|r|sd}t        |      | S |j                  | j                         j	                  |      d      S t        |       rX|r|sd}t        |      | S |s|r|sd}t        |      | S |j                  | j                         j	                  |      d      S t        |       rC|s|sd}t        |      | S |j                  | j                         j	                  |      d      S y )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   rV   	dataframe__narwhals_dataframe___with_versionr   	lazyframe__narwhals_lazyframe__r   series__narwhals_series__)compliant_objectr>   rk   rq   rl   rm   rr   rW   s           rD   _translate_if_compliantr      s+    ./Dn$##  335CCGLTZ ! 
 	
 ./Dn$##2bn$##  335CCGLTZ ! 
 	
 +,Ln$##~~002@@IQW  
 	
 rG   c          	     X
   ddl 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        |      t        | ||||||	      x}	 |S t        |       r|r2t        |       s'|s#d
t        |       j                   }t        |      | S |s|rt        |       r|sd}t        |      | S |st        |       r|sd}t        |      | S |j                   j#                  |       j$                  j'                  |       j)                         S t+        |       rt-        |       r)|r:|s#d
t        |       j                   }t        |      | S |s|sd}t        |      | S |j                   j#                  |       j$                  j'                  |       j)                         S t/        |       rt1        |       r)|r:|s#d
t        |       j                   }t        |      | S |s|sd}t        |      | S |j                   j#                  |       j$                  j'                  |       j)                         S t3        |       r|r|sd}t        |      | S |s|r|sd}t        |      | S t4        j6                  j9                         dk  rt;               d}t=        |      |j                   j?                  t4        j6                        j$                  j'                  |       j)                         S tA        |       rW|s|r|sd}t        |      | S |j                   j#                  |       j$                  j'                  |       j)                         S tC        |       rW|s|r|sd}t        |      | S |j                   j#                  |       j$                  j'                  |       j)                         S tE        |       ri|j                   j#                  |       }|s|s|r|sd|jF                   d}t        |      | S |j$                  j'                  |       j)                         S  ||       reddl m$} |s|r|sd}t        |      | S |tJ        jL                  ur|r| S d}t        |      tJ        jL                  jO                   ||       d      S tQ        j&                  | |      }|t        |||||||	      S |sAdt        |        }tQ        jR                  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`rp   z#Cannot only use `series_only` with zQCannot only use `eager_only` or `eager_or_interchange_only` with polars.LazyFramery   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__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.interchangerw   z!Unsupported dataframe type, got: z

)*narwhals._interchange.dataframer   rN   r/   r0   rO   r2   rP   
ValueErrorr   r   r$   rU   __qualname__rV   r#   	namespacefrom_native_object	compliantr\   to_narwhalsr   r"   r   r&   r   r   DASK_backend_versionr   ImportErrorfrom_backendr   r    r   implementationr   r   V1rz   r   _show_suggestions)rZ   r>   rk   rq   rl   rm   rr   r   r/   r0   r2   rW   
translatedns_sparkr   r   hints                    rD   rs   rs     sI    O7& -)Y!78-(k\5 ^CS/!/eo .%!&?#%
 	

 
  &/>;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**='B#&%!&?#%
 	
 1$}2E1FG,,T--@AA4A6D= CnrG   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`.r   z0Found objects with different native namespaces: rM   )r    _get_native_namespace_single_objlenpop)objrW   xresults       rD   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 r@   )r   __native_namespace__r   rt   r   r   r   to_native_namespace)r   s    rD   r   r     sI     C ''))<<!!44n((*+rG   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 )Nrn   r   r   z_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r=   )r\   itemsvaluesgetattrr   r   rE   )argskwargsargnamevaluevbbackendsrW   r   rm   rk   funcr>   rl   s             rD   wrapperz.narwhalify.<locals>.decorator.<locals>.wrapperF  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   rm   rk   r>   rl   s   ` rD   	decoratorznarwhalify.<locals>.decoratorE  s)    	t#	@ #	@ 
#	@J rG   )r   Callable[..., Any]r   r   rA   )r   r>   rk   rl   rm   r   s    ```` rD   
narwhalifyr     s&    p' 'R |T?rG   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-        |      )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   rP   NON_TEMPORAL_SCALAR_TYPESr   
datetime64dtypeitemr   r   dt	timedeltar!   r   	Timestampto_pydatetime	Timedeltato_pytimedeltaTEMPORAL_SCALAR_TYPES_is_pandas_nar%   as_pyrU   r   )scalar_likepdscalarnpmsrW   s         rD   to_py_scalarr   t  so   . 
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rG   c                    t        t               x}xr8 |j                  j                  j	                  |       xr |j                  |             S r@   )boolr   apitypes	is_scalarisna)r   r   s     rD   r   r     s:    z|#U)?)?)DUQTVVrG   )r   r   rE   r   )rC   DataFrame[IntoDataFrameT]r>   Literal[False]r   r5   )rC   LazyFrame[IntoLazyFrameT]r>   r   r   r7   )rC   Series[IntoSeriesT]r>   r   r   r9   )rC   r   r>   r   r   r   )rC   KDataFrame[IntoDataFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT]r>   r   r   z3IntoDataFrameT | IntoLazyFrameT | IntoSeriesT | Any)rZ   r;   r[   Unpack[OnlySeries]r   r;   )rZ   r;   r[   Unpack[AllowSeries]r   r;   )rZ   r3   r[   Unpack[ExcludeSeries]r   r3   )rZ   r:   r[   Unpack[AllowLazy]r   r:   )rZ   r5   r[   r   r   r   )rZ   r9   r[   r   r   r   )rZ   r9   r[   r   r   r   )rZ   r7   r[   r   r   r   )rZ   zIntoDataFrameT | IntoSeriesTr[   r   r   z/DataFrame[IntoDataFrameT] | Series[IntoSeriesT])rZ   z-IntoDataFrameT | IntoLazyFrameT | IntoSeriesTr[   zUnpack[AllowAny]r   r   )rZ   r<   r[   zUnpack[PassThroughUnknown]r   r<   )rZ   r   r>   r   rk   r   rl   r   rm   bool | Noner   r   )rZ   zJIntoLazyFrameT | IntoDataFrameT | IntoSeriesT | IntoFrame | IntoSeries | Tr>   r   rk   r   rl   r   rm   r   r   zOLazyFrame[IntoLazyFrameT] | DataFrame[IntoDataFrameT] | Series[IntoSeriesT] | T)r   r   r>   r   rk   r   rq   r   rl   r   rm   r   rr   r   r   r   )rZ   r   r>   r   rk   r   rq   r   rl   r   rm   r   rr   r   r   r   )r   z,Frame | Series[Any] | IntoFrame | IntoSeriesr   r   r@   )r   zCallable[..., Any] | Noner>   r   rk   r   rl   r   rm   r   r   r   )r   r   r   r   )r   r   r   r   )\
__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-   rN   r/   r0   rO   r2   narwhals.typingr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r   bytesstrintfloatcomplexr   dater   timer   rE   r\   r   rs   r   r   r   r   r   __all__rA   rG   rD   <module>r      s   "    K K  4       (  8&
 
 
 CL!5#sE7GL ",,8  
RU.AO 
 
RU.AO 
 
LO(;I 
 
 F 
 F 
  9B 
 S 
 S	 T 
 T	'< 
 
 X 
 X	$!$+@$$ 
$ 
(: 
 
(; 
 
$!$+<$$ 
$ 
:/:9L:4: 
: 
V@VJZVPV 
V 
 O 
 O	  	
   	 
   $33 3 3 3 3 U3r 	,, , 	,  $, , , , 	,d 	TT T 	T  $T T T T 	Tn@+	5++ '+d  $d
#d d 	d
 d d dN5pW NrG   