o
    Lh`                     @  s`  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	rd 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jAejBfZCedddjddZDedddkddZDedddlddZDedmd dZDd!ddnd$dZDedod(d)ZEedpd+d)ZEedqd-d)ZEedd.drd5d)ZEedddd6dsd8d)ZEedddd6dtd9d)ZEeddd:dud;d)ZEeddd:dvd<d)ZEeddd=dwd?d)ZEeddd@dxdAd)ZEedddddBdydCd)ZEedddddBdzdDd)ZEeddddEd{dFd)ZEeddddGd|dJd)ZEeddddKd}dLd)ZEed~dNd)ZEd!d!d!ddBddQd)ZEd!d!d!d!ddRddVdWZFddZd[ZGdd\d]ZH	dd^d!d!d^dBddbdcZIddedfZJddgdhZKg di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]r2   Literal[False]returnr)   c                C     d S N r3   r2   r9   r9   S/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/narwhals/translate.py	to_native5      r<   LazyFrame[IntoLazyFrameT]r+   c                C  r7   r8   r9   r:   r9   r9   r;   r<   9   r=   Series[IntoSeriesT]r-   c                C  r7   r8   r9   r:   r9   r9   r;   r<   =   r=   r   boolc                C  r7   r8   r9   r:   r9   r9   r;   r<   A      FKDataFrame[IntoDataFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT]3IntoDataFrameT | IntoLazyFrameT | IntoSeriesT | Anyc                C  s\   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.dataframerD   narwhals.seriesr&   
isinstance_compliant_frame_native_frame_compliant_seriesnativetype	TypeError)r3   r2   rD   r&   msgr9   r9   r;   r<   E   s   

native_objectr/   kwdsc                 K  r7   r8   r9   rP   rQ   r9   r9   r;   from_nativef   rA   rS   r'   c                 K  r7   r8   r9   rR   r9   r9   r;   rS   j   rA   r.   c                 K  r7   r8   r9   rR   r9   r9   r;   rS   n   rA   )series_onlyIntoDataFrameT | IntoSeriesTLiteral[True]
eager_onlyrT   allow_series/DataFrame[IntoDataFrameT] | Series[IntoSeriesT]c                C  r7   r8   r9   rP   r2   rW   rT   rX   r9   r9   r;   rS   r      )rW   rT   rX   Nonec                C  r7   r8   r9   rZ   r9   r9   r;   rS   }   r[   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   )rT   rX   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   )rW   rT   -IntoDataFrameT | IntoLazyFrameT | IntoSeriesTc                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   )rW   rX   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   r2   rW   rT   rX   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   )r2   rT   rX   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   )r2   rW   rT   IntoFrame | IntoSeries-DataFrame[Any] | LazyFrame[Any] | Series[Any]c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   )r2   rW   rX   c                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   bool | Nonec                C  r7   r8   r9   rZ   r9   r9   r;   rS      r[   JIntoLazyFrameT | IntoDataFrameT | IntoSeriesT | IntoFrame | IntoSeries | TOLazyFrame[IntoLazyFrameT] | DataFrame[IntoDataFrameT] | Series[IntoSeriesT] | Tc             	   K  s6   |rdt t|}t|t| ||d||tjd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)r2   rW   eager_or_interchange_onlyrT   rX   version)nextiterrN   _from_native_implr   MAIN)rP   r2   rW   rT   rX   rQ   rO   r9   r9   r;   rS     s   ,)r2   rW   rd   rT   rX   rd   re   r   c                C  s  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| |r4|s2|r4| S |rB|du r@d}t|d}|rL|rLd	}t||| rh|r\|sZd
}t|| S |j|  |ddS |	| r|rx|svd}t|| S |s||r|sd}t|| S |j|  |ddS |
| r|s|sd}t|| S |j|  |ddS t| r|rt| s|sdt| j }t|| S |s|rt| r|sd}t|| S |st| r|sd}t|| S |j| j|  S t| r/t | r|r|sdt| j }t|| S n|s#|s!d}t|| S |j| j|  S t!| rht"| rN|rM|sKdt| j }t|| S n|s\|sZd}t|| S |j| j|  S t#| r|r{|syd}t|| S |s|r|sd}t|| S t$j%& dkrt' d u rd}t(||j)t$j%j|  S t*| r|s|r|sd}t|| S |j| j|  S t+| r|s|r|sd}t|| S |j| j|  S t,| r|j| }|s|s|r|sd|j- d}t|| S |j|  S || rTddl m.} |s.|r9|s7d}t|| S |t/j0urJ|rD| S d}t|t/j0j|| ddS |sbd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.dataframerj   narwhals._utilsrk   rl   rm   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	compliantrS   to_narwhalsr   r   r   r!   r   r   DASK_backend_versionr   ImportErrorfrom_backendr   r   r   implementationrr   r   V1)rP   r2   rW   rd   rT   rX   re   rj   rk   rl   rm   r#   r$   r&   rO   ns_sparkrr   r9   r9   r;   rh   =  sZ  














rh   obj,Frame | Series[Any] | IntoFrame | IntoSeriesc                  G  sF   | sd}t |d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                 S  s   h | ]}t |qS r9   ) _get_native_namespace_single_obj).0xr9   r9   r;   	<setcomp>8  s    z'get_native_namespace.<locals>.<setcomp>rq   z0Found objects with different native namespaces: rE   )rv   lenpop)r   rO   resultr9   r9   r;   get_native_namespace  s   r   c                 C  s$   t | r|  S tjj| j S r8   )r   __native_namespace__r   ri   r   r   r   to_native_namespace)r   r9   r9   r;   r   ?  s   r   TfuncCallable[..., Any] | NoneCallable[..., Any]c                  s(   d fdd}| du r|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   r6   c                   s"   t  d fdd}|S )Nargsr   kwargsr6   c                    s   fdd| D } fdd|  D } fddg | | R D }t|dkr6d}t|| i |}t|d	S )
Nc              	     s   g | ]}t | d qS r^   rS   )r   argrX   rW   r2   rT   r9   r;   
<listcomp>  s    zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>c              
     s$   i | ]\}}|t | d qS r   r   )r   namevaluer   r9   r;   
<dictcomp>  s    zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<dictcomp>c                   s"   h | ]}t |d d  r  qS )r   N)getattr)r   vbr9   r;   r     s    zAnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<setcomp>rq   z_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r1   )itemsvaluesr   rv   r<   )r   r   backendsrO   r   )rX   rW   r   r2   rT   r   r;   wrapper  s   
z.narwhalify.<locals>.decorator.<locals>.wrapper)r   r   r   r   r6   r   r   )r   r   r   )r   r;   	decorator  s   %znarwhalify.<locals>.decoratorN)r   r   r6   r   r9   )r   r2   rW   rT   rX   r   r9   r   r;   
narwhalifyI  s   8)r   scalar_likec                 C  s  t  }| du st| tr| }|S t  }r0t| |jr0| jdkr0|  t }tt	j
|d }|S t| s8t| r>|  }|S |rLt| |jrL|  }|S |rZt| |jrZ|  }|S t| trc| }|S t| rkd}|S t| ru|  }|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   rH   NON_TEMPORAL_SCALAR_TYPESr   
datetime64dtypeitemr   r   dt	timedeltar   r   	Timestampto_pydatetime	Timedeltato_pytimedeltaTEMPORAL_SCALAR_TYPES_is_pandas_nar    as_pyrM   rv   )r   pdscalarnpmsrO   r9   r9   r;   to_py_scalar  sF   


	r   c                 C  s&   t t  }o|jj| o|| S r8   )r@   r   apitypes	is_scalarisna)r   r   r9   r9   r;   r     s   &r   )r   r   r<   r   )r3   r4   r2   r5   r6   r)   )r3   r>   r2   r5   r6   r+   )r3   r?   r2   r5   r6   r-   )r3   r   r2   r@   r6   r   )r3   rB   r2   r@   r6   rC   )rP   r/   rQ   r   r6   r/   )rP   r'   rQ   r   r6   r'   )rP   r.   rQ   r   r6   r.   )rP   rU   r2   rV   rW   rV   rT   r5   rX   rV   r6   rY   )rP   r)   r2   rV   rW   r5   rT   r5   rX   r\   r6   r4   )rP   r0   r2   rV   rW   r5   rT   r5   rX   r\   r6   r0   )rP   r)   r2   rV   rW   rV   rT   r5   rX   r\   r6   r4   )rP   r0   r2   rV   rW   rV   rT   r5   rX   r\   r6   r0   )rP   r]   r2   rV   rW   r5   rT   r5   rX   rV   r6   rB   )rP   r-   r2   rV   rW   r5   rT   rV   rX   r\   r6   r?   )rP   r+   r2   r5   rW   r5   rT   r5   rX   r\   r6   r>   )rP   r)   r2   r5   rW   r5   rT   r5   rX   r\   r6   r4   )rP   r)   r2   r5   rW   rV   rT   r5   rX   r\   r6   r4   )rP   r_   r2   r5   rW   r5   rT   r5   rX   rV   r6   r`   )rP   r-   r2   r5   rW   r5   rT   rV   rX   r\   r6   r?   )rP   r   r2   r@   rW   r@   rT   r@   rX   ra   r6   r   )rP   rb   r2   r@   rW   r@   rT   r@   rX   ra   rQ   r   r6   rc   )rP   r   r2   r@   rW   r@   rd   r@   rT   r@   rX   ra   re   r   r6   r   )r   r   r6   r   r8   )r   r   r2   r@   rW   r@   rT   r@   rX   ra   r6   r   )r   r   r6   r   )r   r   r6   r@   )M
__future__r   datetimer   decimalr   	functoolsr   typingr   r   r   r	   r
   r   narwhals._constantsr   r   narwhals._namespacer   r   r   r   ru   r   r   r   narwhals.dependenciesr   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/   r0   r@   bytesstrintfloatcomplexr   dater   timer   r<   rS   rh   r   r   r   r   r   __all__r9   r9   r9   r;   <module>   s     <,!










> 
c
 
g
;