o
    $&]iQ0                     @  s  d Z ddlmZ ddlmZ ddlmZ ddl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mZmZmZmZ e
rpdd	lmZ dd
l	mZm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&m'Z'm(Z( dgZ)G dd dee*df Z+dS )zxSchema.

Adapted from Polars implementation at:
https://github.com/pola-rs/polars/blob/main/py-polars/polars/schema.py.
    )annotations)OrderedDict)Mapping)partial)TYPE_CHECKINGcast)ImplementationVersionqualified_type_name
zip_strict)get_cudfis_cudf_dtypeis_pandas_like_dtypeis_polars_data_typeis_polars_schemais_pyarrow_data_typeis_pyarrow_schema)Iterable)AnyClassVarN)Self)DType)DTypeBackendIntoArrowSchemaIntoPandasSchemaIntoPolarsSchemaSchemac                      s   e Zd ZU dZejZded< 	d4d5 fd	d
Zd6ddZ	d7ddZ
d8ddZed9ddZed:ddZed;ddZed<dd Zd=d"d#Z	d4d>d'd(Zd?d*d+Zed@d.d/ZedAd2d3Z  ZS )Br   a<  Ordered mapping of column names to their data type.

    Arguments:
        schema: The schema definition given by column names and their associated
            *instantiated* Narwhals data type. Accepts a mapping or an iterable of tuples.

    Examples:
        Define a schema by passing *instantiated* data types.

        >>> import narwhals as nw
        >>> schema = nw.Schema({"foo": nw.Int8(), "bar": nw.String()})
        >>> schema
        Schema({'foo': Int8, 'bar': String})

        Access the data type associated with a specific column name.

        >>> schema["foo"]
        Int8

        Access various schema properties using the `names`, `dtypes`, and `len` methods.

        >>> schema.names()
        ['foo', 'bar']
        >>> schema.dtypes()
        [Int8, String]
        >>> schema.len()
        2
    zClassVar[Version]_versionNschema8Mapping[str, DType] | Iterable[tuple[str, DType]] | NonereturnNonec                   s   |pi }t  | d S N)super__init__)selfr   	__class__ M/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/schema.pyr$   M   s   zSchema.__init__	list[str]c                 C     t |  S )z#Get the column names of the schema.)listkeysr%   r(   r(   r)   namesS      zSchema.nameslist[DType]c                 C  r+   )z!Get the data types of the schema.)r,   valuesr.   r(   r(   r)   dtypesW   r0   zSchema.dtypesintc                 C  s   t | S )z(Get the number of columns in the schema.)lenr.   r(   r(   r)   r5   [   s   z
Schema.lenr   r   c                  sJ   t |tr|s
  S ddl}||}ddlm   fdd|D S )a  Construct a Schema from a pyarrow Schema.

        Arguments:
            schema: A pyarrow Schema or mapping of column names to pyarrow data types.

        Examples:
            >>> import pyarrow as pa
            >>> import narwhals as nw
            >>>
            >>> mapping = {
            ...     "a": pa.timestamp("us", "UTC"),
            ...     "b": pa.date32(),
            ...     "c": pa.string(),
            ...     "d": pa.uint8(),
            ... }
            >>> native = pa.schema(mapping)
            >>>
            >>> nw.Schema.from_arrow(native)
            Schema({'a': Datetime(time_unit='us', time_zone='UTC'), 'b': Date, 'c': String, 'd': UInt8})

            >>> nw.Schema.from_arrow(mapping) == nw.Schema.from_arrow(native)
            True
        r   Nnative_to_narwhals_dtypec                 3  s$    | ]}|j |j jfV  qd S r"   )nametyper   ).0fieldclsr7   r(   r)   	<genexpr>   s
    
z$Schema.from_arrow.<locals>.<genexpr>)
isinstancer   pyarrowr   narwhals._arrow.utilsr7   )r=   r   par(   r<   r)   
from_arrow_   s   

zSchema.from_arrowr   c                C  s>   |s|  S t  rtdd | D rtjntj}| ||S )a3  Construct a Schema from a pandas-like schema representation.

        Arguments:
            schema: A mapping of column names to pandas-like data types.

        Examples:
            >>> import numpy as np
            >>> import pandas as pd
            >>> import pyarrow as pa
            >>> import narwhals as nw
            >>>
            >>> data = {"a": [1], "b": ["a"], "c": [False], "d": [9.2]}
            >>> native = pd.DataFrame(data).convert_dtypes().dtypes.to_dict()
            >>>
            >>> nw.Schema.from_pandas_like(native)
            Schema({'a': Int64, 'b': String, 'c': Boolean, 'd': Float64})
            >>>
            >>> mapping = {
            ...     "a": pd.DatetimeTZDtype("us", "UTC"),
            ...     "b": pd.ArrowDtype(pa.date32()),
            ...     "c": pd.StringDtype("python"),
            ...     "d": np.dtype("uint8"),
            ... }
            >>>
            >>> nw.Schema.from_pandas_like(mapping)
            Schema({'a': Datetime(time_unit='us', time_zone='UTC'), 'b': Date, 'c': String, 'd': UInt8})
        c                 s  s    | ]}t |V  qd S r"   )r   )r:   dtyper(   r(   r)   r>      s    z*Schema.from_pandas_like.<locals>.<genexpr>)r   anyr2   r   CUDFPANDAS_from_pandas_like)r=   r   implr(   r(   r)   from_pandas_like   s   zSchema.from_pandas_like5IntoArrowSchema | IntoPolarsSchema | IntoPandasSchemac                C  s^   t |r	| |S t|r| |S t|tr!|r| |S |  S dt|d|}t|)ao  Construct a Schema from a native schema representation.

        Arguments:
            schema: A native schema object, or mapping of column names to
                *instantiated* native data types.

        Examples:
            >>> import datetime as dt
            >>> import pyarrow as pa
            >>> import narwhals as nw
            >>>
            >>> data = {"a": [1], "b": ["a"], "c": [dt.time(1, 2, 3)], "d": [[2]]}
            >>> native = pa.table(data).schema
            >>>
            >>> nw.Schema.from_native(native)
            Schema({'a': Int64, 'b': String, 'c': Time, 'd': List(Int64)})
        z5Expected an arrow, polars, or pandas schema, but got z

)	r   rC   r   from_polarsr?   r   _from_native_mappingr
   	TypeError)r=   r   msgr(   r(   r)   from_native   s   


zSchema.from_nativer   c                  s2   |s  S ddl m   fdd| D S )a/  Construct a Schema from a polars Schema.

        Arguments:
            schema: A polars Schema or mapping of column names to *instantiated*
                polars data types.

        Examples:
            >>> import polars as pl
            >>> import narwhals as nw
            >>>
            >>> mapping = {
            ...     "a": pl.Datetime(time_zone="UTC"),
            ...     "b": pl.Date(),
            ...     "c": pl.String(),
            ...     "d": pl.UInt8(),
            ... }
            >>> native = pl.Schema(mapping)
            >>>
            >>> nw.Schema.from_polars(native)
            Schema({'a': Datetime(time_unit='us', time_zone='UTC'), 'b': Date, 'c': String, 'd': UInt8})

            >>> nw.Schema.from_polars(mapping) == nw.Schema.from_polars(native)
            True
        r   r6   c                 3  s$    | ]\}}|| j fV  qd S r"   r   r:   r8   rD   r<   r(   r)   r>      
    
z%Schema.from_polars.<locals>.<genexpr>)narwhals._polars.utilsr7   items)r=   r   r(   r<   r)   rL      s   zSchema.from_polars	pa.Schemac                   s2   ddl }ddlm  | fdd D S )a  Convert Schema to a pyarrow Schema.

        Examples:
            >>> import narwhals as nw
            >>> schema = nw.Schema({"a": nw.Int64(), "b": nw.Datetime("ns")})
            >>> schema.to_arrow()
            a: int64
            b: timestamp[ns]
        r   Nnarwhals_to_native_dtypec                 3  $    | ]\}}| |j fV  qd S r"   rQ   rR   rX   r%   r(   r)   r>      rS   z"Schema.to_arrow.<locals>.<genexpr>)r@   rA   rX   r   rU   )r%   rB   r(   rZ   r)   to_arrow   s
   
zSchema.to_arrowdtype_backend%DTypeBackend | Iterable[DTypeBackend]dict[str, Any]c                   s   ddl m} t|tj| jd du st tr$ fdd|  D S t	 }t
|t
| krjddlm}m}m} t
|t
| }}t	|||||||}	d|d	|d
| d|d  d|	 d}
t|
fddt|  |  |D S )am  Convert Schema to an ordered mapping of column names to their pandas data type.

        Arguments:
            dtype_backend: Backend(s) used for the native types. When providing more than
                one, the length of the iterable must be equal to the length of the schema.

        Examples:
            >>> import narwhals as nw
            >>> schema = nw.Schema({"a": nw.Int64(), "b": nw.Datetime("ns")})
            >>> schema.to_pandas()
            {'a': 'int64', 'b': 'datetime64[ns]'}

            >>> schema.to_pandas("pyarrow")
            {'a': 'Int64[pyarrow]', 'b': 'timestamp[ns][pyarrow]'}
        r   rW   )implementationversionNc                   s   i | ]\}}|| d qS )rD   r\   r(   rR   r\   to_native_dtyper(   r)   
<dictcomp>  s    z$Schema.to_pandas.<locals>.<dictcomp>)chainislicerepeatz	Provided z) `dtype_backend`(s), but schema contains z1 field(s).
Hint: instead of
    schema.to_pandas(z+)
you may want to use
    schema.to_pandas(z)
or
    schema.to_pandas()c                   s    i | ]\}}}| ||d qS ra   r(   )r:   r8   rD   backend)rc   r(   r)   rd   2  s    )narwhals._pandas_like.utilsrX   r   r   rG   r   r?   strrU   tupler5   	itertoolsre   rf   rg   from_iterable
ValueErrorr   r-   r2   )r%   r\   rX   backendsre   rf   rg   n_usern_actual
suggestionrO   r(   rb   r)   	to_pandas  s:   	
zSchema.to_pandas	pl.Schemac                   sV   ddl }ddlm  tj } fdd D }|dkr$||S tdt	|S )a%  Convert Schema to a polars Schema.

        Examples:
            >>> import narwhals as nw
            >>> schema = nw.Schema({"a": nw.Int64(), "b": nw.Datetime("ns")})
            >>> schema.to_polars()
            Schema({'a': Int64, 'b': Datetime(time_unit='ns', time_zone=None)})
        r   NrW   c                 3  rY   r"   rQ   rR   rZ   r(   r)   r>   E  rS   z#Schema.to_polars.<locals>.<genexpr>)   r   r   ru   )
polarsrT   rX   r   POLARS_backend_versionrU   r   r   dict)r%   pl
pl_versionr   r(   rZ   r)   	to_polars7  s   	
zSchema.to_polarsnativeHMapping[str, pa.DataType] | Mapping[str, pl.DataType] | IntoPandasSchemac                C  s   t t| }|\}}t|r| td|S t|r$| td|S t|r0| 	td|S d| dt
| d|}t|)Nr   r   r   z7Expected an arrow, polars, or pandas dtype, but found `z: z`

)nextiterrU   r   rL   r   r   rJ   r   rC   r
   rN   )r=   r~   
first_item	first_keyfirst_dtyperO   r(   r(   r)   rM   O  s"   zSchema._from_native_mappingr_   r   c                  s.   ddl m |  fdd| D S )Nr   r6   c                 3  s*    | ]\}}|| j d dfV  qdS )T)allow_objectNrQ   rR   r=   rI   r7   r(   r)   r>   j  s
    
z+Schema._from_pandas_like.<locals>.<genexpr>)rj   r7   rU   )r=   r   r_   r(   r   r)   rH   c  s
   zSchema._from_pandas_liker"   )r   r   r    r!   )r    r*   )r    r1   )r    r4   )r   r   r    r   )r   r   r    r   )r   rK   r    r   )r   r   r    r   )r    rV   )r\   r]   r    r^   )r    ru   )r~   r   r    r   )r   r   r_   r   r    r   )__name__
__module____qualname____doc__r	   MAINr   __annotations__r$   r/   r3   r5   classmethodrC   rJ   rP   rL   r[   rt   r}   rM   rH   __classcell__r(   r(   r&   r)   r   -   s0   
 


%% 
"
5r   ),r   
__future__r   collectionsr   collections.abcr   	functoolsr   typingr   r   narwhals._utilsr   r	   r
   r   narwhals.dependenciesr   r   r   r   r   r   r   r   r   r   rw   r{   r@   rB   typing_extensionsr   narwhals.dtypesr   narwhals.typingr   r   r   r   __all__rk   r   r(   r(   r(   r)   <module>   s$    $
