U
     )3g†'  ã                   @  s  d dl mZ d dlZd dlmZ d dlmZ er°d dlZejdkrPd dlm	Z	 nd dl
m	Z	 d 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	œd
d„Zdd	œdd„Zdd	œdd„Zdd	œdd„Zdd	œdd„Zdd	œdd„Z dd	œdd„Z!dd	œdd„Z"dd	œdd„Z#dd	œdd„Z$dd	œdd„Z%dd	œd d!„Z&dd"d#œd$d%„Z'dd&d'œd(d)„Z(dd*d+œd,d-„Z)dd.d#œd/d0„Z*dd1d'œd2d3„Z+dd4d+œd5d6„Z,dd7d#œd8d9„Z-dd:d'œd;d<„Z.dd=d+œd>d?„Z/dd@d#œdAdB„Z0ddCd#œdDdE„Z1ddFd#œdGdH„Z2ddId#œdJdK„Z3ddLd#œdMdN„Z4ddOd'œdPdQ„Z5ddRd'œdSdT„Z6ddUd#œdVdW„Z7ddXdYœdZd[„Z8dd\d#œd]d^„Z9dd\d'œd_d`„Z:dd\d+œdadb„Z;dcd\ddœdedf„Z<dd\dgœdhdi„Z=ddddddd!dHd%d)dKdNdQd0d3d9d<dWdTd[dBd^d`didfgZ>dS )jé    )ÚannotationsN)ÚTYPE_CHECKING)ÚAny)é   é
   )Ú	TypeGuard)Ú
IntoSeriesr   )Úreturnc                   C  s   t j dd¡S )z;Get Polars module (if already imported - else return None).ÚpolarsN©ÚsysÚmodulesÚget© r   r   ú9/tmp/pip-unpacked-wheel-hfsjijke/narwhals/dependencies.pyÚ
get_polars   s    r   c                   C  s   t j dd¡S )z;Get pandas module (if already imported - else return None).ÚpandasNr   r   r   r   r   Ú
get_pandas"   s    r   c                  C  s    t j dd¡ } dk	r| jS dS )zAGet modin.pandas module (if already imported - else return None).ÚmodinN)r   r   r   r   )r   r   r   r   Ú	get_modin'   s    r   c                   C  s   t j dd¡S )z9Get cudf module (if already imported - else return None).ÚcudfNr   r   r   r   r   Úget_cudf.   s    r   c                   C  s   t j dd¡S )z9Get cupy module (if already imported - else return None).ZcupyNr   r   r   r   r   Úget_cupy3   s    r   c                   C  s   t j dd¡S )z<Get pyarrow module (if already imported - else return None).ÚpyarrowNr   r   r   r   r   Úget_pyarrow8   s    r   c                   C  s   t j dd¡S )z:Get numpy module (if already imported - else return None).ÚnumpyNr   r   r   r   r   Ú	get_numpy=   s    r   c                   C  s   t j dd¡S )z2Get dask (if already imported - else return None).ZdaskNr   r   r   r   r   Úget_daskB   s    r   c                   C  s   t j dd¡S )zCGet dask.dataframe module (if already imported - else return None).zdask.dataframeNr   r   r   r   r   Úget_dask_dataframeG   s    r   c                   C  s   t j dd¡S )z;Get duckdb module (if already imported - else return None).ÚduckdbNr   r   r   r   r   Ú
get_duckdbL   s    r    c                   C  s   t j dd¡S )z>Get dask_expr module (if already imported - else return None).Z	dask_exprNr   r   r   r   r   Úget_dask_exprQ   s    r!   c                   C  s   t j dd¡S )z9Get ibis module (if already imported - else return None).ÚibisNr   r   r   r   r   Úget_ibisV   s    r#   zTypeGuard[pd.DataFrame])Údfr	   c                 C  s   t ƒ  }dk	ot| |jƒS )zBCheck whether `df` is a pandas DataFrame without importing pandas.N)r   Ú
isinstanceÚ	DataFrame)r$   Úpdr   r   r   Úis_pandas_dataframe[   s    r(   zTypeGuard[pd.Series[Any]])Úserr	   c                 C  s   t ƒ  }dk	ot| |jƒS )z@Check whether `ser` is a pandas Series without importing pandas.N)r   r%   ÚSeries)r)   r'   r   r   r   Úis_pandas_series`   s    r+   zTypeGuard[pd.Index])Úindexr	   c                 C  s   t ƒ  }dk	ot| |jƒS )zACheck whether `index` is a pandas Index without importing pandas.N)r   r%   ÚIndex)r,   r'   r   r   r   Úis_pandas_indexe   s    r.   zTypeGuard[mpd.DataFrame]c                 C  s   t ƒ  }dk	ot| |jƒS )z@Check whether `df` is a modin DataFrame without importing modin.N)r   r%   r&   )r$   Úmpdr   r   r   Úis_modin_dataframej   s    r0   zTypeGuard[mpd.Series]c                 C  s   t ƒ  }dk	ot| |jƒS )z>Check whether `ser` is a modin Series without importing modin.N)r   r%   r*   )r)   r/   r   r   r   Úis_modin_serieso   s    r1   zTypeGuard[mpd.Index]c                 C  s   t ƒ  }dk	ot| |jƒS )z?Check whether `index` is a modin Index without importing modin.N)r   r%   r-   )r,   r/   r   r   r   Úis_modin_indext   s     ÿr2   zTypeGuard[cudf.DataFrame]c                 C  s   t ƒ  }dk	ot| |jƒS )z>Check whether `df` is a cudf DataFrame without importing cudf.N)r   r%   r&   )r$   r   r   r   r   Úis_cudf_dataframe{   s    r3   zTypeGuard[cudf.Series[Any]]c                 C  s   t ƒ  }dk	ot| |jƒS )z<Check whether `ser` is a cudf Series without importing cudf.N)r   r%   r*   )r)   r   r   r   r   Úis_cudf_series€   s    r4   zTypeGuard[cudf.Index]c                 C  s   t ƒ  }dk	ot| |jƒS )z=Check whether `index` is a cudf Index without importing cudf.N)r   r%   r-   )r,   r   r   r   r   Úis_cudf_index…   s     ÿr5   zTypeGuard[dd.DataFrame]c                 C  s   t ƒ  }dk	ot| |jƒS )z>Check whether `df` is a Dask DataFrame without importing Dask.N)r   r%   r&   )r$   Úddr   r   r   Úis_dask_dataframeŒ   s    r7   z"TypeGuard[duckdb.DuckDBPyRelation]c                 C  s   t ƒ  }dk	ot| |jƒS )zACheck whether `df` is a DuckDB Relation without importing DuckDB.N)r    r%   ZDuckDBPyRelation)r$   r   r   r   r   Úis_duckdb_relation‘   s     ÿr8   zTypeGuard[ibis.Table]c                 C  s   t ƒ  }dk	ot| |jjjƒS )z:Check whether `df` is a Ibis Table without importing Ibis.N)r#   r%   ÚexprÚtypesÚTable)r$   r"   r   r   r   Úis_ibis_table˜   s    r<   zTypeGuard[pl.DataFrame]c                 C  s   t ƒ  }dk	ot| |jƒS )zBCheck whether `df` is a Polars DataFrame without importing Polars.N)r   r%   r&   ©r$   Úplr   r   r   Úis_polars_dataframe   s    r?   zTypeGuard[pl.LazyFrame]c                 C  s   t ƒ  }dk	ot| |jƒS )zBCheck whether `df` is a Polars LazyFrame without importing Polars.N)r   r%   Z	LazyFramer=   r   r   r   Úis_polars_lazyframe¢   s    r@   zTypeGuard[pl.Series]c                 C  s   t ƒ  }dk	ot| |jƒS )z@Check whether `ser` is a Polars Series without importing Polars.N)r   r%   r*   )r)   r>   r   r   r   Úis_polars_series§   s    rA   zTypeGuard[pa.ChunkedArray]c                 C  s   t ƒ  }dk	ot| |jƒS )zHCheck whether `ser` is a PyArrow ChunkedArray without importing PyArrow.N)r   r%   ZChunkedArray)r)   Úpar   r   r   Úis_pyarrow_chunked_array¬   s    rC   zTypeGuard[pa.Table]c                 C  s   t ƒ  }dk	ot| |jƒS )z@Check whether `df` is a PyArrow Table without importing PyArrow.N)r   r%   r;   )r$   rB   r   r   r   Úis_pyarrow_table±   s    rD   zTypeGuard[np.ndarray])Úarrr	   c                 C  s   t ƒ  }dk	ot| |jƒS )z=Check whether `arr` is a NumPy Array without importing NumPy.N)r   r%   Zndarray)rE   Únpr   r   r   Úis_numpy_array¶   s    rG   Úboolc                 C  s   t | ƒpt| ƒpt| ƒS )z†
    Check whether `df` is a pandas-like DataFrame without doing any imports

    By "pandas-like", we mean: pandas, Modin, cuDF.
    )r(   r0   r3   )r$   r   r   r   Úis_pandas_like_dataframe»   s    rI   c                 C  s   t | ƒpt| ƒpt| ƒS )z„
    Check whether `ser` is a pandas-like Series without doing any imports

    By "pandas-like", we mean: pandas, Modin, cuDF.
    )r+   r1   r4   )r)   r   r   r   Úis_pandas_like_seriesÄ   s    rJ   c                 C  s   t | ƒpt| ƒpt| ƒS )z…
    Check whether `index` is a pandas-like Index without doing any imports

    By "pandas-like", we mean: pandas, Modin, cuDF.
    )r.   r2   r5   )r,   r   r   r   Úis_pandas_like_indexÍ   s    ÿrK   r   )Únative_seriesr	   c                 C  s8   ddl m} t| |ƒp6t| dƒp6t| ƒp6t| ƒp6t| ƒ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*   Z__narwhals_series__)Znarwhals.seriesr*   r%   ÚhasattrrA   rC   rJ   )rL   r*   r   r   r   Úis_into_seriesØ   s    
ÿþýûrN   )Únative_dataframer	   c                 C  s8   ddl m} t| |ƒp6t| dƒp6t| ƒp6t| ƒp6t| ƒ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&   Z__narwhals_dataframe__)Znarwhals.dataframer&   r%   rM   r?   rD   rI   )rO   r&   r   r   r   Úis_into_dataframeþ   s    
ÿþýûrP   )?Ú
__future__r   r   Útypingr   r   r   rF   Úversion_infor   Ztyping_extensionsr   Zdask.dataframeZ	dataframer6   r   r"   Zmodin.pandasr   r/   r'   r
   r>   r   rB   Znarwhals.typingr   r   r   r   r   r   r   r   r   r   r    r!   r#   r(   r+   r.   r0   r1   r2   r3   r4   r5   r7   r8   r<   r?   r@   rA   rC   rD   rG   rI   rJ   rK   rN   rP   Ú__all__r   r   r   r   Ú<module>   sœ   
		&'ç