
    bZhk                       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mZmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZm Z m!Z!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+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3  ed      Z4e5e6e7e8e9e:efZ;ejx                  ejz                  ej|                  fZ?edd	 	 	 	 	 d:d       Z@edd	 	 	 	 	 d;d       Z@edd	 	 	 	 	 d<d       Z@ed=d       Z@ddd	 	 	 	 	 	 	 d>dZ@ed?d       ZAed@d       ZAedAd       ZAedd	 	 	 	 	 	 	 	 	 	 	 dBd       ZAedddd	 	 	 	 	 	 	 	 	 	 	 dCd       ZAedddd	 	 	 	 	 	 	 	 	 	 	 dDd       ZAeddd	 	 	 	 	 	 	 	 	 	 	 dEd       ZAeddd	 	 	 	 	 	 	 	 	 	 	 dFd       ZAeddd 	 	 	 	 	 	 	 	 	 	 	 dGd!       ZAeddd"	 	 	 	 	 	 	 	 	 	 	 dHd#       ZAeddddd$	 	 	 	 	 	 	 	 	 	 	 dId%       ZAeddddd$	 	 	 	 	 	 	 	 	 	 	 dJd&       ZAedddd'	 	 	 	 	 	 	 	 	 	 	 dKd(       ZAedddd)	 	 	 	 	 	 	 	 	 	 	 dLd*       ZAedddd+	 	 	 	 	 	 	 	 	 	 	 dMd,       ZAe	 	 	 	 	 	 	 	 	 	 	 	 dNd-       ZAddd.d.dd/	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dOd0ZAd.d.d.d.dd1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dPd2ZB	 	 	 	 dQd3ZC	 	 	 	 dQd4ZD	 dRddd.d.d5d/	 	 	 	 	 	 	 	 	 	 	 	 	 dSd6ZEdTd7ZFdUd8ZGg d9ZHy)V    )annotationsN)Decimalwraps)TYPE_CHECKINGAnyCallableLiteralTypeVaroverload)is_native_arrowis_native_pandas_likeis_native_polarsis_native_spark_like)Version)get_dask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IntoDataFrameT	IntoFrame
IntoFrameTIntoLazyFrameT
IntoSeriesIntoSeriesT
LazyFrameTSeriesTT.pass_throughc                    y N narwhals_objectr0   s     a/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/narwhals/translate.py	to_nativer7   5   s         c                    y r2   r3   r4   s     r6   r7   r7   9   s     r8   c                    y r2   r3   r4   s     r6   r7   r7   =   s     r8   c                    y r2   r3   r4   s     r6   r7   r7   A   s    CFr8   )strictr0   c                  ddl m} ddlm} ddlm}  |||dd      }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.
        strict: Determine what happens if `narwhals_object` isn't a Narwhals class

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

            *Deprecated* (v1.13.0)

            Please use `pass_through` instead. Note that `strict` is still available
            (and won't emit a deprecation warning) if you use `narwhals.stable.v1`,
            see [perfect backwards compatibility policy](../backcompat.md/).
        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   validate_strict_and_pass_though)	BaseFramer#   FTpass_through_defaultemit_deprecation_warningzExpected Narwhals object, got .)narwhals._utilsr?   narwhals.dataframer@   narwhals.seriesr$   
isinstance_compliant_frame_native_frame_compliant_seriesnativetype	TypeError)r5   r<   r0   r?   r@   r$   msgs          r6   r7   r7   E   s    < @,&25SWL /9-//===/6*00777.tO/D.EQGnr8   c                     y r2   r3   native_objectkwdss     r6   from_nativerT   v   s    ADr8   c                     y r2   r3   rQ   s     r6   rT   rT   z       GJr8   c                     y r2   r3   rQ   s     r6   rT   rT   ~   rV   r8   )series_onlyc                    y r2   r3   rR   r0   
eager_onlyrX   allow_seriess        r6   rT   rT      s     7:r8   )r[   rX   r\   c                    y r2   r3   rZ   s        r6   rT   rT           !$r8   c                    y r2   r3   rZ   s        r6   rT   rT           	r8   )rX   r\   c                    y r2   r3   rZ   s        r6   rT   rT      r^   r8   c                    y r2   r3   rZ   s        r6   rT   rT      r`   r8   )r[   rX   c                    y r2   r3   rZ   s        r6   rT   rT      s	     ORr8   )r[   r\   c                    y r2   r3   rZ   s        r6   rT   rT           r8   r0   r[   rX   r\   c                    y r2   r3   rZ   s        r6   rT   rT      r^   r8   c                    y r2   r3   rZ   s        r6   rT   rT      r^   r8   )r0   rX   r\   c                    y r2   r3   rZ   s        r6   rT   rT      r^   r8   )r0   r[   rX   c                    y r2   r3   rZ   s        r6   rT   rT      s     58r8   )r0   r[   r\   c                    y r2   r3   rZ   s        r6   rT   rT      re   r8   c                    y r2   r3   rZ   s        r6   rT   rT   
  s     r8   F)r<   r0   r[   rX   r\   c          	         ddl m}  |||dd      }|r"dt        t        |            }t	        |      t        | ||d||t        j                        S )aq  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__`
        strict: Determine what happens if the object can't be converted to Narwhals

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

            *Deprecated* (v1.13.0)

            Please use `pass_through` instead. Note that `strict` is still available
            (and won't emit a deprecation warning) if you use `narwhals.stable.v1`,
            see [perfect backwards compatibility policy](../backcompat.md/).
        pass_through: Determine what happens if the object can't be converted to Narwhals

            - `False` or `None` (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.
    r   r>   FTrA   z1from_native() got an unexpected keyword argument )r0   r[   eager_or_interchange_onlyrX   r\   version)rE   r?   nextiterrN   _from_native_implr   MAIN)	rR   r<   r0   r[   rX   r\   rS   r?   rO   s	            r6   rT   rT     sh    d @25SWL A$tDzBRAUVn!"'! r8   )r0   r[   rn   rX   r\   c               $   ddl m}m}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        |       rddl$m%} |r|sd}t        |      | S |s|r|sd}t        |      | S  |tM                     }|dk  rtO               d}tQ        |       |||      j;                  |       j=                         S tS        |       rW|s|r|sd}t        |      | S |j4                  j7                  |       j8                  j;                  |       j=                         S tU        |       rW|s|r|sd}t        |      | S |j4                  j7                  |       j8                  j;                  |       j=                         S tW        |       ri|j4                  j7                  |       }|s|s|r|sd|jX                   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_seriesparse_versionr    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.LazyFrame)DaskNamespacez1Cannot only use `series_only` with dask DataFramezOCannot only use `eager_only` or `eager_or_interchange_only` with dask DataFrame)i        zPlease install dask-expr)backend_versionro   zNCannot 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: )1rE   ru   rv   rw   rx   ry   rF   r!   r"   rG   r$   rH   
ValueErrorrN   	dataframe__narwhals_dataframe___with_version	lazyframe__narwhals_lazyframe__series__narwhals_series__r   r   rM   __qualname__r   	namespacefrom_native_object	compliantrT   to_narwhalsr   r   r   r   r   narwhals._dask.namespacer|   r   r   ImportErrorr   r   r   implementationnarwhals._interchange.dataframer   r   V1)rR   r0   r[   rn   rX   r\   ro   ru   rv   rw   rx   ry   r!   r"   r$   rO   r|   dask_versionns_sparkr   s                       r6   rr   rr   [  s     8& -)Y!78-(k\5 ^CS/!/eo m,Dn$    002@@IQW ! 
 	
 
 	.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$  $XZ0=(]_-D,Cc"",H['[]	
 
M	*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 
)	7DF   n$  '**$$$Y  C. zz##$4]$C=#YY\]abo]p\qrnr8   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: rD   )r    _get_native_namespace_single_objlenpop)objrO   xresults       r6   get_native_namespacer   >  se    0 Mo;>?a.q1?F?
6{a@Jo::<	 @s   Ac                L   ddl m} ddlm}  |t        t
              5  t        j                  j                  j                  |       j                  j                         cd d d        S # 1 sw Y   nxY w ||       r| j                         S dt        |        }t	        |      )Nr   )suppress)has_native_namespacez4Could not get native namespace from object of type: )
contextlibr   rE   r   rN   AssertionErrorr   rs   r   r   r   to_native_namespace__native_namespace__rM   )r   r   r   rO   s       r6   r   r   `  s     $4	)^	, /||%%88

.,,./ / /
 C ''))@c
LC
C.s   AA))A2Tc               X    ddl m}  ||dd      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.
        strict: Determine what happens if the object can't be converted to Narwhals

            *Deprecated* (v1.13.0)

            Please use `pass_through` instead. Note that `strict` is still available
            (and won't emit a deprecation warning) if you use `narwhals.stable.v1`,
            see [perfect backwards compatibility policy](../backcompat.md/).

            - `True` or `None` (default): raise an error
            - `False`: pass object through as-is
        pass_through: Determine what happens if the object can't be converted to Narwhals

            - `False` or `None` (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`: 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>   TrA   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 )Nrf   r   r~   z_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r/   )rT   itemsvaluesgetattrr   r   r7   )argskwargsargnamevaluevbbackendsrO   r   r\   r[   funcr0   rX   s             r6   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   r\   r[   r0   rX   s   ` r6   	decoratorznarwhalify.<locals>.decorator  s)    	t#	@ #	@ 
#	@J r8   )r   Callable[..., Any]r   r   )rE   r?   )r   r<   r0   r[   rX   r\   r?   r   s     ````  r6   
narwhalifyr   r  sB    D @24RVL' 'R | r8   c                   t               }| t        | t              r| }|S t               x}rht        | |j                        rR| j
                  dk(  rC| j                         dz  }t        j                  ddd      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]i  i  r~   )microsecondsz/Expected object convertible to a scalar, found z.
)r   rH   NON_TEMPORAL_SCALAR_TYPESr   
datetime64dtypeitemdtdatetime	timedeltar   r   	Timestampto_pydatetime	Timedeltato_pytimedeltaTEMPORAL_SCALAR_TYPES_is_pandas_nar   as_pyrM   r   )scalar_likepdscalarnpmsrO   s         r6   to_py_scalarr     s|   4 
Bj6OP8 M5 {		{BMM2!114'T1a(2<<R+HH* M) 
	%)D!!#& M% 

;5**," M! 

;5++- M 
K!6	7 M 
{	# M 
;	'""$ M	 >d;>O=PPSo 	 or8   c                    t        t               x}xr8 |j                  j                  j	                  |       xr |j                  |             S r2   )boolr   apitypes	is_scalarisna)r   r   s     r6   r   r   %  s:    z|#U)?)?)DUQTVVr8   )r   r   r7   r   )r5   DataFrame[IntoDataFrameT]r0   Literal[False]r   r&   )r5   zLazyFrame[IntoFrameT]r0   r   r   r(   )r5   Series[IntoSeriesT]r0   r   r   r+   )r5   r   r0   r   r   r   )r5   zGDataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT] | Series[IntoSeriesT]r<   bool | Noner0   r   r   z/IntoDataFrameT | IntoFrameT | IntoSeriesT | Any)rR   r-   rS   r   r   r-   )rR   r%   rS   r   r   r%   )rR   r,   rS   r   r   r,   )rR   zIntoDataFrameT | IntoSeriesTr0   Literal[True]r[   r   rX   r   r\   r   r   z/DataFrame[IntoDataFrameT] | Series[IntoSeriesT])rR   r&   r0   r   r[   r   rX   r   r\   Noner   r   )rR   r.   r0   r   r[   r   rX   r   r\   r   r   r.   )rR   r&   r0   r   r[   r   rX   r   r\   r   r   r   )rR   r.   r0   r   r[   r   rX   r   r\   r   r   r.   )rR   z)IntoFrameT | IntoLazyFrameT | IntoSeriesTr0   r   r[   r   rX   r   r\   r   r   zGDataFrame[IntoFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT])rR   r+   r0   r   r[   r   rX   r   r\   r   r   r   )rR   r)   r0   r   r[   r   rX   r   r\   r   r   zLazyFrame[IntoLazyFrameT])rR   r&   r0   r   r[   r   rX   r   r\   r   r   r   )rR   r&   r0   r   r[   r   rX   r   r\   r   r   r   )rR   zIntoFrame | IntoSeriesr0   r   r[   r   rX   r   r\   r   r   z-DataFrame[Any] | LazyFrame[Any] | Series[Any])rR   r+   r0   r   r[   r   rX   r   r\   r   r   r   )rR   r   r0   r   r[   r   rX   r   r\   r   r   r   )rR   zFIntoLazyFrameT | IntoFrameT | IntoSeriesT | IntoFrame | IntoSeries | Tr<   r   r0   r   r[   r   rX   r   r\   r   rS   r   r   zKLazyFrame[IntoLazyFrameT] | DataFrame[IntoFrameT] | Series[IntoSeriesT] | T)rR   r   r0   r   r[   r   rn   r   rX   r   r\   r   ro   r   r   r   )r   zFDataFrame[Any] | LazyFrame[Any] | Series[Any] | IntoFrame | IntoSeriesr   r   r2   )r   zCallable[..., Any] | Noner<   r   r0   r   r[   r   rX   r   r\   r   r   r   )r   r   r   r   )r   r   r   r   )I
__future__r   r   r   decimalr   	functoolsr   typingr   r   r	   r
   r   r   narwhals._namespacer   r   r   r   rE   r   narwhals.dependenciesr   r   r   r   r   r   r   r   r   r   r   r   r   r   rF   r!   r"   rG   r$   narwhals.typingr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r   bytesstrintfloatcomplexr   dater   timer   r7   rT   rr   r   r   r   r   r   __all__r3   r8   r6   <module>r      s   "    K K  $   " 7&
 
 
 CL!5#sE7GL ",,8  
RU.AO 
 
NQ*=K 
 
LO(;I 
 
 F 
 F  $..
 . . 5.b 
 D 
 D 
 J 
 J 
 J 
 J 
 #&:/:  : 	:
  :  : 5: 
: 

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

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

 "%"%R<R  R 	R
  R  R MR 
R 

 "%   	
    
 
 $'!$"%$!$ !$ 	$
  $ $ $ 
$ 
 $'!$"%$!$ !$ 	$
  $ $ $ 
$ 
 $'"%$!$ !$ 	$
  $ $ $ 
$ 
 $'!$"%8)8 !8 	8
  8  8 38 
8 
 $'!$ ! 	
    
 
  	
   	 
  $ $CYC C 	C
 C C C C QCR &+ $`` ` 	`  $` ` ` ` 	`F
PD	O& '+u  $ $u
#u u 	u
 u u u up8vW Nr8   