o
    LhNI                     @  s*  d dl mZ d dlZd dlmZmZ d dlmZ er|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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Z0dddZ1dddZ2dddZ3dddZ4dddZ5dddZ6dddZ7dddZ8dddZ9dd d!Z:dd"d#Z;dd$d%Z<dd&d'Z=dd(d)Z>dd*d+Z?dd,d-Z@dd0d1ZAdd3d4ZBdd6d7ZCdd9d:ZDdd=d>ZEdd@dAZFddCdDZGddFdGZHddIdJZIddLdMZJddOdPZKddSdTZLddVdWZMddYdZZNdd\d]ZOdd_d`ZPddbdcZQddedfZRddhdiZSddkdlZTddndoZUddqdrZVddtduZWddwdxZXdd|d}ZYdddZZdddZ[dddZ\dddZ]dddZ^dddZ_dddZ`dddZadddZbdddZcdddZddddZedddZfg dZgdS )    )annotationsN)TYPE_CHECKINGAny)issue_warning	DataFrame)	TypeGuardTypeIs)SQLFrameDataFrame)r   	LazyFrameSeries)	IntoDataFrameTIntoLazyFrameTIntoSeriesT_1DArray_1DArrayInt_2DArray_NDArray_NumpyScalar_ShapeT	fireducksreturnr   c                   C     t jddS )z;Get Polars module (if already imported - else return None).polarsNsysmodulesget r   r   V/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/narwhals/dependencies.py
get_polars.      r!   c                   C  r   )z;Get pandas module (if already imported - else return None).pandasNr   r   r   r   r    
get_pandas3   r"   r$   c                  C  s    t jdd } du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_modin8   s   r&   c                   C  r   )z9Get cudf module (if already imported - else return None).cudfNr   r   r   r   r    get_cudf?   r"   r(   c                   C  r   )z9Get cupy module (if already imported - else return None).cupyNr   r   r   r   r    get_cupyD   r"   r*   c                   C  r   )z<Get pyarrow module (if already imported - else return None).pyarrowNr   r   r   r   r    get_pyarrowI   r"   r,   c                   C  r   )z:Get numpy module (if already imported - else return None).numpyNr   r   r   r   r    	get_numpyN   r"   r.   c                   C  r   )z2Get dask (if already imported - else return None).daskNr   r   r   r   r    get_daskS   r"   r0   c                   C  r   )zCGet dask.dataframe module (if already imported - else return None).zdask.dataframeNr   r   r   r   r    get_dask_dataframeX   r"   r1   c                   C  r   )z;Get duckdb module (if already imported - else return None).duckdbNr   r   r   r   r    
get_duckdb]   r"   r3   c                   C  r   )z9Get ibis module (if already imported - else return None).ibisNr   r   r   r   r    get_ibisb   r"   r5   c                  C  s,   t   } durt| dr| jS tjddS )z>Get dask_expr module (if already imported - else return None).N	dask_expr)r1   hasattrr6   r   r   r   )ddr   r   r    get_dask_exprg   s   r9   c                   C  r   )z<Get pyspark module (if already imported - else return None).pysparkNr   r   r   r   r    get_pysparkn   r"   r;   c                   C  r   )z@Get pyspark.sql module (if already imported - else return None).zpyspark.sqlNr   r   r   r   r    get_pyspark_sqls   r"   r<   c                   C  r   )zHGet pyspark.sql.connect module (if already imported - else return None).zpyspark.sql.connectNr   r   r   r   r    get_pyspark_connectx   r"   r=   c                   C  r   )z=Get sqlframe module (if already imported - else return None).sqlframeNr   r   r   r   r    get_sqlframe}   r"   r?   dfNonec                 C  s2   t | st| rdt|  d}t|t d S d S )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typer   UserWarning)r@   msgr   r   r    _warn_if_narwhals_df_or_lf   s
   rH   serc                 C  s*   t | rdt|  d}t|t d S d S )NrB   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_seriesrE   r   rF   )rI   rG   r   r   r    _warn_if_narwhals_series   s
   rK   TypeIs[pd.DataFrame]c                   s:   t   t  }durt |jpt fd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  4    | ]}t j|d  d uot jjV  qd S N)r   r   r   
isinstancer#   r   .0module_namer@   modr   r    	<genexpr>       
z&is_pandas_dataframe.<locals>.<genexpr>)rH   r$   rO   r   anyIMPORT_HOOKS)r@   pdr   rS   r    is_pandas_dataframe      (rZ   TypeIs[pd.Series[Any]]c                   s:   t  t  }durt|jpt fd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  s4    | ]}t j|d   d uot jjV  qd S rN   )r   r   r   rO   r#   r   rP   rT   rI   r   r    rU      rV   z#is_pandas_series.<locals>.<genexpr>)rK   r$   rO   r   rW   rX   )rI   rY   r   r]   r    is_pandas_series   r[   r^   indexTypeIs[pd.Index[Any]]c                   s2   t   }durt |jpt fddtD S )zACheck whether `index` is a pandas Index without importing pandas.Nc                 3  rM   rN   )r   r   r   rO   r#   IndexrP   r_   rT   r   r    rU      rV   z"is_pandas_index.<locals>.<genexpr>)r$   rO   ra   rW   rX   )r_   rY   r   rb   r    is_pandas_index   s   (rc   TypeIs[mpd.DataFrame]c                 C  "   t |  t  }duo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)rH   r&   rO   r   )r@   mpdr   r   r    is_modin_dataframe      rg   TypeIs[mpd.Series]c                 C  re   )zCheck whether `ser` is a modin Series without importing modin.

    Warning:
        This method cannot be called on Narwhals Series.
    N)rK   r&   rO   r   )rI   rf   r   r   r    is_modin_series   rh   rj   TypeIs[mpd.Index[Any]]c                 C     t   }duot| |jS )z?Check whether `index` is a modin Index without importing modin.N)r&   rO   ra   )r_   rf   r   r   r    is_modin_index      rm   TypeIs[cudf.DataFrame]c                 C  re   )zCheck whether `df` is a cudf DataFrame without importing cudf.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rH   r(   rO   r   )r@   r'   r   r   r    is_cudf_dataframe   rh   rp   TypeIs[cudf.Series[Any]]c                 C  re   )zCheck whether `ser` is a cudf Series without importing cudf.

    Warning:
        This method cannot be called on Narwhals Series.
    N)rK   r(   rO   r   )rI   r'   r   r   r    is_cudf_series   rh   rr   TypeIs[cudf.Index]c                 C  rl   )z=Check whether `index` is a cudf Index without importing cudf.N)r(   rO   ra   )r_   r'   r   r   r    is_cudf_index   s   rt   objboolc                 C  s$   t   }d uot| |jo| jdkS )N   )r*   rO   ndarraysize)ru   r)   r   r   r    is_cupy_scalar   s
   
rz   TypeIs[dd.DataFrame]c                 C  re   )zCheck whether `df` is a Dask DataFrame without importing Dask.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rH   r1   rO   r   )r@   r8   r   r   r    is_dask_dataframe   rh   r|   TypeIs[duckdb.DuckDBPyRelation]c                 C  re   )zCheck whether `df` is a DuckDB Relation without importing DuckDB.

    Warning:
        This method cannot be called on Narwhals DataFrame/LazyFrame.
    N)rH   r3   rO   DuckDBPyRelation)r@   r2   r   r   r    is_duckdb_relation  s   r   TypeIs[ibis.Table]c                 C  s&   t |  t  }duo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)rH   r5   rO   exprtypesTable)r@   r4   r   r   r    is_ibis_table  s   r   TypeIs[pl.DataFrame]c                 C  re   )zCheck whether `df` is a Polars DataFrame without importing Polars.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    N)rH   r!   rO   r   r@   plr   r   r    is_polars_dataframe  rh   r   TypeIs[pl.LazyFrame]c                 C  re   )zCheck whether `df` is a Polars LazyFrame without importing Polars.

    Warning:
        This method cannot be called on Narwhals DataFrame/LazyFrame.
    N)rH   r!   rO   r   r   r   r   r    is_polars_lazyframe!  rh   r   TypeIs[pl.Series]c                 C  re   )zCheck whether `ser` is a Polars Series without importing Polars.

    Warning:
        This method cannot be called on Narwhals Series.
    N)rK   r!   rO   r   )rI   r   r   r   r    is_polars_series+  rh   r   TypeIs[pa.ChunkedArray[Any]]c                 C  re   )zCheck whether `ser` is a PyArrow ChunkedArray without importing PyArrow.

    Warning:
        This method cannot be called on Narwhals Series.
    N)rK   r,   rO   ChunkedArray)rI   par   r   r    is_pyarrow_chunked_array5  rh   r   TypeIs[pa.Table]c                 C  re   )zCheck whether `df` is a PyArrow Table without importing PyArrow.

    Warning:
        This method cannot be called on Narwhals DataFrame/LazyFrame.
    N)rH   r,   rO   r   )r@   r   r   r   r    is_pyarrow_table?  rh   r   TypeIs[pa.Scalar[Any]]c                 C  s   t   }d uot| |jS rN   )r,   rO   Scalar)ru   r   r   r   r    is_pyarrow_scalarI  s   r   TypeIs[pyspark_sql.DataFrame]c                 C  s&   t |  tt  }duo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)rH   rv   r<   rO   r   )r@   pyspark_sqlr   r   r    is_pyspark_dataframeM  s
   
r   TypeIs[PySparkConnectDataFrame]c                 C  sF   t |  t dur!zddlm} W n
 ty   Y dS w t| |S dS )zCheck whether `df` is a PySpark Connect DataFrame without importing PySpark.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    Nr   r   F)rH   r=   pyspark.sql.connect.dataframer   ImportErrorrO   r@   r   r   r   r    is_pyspark_connect_dataframeZ  s   

r   TypeIs[SQLFrameDataFrame]c                 C  s,   t |  t durddlm} t| |S dS )zCheck whether `df` is a SQLFrame DataFrame without importing SQLFrame.

    Warning:
        This method cannot be called on a Narwhals DataFrame/LazyFrame.
    Nr   )BaseDataFrameF)rH   r?   sqlframe.base.dataframer   rO   )r@   r   r   r   r    is_sqlframe_dataframej  s
   

r   arrAny | _NDArray[_ShapeT]TypeIs[_NDArray[_ShapeT]]c                 C  rl   )z=Check whether `arr` is a NumPy Array without importing NumPy.N)r.   rO   rx   r   npr   r   r    is_numpy_arrayx  rn   r   TypeIs[_1DArray]c                 C     t | o| jdkS )z@Check whether `arr` is a 1D NumPy Array without importing NumPy.rw   r   ndimr   r   r   r    is_numpy_array_1d}     r   TypeIs[_1DArrayInt]c                 C  s"   t   }ot| o|| j|jS rN   )r.   r   
issubdtypedtypeintegerr   r   r   r    is_numpy_array_1d_int  s
   
r   TypeIs[_2DArray]c                 C  r   )z@Check whether `arr` is a 2D NumPy Array without importing NumPy.   r   r   r   r   r    is_numpy_array_2d  r   r   scalarTypeGuard[_NumpyScalar]c                 C  rl   )zACheck whether `scalar` is a NumPy Scalar without importing NumPy.N)r.   rO   generic)r   r   r   r   r    is_numpy_scalar  s   r   c                 C      t |  t| pt| p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.
    )rH   rZ   rg   rp   )r@   r   r   r    is_pandas_like_dataframe     r   c                 C  r   )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.
    )rK   r^   rj   rr   rI   r   r   r    is_pandas_like_series  r   r   c                 C  s   t | pt| pt| S )zCheck whether `index` is a pandas-like Index without doing any imports.

    By "pandas-like", we mean: pandas, Modin, cuDF.
    )rc   rm   rt   )r_   r   r   r    is_pandas_like_index  s   r   native_seriesAny | IntoSeriesTTypeIs[IntoSeriesT]c                 C  8   ddl m} t| |pt| dpt| pt| p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   rO   r7   r   r   r   )r   r   r   r   r    is_into_series     
r   native_dataframeAny | IntoDataFrameTTypeIs[IntoDataFrameT]c                 C  r   )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   rO   r7   r   r   r   )r   r   r   r   r    is_into_dataframe  r   r   DataFrame[IntoDataFrameT] | Any!TypeIs[DataFrame[IntoDataFrameT]]c                 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   rO   r   r   r   r    rC        	
rC   lfAny | LazyFrame[IntoLazyFrameT]!TypeIs[LazyFrame[IntoLazyFrameT]]c                 C  r   )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   rO   )r   r   r   r   r    rD     r   rD   Any | Series[IntoSeriesT]TypeIs[Series[IntoSeriesT]]c                 C  r   )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   rO   )rI   r   r   r   r    rJ     s   
rJ   c                 C  s   t | o| j S rN   )rJ   r   
is_integerr   r   r   r    is_narwhals_series_int+  s   r   )r(   r5   r&   r.   r$   r!   r,   rp   rr   r|   r   r   r   rg   rj   rC   rD   rJ   r   rZ   rc   r   r   r^   r   r   r   r   r   )r   r   )r@   r   r   rA   )rI   r   r   rA   )r@   r   r   rL   )rI   r   r   r\   )r_   r   r   r`   )r@   r   r   rd   )rI   r   r   ri   )r_   r   r   rk   )r@   r   r   ro   )rI   r   r   rq   )r_   r   r   rs   )ru   r   r   rv   )r@   r   r   r{   )r@   r   r   r}   )r@   r   r   r   )r@   r   r   r   )r@   r   r   r   )rI   r   r   r   )rI   r   r   r   )r@   r   r   r   )ru   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   )r   r   r   r   )r   r   r   r   )r@   r   r   rv   )rI   r   r   rv   )r_   r   r   rv   )r   r   r   r   )r   r   r   r   )r@   r   r   r   )r   r   r   r   )rI   r   r   r   )h
__future__r   r   typingr   r   narwhals._exceptionsr   r'   dask.dataframe	dataframer8   r2   r4   modin.pandasr#   rf   rY   r   r   r+   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   	frozensetrX   r!   r$   r&   r(   r*   r,   r.   r0   r1   r3   r5   r9   r;   r<   r=   r?   rH   rK   rZ   r^   rc   rg   rj   rm   rp   rr   rt   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rC   rD   rJ   r   __all__r   r   r   r    <module>   s   ,
























	







































%
%


