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:d|d:d:dkdkdkdsdhdtd=d=d=d=d=d=dudvdwdxZ;d6d6dydzd{Z<drd1dxd{gZ=d:S )}    )annotations)datetime)	timedeltawraps)TYPE_CHECKING)Any)Callable)Literal)TypeVar)overload)get_cudf)get_cupy)get_dask)get_dask_expr)	get_modin)	get_numpy)
get_pandas)
get_polars)get_pyarrow)is_cudf_dataframe)is_cudf_series)is_dask_dataframe)is_duckdb_relation)is_ibis_table)is_modin_dataframe)is_modin_series)is_pandas_dataframe)is_pandas_series)is_polars_dataframe)is_polars_lazyframe)is_polars_series)is_pyarrow_chunked_array)is_pyarrow_table	DataFrame	LazyFrameSeries)DTypes)IntoDataFrameT)
IntoFrameT)IntoSeriesTT.pass_throughzDataFrame[IntoDataFrameT]zLiteral[False]r+   )narwhals_objectr0   returnc                C  s   d S N r1   r0   r4   r4   6/tmp/pip-unpacked-wheel-hfsjijke/narwhals/translate.py	to_native:   s    r7   zLazyFrame[IntoFrameT]r,   c                C  s   d S r3   r4   r5   r4   r4   r6   r7   >   s    r)   r   c                C  s   d S r3   r4   r5   r4   r4   r6   r7   B   s    boolc                C  s   d S r3   r4   r5   r4   r4   r6   r7   D   s    N)strictr0   z6DataFrame[IntoFrameT] | LazyFrame[IntoFrameT] | Serieszbool | NonezIntoFrameT | Any)r1   r9   r0   r2   c                C  sx   ddl m} ddlm} ddlm} |||ddd}t| |rF| jjS t| |rX| j	j
S |stdt|  d	}t|| S )
z
    Convert Narwhals object to native one.

    Arguments:
        narwhals_object: Narwhals object.
        strict: whether to raise on non-Narwhals input.

    Returns:
        Object of class that user started with.
    r   )	BaseFramer(   validate_strict_and_pass_thoughFTZpass_through_defaultZemit_deprecation_warningzExpected Narwhals object, got .)narwhals.dataframer:   narwhals.seriesr)   narwhals.utilsr<   
isinstanceZ_compliant_frameZ_native_frameZ_compliant_seriesZ_native_seriestype	TypeError)r1   r9   r0   r:   r)   r<   msgr4   r4   r6   r7   H   s"       

)
eager_onlyseries_onlyzIntoDataFrameT | IntoSeriesTzLiteral[True]None)native_objectr0   rF   eager_or_interchange_onlyrG   allow_seriesr2   c                C  s   d S r3   r4   rI   r0   rF   rJ   rG   rK   r4   r4   r6   from_nativek   s    	rM   )rJ   rG   z"DataFrame[IntoDataFrameT] | Seriesc                C  s   d S r3   r4   rL   r4   r4   r6   rM   w   s    	)rF   rG   rK   c                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	c                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	)rJ   rG   rK   c                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	c                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	)rF   rJ   rG   zIntoFrameT | IntoSeriesTc                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	)rF   rJ   rK   r-   c                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	)rF   rJ   rG   rK   z-DataFrame[IntoFrameT] | LazyFrame[IntoFrameT]c                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	c                C  s   d S r3   r4   rL   r4   r4   r6   rM      s    	)r0   rF   rG   rK   c                C  s   dS )z
    from_native(df, pass_through=False, eager_or_interchange_only=True)
    from_native(df, eager_or_interchange_only=True)
    Nr4   rL   r4   r4   r6   rM      s    
)r0   rJ   rG   rK   c                C  s   dS )zc
    from_native(df, pass_through=False, eager_only=True)
    from_native(df, eager_only=True)
    Nr4   rL   r4   r4   r6   rM      s    
)r0   rF   rJ   rG   z(DataFrame[Any] | LazyFrame[Any] | Seriesc                C  s   dS )zg
    from_native(df, pass_through=False, allow_series=True)
    from_native(df, allow_series=True)
    Nr4   rL   r4   r4   r6   rM     s    
)r0   rF   rJ   rK   c                C  s   dS )ze
    from_native(df, pass_through=False, series_only=True)
    from_native(df, series_only=True)
    Nr4   rL   r4   r4   r6   rM     s    
r0   rF   rJ   rG   rK   c                C  s   dS )zA
    from_native(df, pass_through=False)
    from_native(df)
    Nr4   rL   r4   r4   r6   rM   #  s    
)rJ   c                C  s   d S r3   r4   rL   r4   r4   r6   rM   4  s    	)r9   r0   rF   rJ   rG   rK   )rI   r9   r0   rF   rJ   rG   rK   r2   c          	   	   C  s>   ddl m} ddlm} |||ddd}t| ||||||dS )aX  
    Convert dataframe/series to Narwhals DataFrame, LazyFrame, or Series.

    Arguments:
        native_object: Raw object from user.
            Depending on the other arguments, input object can be:

            - pandas.DataFrame
            - polars.DataFrame
            - polars.LazyFrame
            - anything with a `__narwhals_dataframe__` or `__narwhals_lazyframe__` method
            - pandas.Series
            - polars.Series
            - anything with a `__narwhals_series__` method
        strict: Determine what happens if the object isn't supported by Narwhals:

            - `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](https://narwhals-dev.github.io/narwhals/backcompat/).
        pass_through: Determine what happens if the object isn't supported by Narwhals:

            - `False` (default): raise an error
            - `True`: pass object through as-is
        eager_only: Whether to only allow eager objects.
        eager_or_interchange_only: Whether to only allow eager objects or objects which
            implement the Dataframe Interchange Protocol.
        series_only: Whether to only allow series.
        allow_series: Whether to allow series (default is only dataframe / lazyframe).

    Returns:
        narwhals.DataFrame or narwhals.LazyFrame or narwhals.Series
    r   dtypesr;   FTr=   )r0   rF   rJ   rG   rK   rP   )ZnarwhalsrP   rA   r<   _from_native_impl)	rI   r9   r0   rF   rJ   rG   rK   rP   r<   r4   r4   r6   rM   @  s"    .   Fr*   )rI   r0   rF   rJ   rG   rK   rP   r2   c                C  s  ddl m} ddlm} ddlm}	 ddlm}
 ddlm	} ddl
m} ddlm} dd	lm} dd
lm} ddlm} ddlm} ddlm} ddlm} ddlm} ddlm} ddlm} t| ||fr|s| S t| |r|s|r| S |r|dkrd}t|d}|r$|r$d}t|t| dr\|rL|sHd}t || S || ! ddS t| dr|r|sd}t || S |s|r|sd}t || S || " ddS t| dr|s|sd}t || S || # ddS t$| r6|r|sd}t || S t% }||| ||j&|d ddS t'| r|r\|sXd!}t || S |sh|r~|szd"}t || S t% }||| ||j&|d ddS t(| rt% }|s|sd}t || S ||| ||j&|d ddS t)| r4|r|s
d}t || S t* }||| ||j&|j+|d#ddS t,| r|sZ|sVd}t || S t* }||| |j+||j&|d$ddS t-| rt. }|r|sd%}t || S ||| |j/||j&|d$ddS t0| rt. }|s|sd}t || S ||| |j/||j&|d$ddS t1| rdt2 }|rD|s@d&}t || S ||| |j3||j&|d$ddS t4| rt2 }|s|sd}t || S ||| |j3||j&|d$ddS t5| rt6 }|r|sd'}t || S ||| ||j&|d ddS t7| rBt6 }|s$|s d}t || S ||| ||j&d(|d)ddS t8| r|rh|sdd*}t || S |st|r|sd+}t || S t9 d krd,}t:|||	| |t; j&|d ddS t<| r|s|r|sd-}n| S t |||
| |d.d/dS t=| rB|s|r.|s*d0}t || S ||| |d.d/dS t| d1r|sZ|rp|sld2}t || S ||| |d.d/dS |sd3t>|  }t || S )4Nr   )ArrowDataFrame)ArrowSeries)DaskLazyFrame)DuckDBInterchangeFrame)IbisInterchangeFrame)InterchangeFrame)PandasLikeDataFrame)PandasLikeSeries)PolarsDataFrame)PolarsLazyFrame)PolarsSeriesr$   r&   r(   )Implementation)parse_versionFzJInvalid parameter combination: `series_only=True` and `allow_series=False`TzUInvalid parameter combination: `eager_only=True` and `eager_or_interchange_only=True`__narwhals_dataframe__z,Cannot only use `series_only` with dataframefull)level__narwhals_lazyframe__z,Cannot only use `series_only` with lazyframezJCannot only use `eager_only` or `eager_or_interchange_only` with lazyframe__narwhals_series__z4Please set `allow_series=True` or `series_only=True`z3Cannot only use `series_only` with polars.DataFrame)backend_versionrP   z3Cannot only use `series_only` with polars.LazyFramezQCannot only use `eager_only` or `eager_or_interchange_only` with polars.LazyFrame)rd   implementationrP   )re   rd   rP   z2Cannot only use `series_only` with modin.DataFramez1Cannot only use `series_only` with cudf.DataFramez.Cannot only use `series_only` with arrow table )rd   namerP   z1Cannot only use `series_only` with dask DataFramezOCannot only use `eager_only` or `eager_or_interchange_only` with dask DataFramezPlease install dask-exprzMCannot only use `series_only=True` or `eager_only=False` with DuckDB RelationrO   ZinterchangezHCannot only use `series_only=True` or `eager_only=False` with Ibis tableZ__dataframe__zhCannot only use `series_only=True` or `eager_only=False` with object which only implements __dataframe__zLExpected pandas-like dataframe, Polars dataframe, or Polars lazyframe, got: )?Znarwhals._arrow.dataframerR   Znarwhals._arrow.seriesrS   Znarwhals._dask.dataframerT   Znarwhals._duckdb.dataframerU   Znarwhals._ibis.dataframerV   Znarwhals._interchange.dataframerW   Znarwhals._pandas_like.dataframerX   Znarwhals._pandas_like.seriesrY   Znarwhals._polars.dataframerZ   r[   Znarwhals._polars.seriesr\   r?   r%   r'   r@   r)   rA   r]   r^   rB   
ValueErrorhasattrrD   r_   rb   rc   r   r   __version__r    r!   r   r   ZPANDASr   r   r   ZMODINr   r   r   ZCUDFr   r#   r   r"   r   r   ImportErrorr   r   r   rC   )rI   r0   rF   rJ   rG   rK   rP   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r%   r'   r)   r]   r^   rE   plpdZmpdZcudfpar4   r4   r6   rQ     sV   






	

	

	










rQ   )objr2   c                 C  s   |   S )a  
    Get native namespace from object.

    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'...>
    )__native_namespace__)ro   r4   r4   r6   get_native_namespace  s    rq   TzCallable[..., Any] | NoneCallable[..., Any])funcr9   r0   rF   rJ   rG   rK   r2   c          	        sP   ddl m} ||dddddd fdd}| d	krD|S || S d	S )
av  
    Decorate function so it becomes dataframe-agnostic.

    `narwhalify` will try to convert any dataframe/series-like object into the narwhal
    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 `strict=True`, then every input and every output will be required to be a
    dataframe/series-like object.

    Instead of writing

    ```python
    import narwhals as nw


    def func(df):
        df = nw.from_native(df, strict=False)
        df = df.group_by("a").agg(nw.col("b").sum())
        return nw.to_native(df)
    ```

    you can just write

    ```python
    import narwhals as nw


    @nw.narwhalify
    def func(df):
        return df.group_by("a").agg(nw.col("b").sum())
    ```

    You can also pass in extra arguments, e.g.

    ```python
    @nw.narwhalify(eager_only=True)
    ```

    that will get passed down to `nw.from_native`.

    Arguments:
        func: Function to wrap in a `from_native`-`to_native` block.
        strict: Whether to raise if object can't be converted or to just leave it as-is
            (default).
        eager_only: Whether to only allow eager objects.
        eager_or_interchange_only: Whether to only allow eager objects or objects which
            implement the Dataframe Interchange Protocol.
        series_only: Whether to only allow series.
        allow_series: Whether to allow series (default is only dataframe / lazyframe).
    r   r;   Tr=   rr   )rs   r2   c                   s,   t  dddd fdd}|S )Nr   )argskwargsr2   c                    s   fdd| D } fdd|  D } fdd| | D }t|dkrjd}t|| |}t|d	S )
Nc              
     s    g | ]}t | d qS rN   rM   ).0argrK   rF   rJ   r0   rG   r4   r6   
<listcomp>1  s   	zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>c                   s&   i | ]\}}|t | d qS rv   rw   )rx   rg   valuerz   r4   r6   
<dictcomp>=  s   	 zBnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<dictcomp>c                   s"   h | ]}t |d d  r  qS )rp   N)getattr)rx   vbr4   r6   	<setcomp>I  s   zAnarwhalify.<locals>.decorator.<locals>.wrapper.<locals>.<setcomp>   z_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r/   )itemsvalueslenrh   r7   )rt   ru   backendsrE   result)rK   rF   rJ   rs   r0   rG   r   r6   wrapper/  s    		


z.narwhalify.<locals>.decorator.<locals>.wrapperr   )rs   r   rz   )rs   r6   	decorator.  s    "'znarwhalify.<locals>.decoratorN)rA   r<   )	rs   r9   r0   rF   rJ   rG   rK   r<   r   r4   rz   r6   
narwhalify  s    @   +r   )scalar_liker2   c                 C  sj  | dkrdS t | tr| S t }|rXt | |jrX| jdkrXtdddt|  d d S |rx|| rxt	| drx|  S t
 }|rt | |jr|  S |rt | |jr|  S |r|jj| rz|| }W n tk
r   Y n
X |rdS t | ttfr| S t }|r t | |jr |  S t }|rNt | |jrN| jdkrN|  S dt|  d	}t|dS )
a  If a scalar is not Python native, converts it to Python native.

    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
    Nzdatetime64[ns]i  r   i  )microsecondsitemz/Expected object convertible to a scalar, found zH. Please report a bug to https://github.com/narwhals-dev/narwhals/issues)rB   NON_TEMPORAL_SCALAR_TYPESr   Z
datetime64Zdtyper   r   r   Zisscalarri   r   Z	TimestampZto_pydatetimeZ	TimedeltaZto_pytimedeltaapitypesZ	is_scalarZisna	Exceptionr   ZScalarZas_pyr   ZndarraysizerC   rh   )r   nprm   Zis_narn   ZcupyrE   r4   r4   r6   to_py_scalar`  sT    


r   )N)>
__future__r   r   r   	functoolsr   typingr   r   r	   r
   r   r   Znarwhals.dependenciesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r?   r%   r'   r@   r)   Znarwhals.typingr*   r+   r,   r-   r.   r8   bytesstrintfloatcomplexr   r7   rM   rQ   rq   r   r   __all__r4   r4   r4   r6   <module>   s`  
#                 C   X  xH