
    bZh	I                       d dl mZ d dlZd dlmZmZ erbd dlZd dlmZ	 d dl
Z
d dlZd dlmZ d dlZd dlZd dlZd dlmZ d dlmZ d dlmZmZ d dlmZ 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(m)Z)m*Z*m+Z+m,Z,  e-d
g      Z.dBdZ/dBdZ0dBdZ1dBdZ2dBdZ3dBdZ4dBdZ5dBdZ6dBdZ7dBdZ8dBdZ9dBdZ:dBdZ;dBdZ<dBdZ=dBdZ>dCdZ?dDdZ@dEdZAdFdZBdGdZCdHd ZDdId!ZEdJd"ZFdKd#ZGdLd$ZHdMd%ZIdNd&ZJdOd'ZKdPd(ZLdQd)ZMdRd*ZNdSd+ZOdTd,ZPdUd-ZQdVd.ZRdWd/ZSdXd0ZTdYd1ZUdZd2ZVd[d3ZWd\d4ZXd]d5ZYd^d6ZZd_d7Z[d`d8Z\dad9Z]dbd:Z^dcd;Z_ddd<Z`	 	 	 	 ded=Za	 	 	 	 dfd>Zbdgd?Zcdgd@Zdg dAZey)h    )annotationsN)TYPE_CHECKINGAny	DataFrame)	TypeGuardTypeIs)SQLFrameDataFrame)r   	LazyFrameSeries)	IntoDataFrameTIntoLazyFrameTIntoSeriesT_1DArray_1DArrayInt_2DArray_NDArray_NumpyScalar_ShapeT	fireducksc                 B    t         j                  j                  dd      S )z;Get Polars module (if already imported - else return None).polarsNsysmodulesget     d/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/narwhals/dependencies.py
get_polarsr!   ,       ;;??8T**r   c                 B    t         j                  j                  dd      S )z;Get pandas module (if already imported - else return None).pandasNr   r   r   r    
get_pandasr%   1   r"   r   c                 `    t         j                  j                  dd      x} | j                  S y)zAGet modin.pandas module (if already imported - else return None).modinN)r   r   r   r$   )r'   s    r    	get_modinr(   6   s)    $//<||r   c                 B    t         j                  j                  dd      S )z9Get cudf module (if already imported - else return None).cudfNr   r   r   r    get_cudfr+   =       ;;??64((r   c                 B    t         j                  j                  dd      S )z9Get cupy module (if already imported - else return None).cupyNr   r   r   r    get_cupyr/   B   r,   r   c                 B    t         j                  j                  dd      S )z<Get pyarrow module (if already imported - else return None).pyarrowNr   r   r   r    get_pyarrowr2   G       ;;??9d++r   c                 B    t         j                  j                  dd      S )z:Get numpy module (if already imported - else return None).numpyNr   r   r   r    	get_numpyr6   L   s    ;;??7D))r   c                 B    t         j                  j                  dd      S )z2Get dask (if already imported - else return None).daskNr   r   r   r    get_daskr9   Q   r,   r   c                 B    t         j                  j                  dd      S )zCGet dask.dataframe module (if already imported - else return None).zdask.dataframeNr   r   r   r    get_dask_dataframer;   V   s    ;;??+T22r   c                 B    t         j                  j                  dd      S )z;Get duckdb module (if already imported - else return None).duckdbNr   r   r   r    
get_duckdbr>   [   r"   r   c                 B    t         j                  j                  dd      S )z9Get ibis module (if already imported - else return None).ibisNr   r   r   r    get_ibisrA   `   r,   r   c                     t               x} t        | d      r| j                  S t        j                  j                  dd      S )z>Get dask_expr module (if already imported - else return None).N	dask_expr)r;   hasattrrC   r   r   r   )dds    r    get_dask_exprrF   e   s8     ""/GB4L||;;??;--r   c                 B    t         j                  j                  dd      S )z<Get pyspark module (if already imported - else return None).pysparkNr   r   r   r    get_pysparkrI   l   r3   r   c                 B    t         j                  j                  dd      S )z@Get pyspark.sql module (if already imported - else return None).zpyspark.sqlNr   r   r   r    get_pyspark_sqlrK   q   s    ;;??=$//r   c                 B    t         j                  j                  dd      S )zHGet pyspark.sql.connect module (if already imported - else return None).zpyspark.sql.connectNr   r   r   r    get_pyspark_connectrM   v   s    ;;??0$77r   c                 B    t         j                  j                  dd      S )z=Get sqlframe module (if already imported - else return None).sqlframeNr   r   r   r    get_sqlframerP   {   s    ;;??:t,,r   c                d    t        |       st        |       rdt        |        d}t        |      y )NYou passed a `z` to `is_pandas_dataframe`.

Hint: Instead of e.g. `is_pandas_dataframe(df)`, did you mean `is_pandas_dataframe(df.to_native())`?)is_narwhals_dataframeis_narwhals_lazyframetype	TypeError)dfmsgs     r    _raise_if_narwhals_df_or_lfrY      s?    R $9"$=T"XJ 'B B 	
 n %>r   c                N    t        |       rdt        |        d}t        |      y )NrR   zz` to `is_pandas_series`.

Hint: Instead of e.g. `is_pandas_series(ser)`, did you mean `is_pandas_series(ser.to_native())`?)is_narwhals_seriesrU   rV   )serrX   s     r    _raise_if_narwhals_seriesr]      s7    #T#YK (@ @ 	
 n r   c                     t                t               x}duxr t         |j                        xs t	         fdt
        D              S )zCheck whether `df` is a pandas DataFrame without importing pandas.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    Nc              3     K   | ]J  }t         j                  j                  |d       xd uxr  t        j                  j
                         L y wN)r   r   r   
isinstancer$   r   ).0module_namerW   mods     r    	<genexpr>z&is_pandas_dataframe.<locals>.<genexpr>   sT      V  T2	24? 	1r3:://0	1V   AA)rY   r%   ra   r   anyIMPORT_HOOKS)rW   pdrd   s   ` @r    is_pandas_dataframerj      sP      #<R,MB1M RU V (V S r   c                     t                t               x}duxr t         |j                        xs t	         fdt
        D              S )zCheck whether `ser` is a pandas Series without importing pandas.

    Warning:
        This method cannot be called on Narwhals Series.
    Nc              3     K   | ]J  }t         j                  j                  |d       xd uxr  t        j                  j
                         L y wr`   )r   r   r   ra   r$   r   )rb   rc   rd   r\   s     r    re   z#is_pandas_series.<locals>.<genexpr>   sT      T  T2	24? 	/sCJJ--.	/Trf   )r]   r%   ra   r   rg   rh   )r\   ri   rd   s   ` @r    is_pandas_seriesrm      sP     c"<R,KC1K PS T (T Q r   c                     t               x}duxr t         |j                        xs t         fdt        D              S )zACheck whether `index` is a pandas Index without importing pandas.Nc              3     K   | ]J  }t         j                  j                  |d       xd uxr  t        j                  j
                         L y wr`   )r   r   r   ra   r$   Index)rb   rc   indexrd   s     r    re   z"is_pandas_index.<locals>.<genexpr>   sT      U  T2	24? 	0ucjj../	0Urf   )r%   ra   rp   rg   rh   )rq   ri   rd   s   ` @r    is_pandas_indexrr      sF    <R,LE2881L QT U (U R r   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `df` is a modin DataFrame without importing modin.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rY   r(   ra   r   )rW   mpds     r    is_modin_dataframeru      s.      #;Ct+M
2s}}0MMr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `ser` is a modin Series without importing modin.

    Warning:
        This method cannot be called on Narwhals Series.
    N)r]   r(   ra   r   )r\   rt   s     r    is_modin_seriesrw      s.     c";Ct+K
3

0KKr   c                N    t               x}duxr t        | |j                        S )z?Check whether `index` is a modin Index without importing modin.N)r(   ra   rp   )rq   rt   s     r    is_modin_indexry      s$    ;Ct+L
5#))0LLr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `df` is a cudf DataFrame without importing cudf.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rY   r+   ra   r   )rW   r*   s     r    is_cudf_dataframer{      s.      #JDt+N
2t~~0NNr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `ser` is a cudf Series without importing cudf.

    Warning:
        This method cannot be called on Narwhals Series.
    N)r]   r+   ra   r   )r\   r*   s     r    is_cudf_seriesr}      s.     c"JDt+L
30LLr   c                N    t               x}duxr t        | |j                        S )z=Check whether `index` is a cudf Index without importing cudf.N)r+   ra   rp   )rq   r*   s     r    is_cudf_indexr      s+    JDt+ 
tzz1 r   c                p    t               x}d uxr' t        | |j                        xr | j                  dk(  S )N   )r/   ra   ndarraysize)objr.   s     r    is_cupy_scalarr      s9    	D( 	sDLL)	HHMr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `df` is a Dask DataFrame without importing Dask.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rY   r;   ra   r   )rW   rE   s     r    is_dask_dataframer      s/      #$&&Bt3T
2r||8TTr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `df` is a DuckDB Relation without importing DuckDB.

    Warning:
        This method cannot be called on Narwhals DataFrame/LazyFrame.
    N)rY   r>   ra   DuckDBPyRelation)rW   r=   s     r    is_duckdb_relationr      s7      # l"F4/ J
F##5 r   c                    t        |        t               x}duxr* t        | |j                  j                  j
                        S )zCheck whether `df` is a Ibis Table without importing Ibis.

    Warning:
        This method cannot be called on Narwhals DataFrame/LazyFrame.
    N)rY   rA   ra   exprtypesTable)rW   r@   s     r    is_ibis_tabler     s8      #JDt+U
2tyy?T?T0UUr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `df` is a Polars DataFrame without importing Polars.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rY   r!   ra   r   rW   pls     r    is_polars_dataframer     .      #,Bt+L
2r||0LLr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `df` is a Polars LazyFrame without importing Polars.

    Warning:
        This method cannot be called on Narwhals DataFrame/LazyFrame.
    N)rY   r!   ra   r   r   s     r    is_polars_lazyframer     r   r   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `ser` is a Polars Series without importing Polars.

    Warning:
        This method cannot be called on Narwhals Series.
    N)r]   r!   ra   r   )r\   r   s     r    is_polars_seriesr   )  s.     c",Bt+J
3		0JJr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `ser` is a PyArrow ChunkedArray without importing PyArrow.

    Warning:
        This method cannot be called on Narwhals Series.
    N)r]   r2   ra   ChunkedArray)r\   pas     r    is_pyarrow_chunked_arrayr   3  s.     c"-B,QC1QQr   c                d    t        |        t               x}duxr t        | |j                        S )zCheck whether `df` is a PyArrow Table without importing PyArrow.

    Warning:
        This method cannot be called on Narwhals DataFrame/LazyFrame.
    N)rY   r2   ra   r   )rW   r   s     r    is_pyarrow_tabler   =  s.      #-B,IB1IIr   c                N    t               x}d uxr t        | |j                        S r`   )r2   ra   Scalar)r   r   s     r    is_pyarrow_scalarr   G  s$    -B,KC1KKr   c                v    t        |        t        t               x}duxr t        | |j                              S )zCheck whether `df` is a PySpark DataFrame without importing PySpark.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rY   boolrK   ra   r   )rW   pyspark_sqls     r    is_pyspark_dataframer   K  s>      #')	)$6 	2r;001 r   c                r    t        |        t               	 ddlm} t        | |      S y# t        $ r Y yw xY w)zCheck whether `df` is a PySpark Connect DataFrame without importing PySpark.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    r   r   F)rY   rM   pyspark.sql.connect.dataframer   ImportErrorra   rW   r   s     r    is_pyspark_connect_dataframer   X  sC      #(	? "i((  		s   * 	66c                R    t        |        t               ddlm} t	        | |      S y)zCheck whether `df` is a SQLFrame DataFrame without importing SQLFrame.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    r   )BaseDataFrameF)rY   rP   sqlframe.base.dataframer   ra   )rW   r   s     r    is_sqlframe_dataframer   h  s'      #~!9"m,,r   c                N    t               x}duxr t        | |j                        S )z=Check whether `arr` is a NumPy Array without importing NumPy.N)r6   ra   r   arrnps     r    is_numpy_arrayr   v  s$    +Bd*Jz#rzz/JJr   c                :    t        |       xr | j                  dk(  S )z@Check whether `arr` is a 1D NumPy Array without importing NumPy.r   r   ndimr   s    r    is_numpy_array_1dr   {      #0388q=0r   c                    t               x}xr3 t        |       xr& |j                  | j                  |j                        S r`   )r6   r   
issubdtypedtypeintegerr   s     r    is_numpy_array_1d_intr     s9    {	 	1c"	1MM#))RZZ0r   c                :    t        |       xr | j                  dk(  S )z@Check whether `arr` is a 2D NumPy Array without importing NumPy.   r   r   s    r    is_numpy_array_2dr     r   r   c                N    t               x}duxr t        | |j                        S )zACheck whether `scalar` is a NumPy Scalar without importing NumPy.N)r6   ra   generic)scalarr   s     r    is_numpy_scalarr     s&    
 +Bd*Mz&"**/MMr   c                b    t        |        t        |       xs t        |       xs t        |       S )zCheck whether `df` is a pandas-like DataFrame without doing any imports.

    By "pandas-like", we mean: pandas, Modin, cuDF.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    )rY   rj   ru   r{   )rW   s    r    is_pandas_like_dataframer     s.      #r"U&8&<U@QRT@UUr   c                b    t        |        t        |       xs t        |       xs t        |       S )zCheck whether `ser` is a pandas-like Series without doing any imports.

    By "pandas-like", we mean: pandas, Modin, cuDF.

    Warning:
        This method cannot be called on Narwhals Series.
    )r]   rm   rw   r}   r\   s    r    is_pandas_like_seriesr     s+     c"C OOC$8ON3<OOr   c                L    t        |       xs t        |       xs t        |       S )zCheck whether `index` is a pandas-like Index without doing any imports.

    By "pandas-like", we mean: pandas, Modin, cuDF.
    )rr   ry   r   )rq   s    r    is_pandas_like_indexr     s%     	O."7O=;Or   c                    ddl m} t        | |      xs3 t        | d      xs% t	        |       xs t        |       xs t        |       S )a  Check whether `native_series` can be converted to a Narwhals Series.

    Arguments:
        native_series: The object to check.

    Returns:
        `True` if `native_series` can be converted to a Narwhals Series, `False` otherwise.

    Examples:
        >>> import pandas as pd
        >>> import polars as pl
        >>> import numpy as np
        >>> import narwhals as nw

        >>> s_pd = pd.Series([1, 2, 3])
        >>> s_pl = pl.Series([1, 2, 3])
        >>> np_arr = np.array([1, 2, 3])

        >>> nw.dependencies.is_into_series(s_pd)
        True
        >>> nw.dependencies.is_into_series(s_pl)
        True
        >>> nw.dependencies.is_into_series(np_arr)
        False
    r   r   __narwhals_series__)narwhals.seriesr   ra   rD   r   r   r   )native_seriesr   s     r    is_into_seriesr     sS    4 ' 	=&) 	0="78	0M*	0 $M2	0 !/r   c                    ddl m} t        | |      xs3 t        | d      xs% t	        |       xs t        |       xs t        |       S )a  Check whether `native_dataframe` can be converted to a Narwhals DataFrame.

    Arguments:
        native_dataframe: The object to check.

    Returns:
        `True` if `native_dataframe` can be converted to a Narwhals DataFrame, `False` otherwise.

    Examples:
        >>> import pandas as pd
        >>> import polars as pl
        >>> import numpy as np
        >>> from narwhals.dependencies import is_into_dataframe

        >>> df_pd = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
        >>> df_pl = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
        >>> np_arr = np.array([[1, 4], [2, 5], [3, 6]])

        >>> is_into_dataframe(df_pd)
        True
        >>> is_into_dataframe(df_pl)
        True
        >>> is_into_dataframe(np_arr)
        False
    r   r   __narwhals_dataframe__)narwhals.dataframer   ra   rD   r   r   r   )native_dataframer   s     r    is_into_dataframer     sX    4 - 	#Y/ 	6#%=>	6/0	6 ,-	6 $$45r   c                &    ddl m} t        | |      S )zCheck whether `df` is a Narwhals DataFrame.

    This is useful if you expect a user to pass in a Narwhals
    DataFrame directly, and you want to catch both `narwhals.DataFrame`
    and `narwhals.stable.v1.DataFrame`.
    r   r   )r   r   ra   r   s     r    rS   rS          -b)$$r   c                &    ddl m} t        | |      S )zCheck whether `lf` is a Narwhals LazyFrame.

    This is useful if you expect a user to pass in a Narwhals
    LazyFrame directly, and you want to catch both `narwhals.LazyFrame`
    and `narwhals.stable.v1.LazyFrame`.
    r   )r   )r   r   ra   )lfr   s     r    rT   rT     r   r   c                &    ddl m} t        | |      S )zCheck whether `ser` is a Narwhals Series.

    This is useful if you expect a user to pass in a Narwhals
    Series directly, and you want to catch both `narwhals.Series`
    and `narwhals.stable.v1.Series`.
    r   r   )r   r   ra   )r\   r   s     r    r[   r[     s     'c6""r   c                P    t        |       xr | j                  j                         S r`   )r[   r   
is_integerr   s    r    is_narwhals_series_intr   )  s    c"=syy';';'==r   )r+   rA   r(   r6   r%   r!   r2   r{   r}   r   r   r   r   ru   rw   rS   rT   r[   r   rj   rr   r   r   rm   r   r   r   r   r   )returnr   )rW   r   r   None)r\   r   r   r   )rW   r   r   zTypeIs[pd.DataFrame])r\   r   r   zTypeIs[pd.Series[Any]])rq   r   r   zTypeIs[pd.Index[Any]])rW   r   r   zTypeIs[mpd.DataFrame])r\   r   r   zTypeIs[mpd.Series])rq   r   r   zTypeIs[mpd.Index[Any]])rW   r   r   zTypeIs[cudf.DataFrame])r\   r   r   zTypeIs[cudf.Series[Any]])rq   r   r   zTypeIs[cudf.Index])r   r   r   r   )rW   r   r   zTypeIs[dd.DataFrame])rW   r   r   zTypeIs[duckdb.DuckDBPyRelation])rW   r   r   zTypeIs[ibis.Table])rW   r   r   zTypeIs[pl.DataFrame])rW   r   r   zTypeIs[pl.LazyFrame])r\   r   r   zTypeIs[pl.Series])r\   r   r   zTypeIs[pa.ChunkedArray[Any]])rW   r   r   zTypeIs[pa.Table])r   r   r   zTypeIs[pa.Scalar[Any]])rW   r   r   zTypeIs[pyspark_sql.DataFrame])rW   r   r   zTypeIs[PySparkConnectDataFrame])rW   r   r   zTypeIs[SQLFrameDataFrame])r   zAny | _NDArray[_ShapeT]r   zTypeIs[_NDArray[_ShapeT]])r   r   r   zTypeIs[_1DArray])r   r   r   zTypeIs[_1DArrayInt])r   r   r   zTypeIs[_2DArray])r   r   r   zTypeGuard[_NumpyScalar])rW   r   r   r   )r\   r   r   r   )rq   r   r   r   )r   zAny | IntoSeriesTr   zTypeIs[IntoSeriesT])r   zAny | IntoDataFrameTr   zTypeIs[IntoDataFrameT])rW   zDataFrame[IntoDataFrameT] | Anyr   z!TypeIs[DataFrame[IntoDataFrameT]])r   zAny | LazyFrame[IntoLazyFrameT]r   z!TypeIs[LazyFrame[IntoLazyFrameT]])r\   zAny | Series[IntoSeriesT]r   zTypeIs[Series[IntoSeriesT]])f
__future__r   r   typingr   r   r*   dask.dataframe	dataframerE   r=   r@   modin.pandasr$   rt   ri   r   r   r1   r   pyspark.sqlsqlr   r   r   PySparkConnectDataFrametyping_extensionsr   r	   narwhals._spark_like.dataframer
   r   r   r   r   narwhals.typingr   r   r   r   r   r   r   r   r   	frozensetrh   r!   r%   r(   r+   r/   r2   r6   r9   r;   r>   rA   rF   rI   rK   rM   rP   rY   r]   rj   rm   rr   ru   rw   ry   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   rS   rT   r[   r   __all__r   r   r    <module>r      s   # 
 %%R3@7&
 
 
  +'+
+
)
)
,
*
)
3
+
)
.,
0
8
-
NLM
OMU	VMMKRJL
 K
1
1
N	V	P"J"J%'%&%%'%&%	#>r   