o
    hQ                     @  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	 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 ergddlmZ ddlmZ ddlZddlZddlmZ ddlmZ eeef ZneZdgZG dd deZdS )zxSchema.

Adapted from Polars implementation at:
https://github.com/pola-rs/polars/blob/main/py-polars/polars/schema.py.
    )annotations)OrderedDict)partial)TYPE_CHECKING)Iterable)Mapping)cast)Implementation)Version)parse_version)Any)ClassVarN)DType)DTypeBackendSchemac                      sv   e Zd ZU dZejZded< 	dd  fd	d
Zd!ddZ	d"ddZ
d#ddZd$ddZ	dd%ddZd&ddZ  ZS )'r   a  Ordered mapping of column names to their data type.

    Arguments:
        schema: Mapping[str, DType] | Iterable[tuple[str, DType]] | None
            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__ C/var/www/vscode/kcb/lib/python3.10/site-packages/narwhals/schema.pyr   G   s   zSchema.__init__	list[str]c                 C     t |  S )zXGet the column names of the schema.

        Returns:
            Column names.
        )listkeysr   r   r   r   namesN      zSchema.nameslist[DType]c                 C  r   )z^Get the data types of the schema.

        Returns:
            Data types of schema.
        )r    valuesr"   r   r   r   dtypesV   r$   zSchema.dtypesintc                 C  s   t | S )zbGet the number of columns in the schema.

        Returns:
            Number of columns.
        )lenr"   r   r   r   r)   ^   s   z
Schema.len	pa.Schemac                   s2   ddl }ddlm  | fdd D S )a7  Convert Schema to a pyarrow Schema.

        Returns:
            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  s$    | ]\}}| |j fV  qd S r   r   .0namedtyper,   r   r   r   	<genexpr>w   s
    
z"Schema.to_arrow.<locals>.<genexpr>)pyarrownarwhals._arrow.utilsr,   r   items)r   par   r2   r   to_arrowf   s
   zSchema.to_arrowdtype_backend%DTypeBackend | Iterable[DTypeBackend]dict[str, Any]c                   s  ddl }ddlm} t|tjt|| jd du st t	r+ fdd| 
 D S t }t|t| kryddlm} ddlm} dd	lm} t|t| }}	t||||||	|	}
d
|d|	d| d|d  d|
 d}t|fddt|  |  |D S )a  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.

        Returns:
            An ordered mapping of column names to their pandas data type.

        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   Nr+   )implementationbackend_versionversionc                   s   i | ]\}}|| d qS )r1   r9   r   r.   r9   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 r?   r   )r/   r0   r1   backend)rA   r   r   rB      s    )pandasnarwhals._pandas_like.utilsr,   r   r	   PANDASr   r   
isinstancestrr6   tupler)   	itertoolsrC   rD   rE   from_iterable
ValueErrorzipr!   r&   )r   r9   pdr,   backendsrC   rD   rE   n_usern_actual
suggestionmsgr   r@   r   	to_pandas|   sF   	
zSchema.to_pandas	pl.Schemac                   sV   ddl }ddlm  t| fdd D }dkr$||S tdt|S )ax  Convert Schema to a polars Schema.

        Returns:
            A polars Schema or plain dict (prior to polars 1.0).

        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   Nr+   c                 3  s(    | ]\}}| |j d fV  qdS ))r=   Nr-   r.   r,   
pl_versionr   r   r   r3      s    
z#Schema.to_polars.<locals>.<genexpr>)   r   r   rY   )polarsnarwhals._polars.utilsr,   r   r6   r   r   dict)r   plr   r   rZ   r   	to_polars   s   zSchema.to_polarsr   )r   r   r   r   )r   r   )r   r%   )r   r(   )r   r*   )r9   r:   r   r;   )r   rY   )__name__
__module____qualname____doc__r
   MAINr   __annotations__r   r#   r'   r)   r8   rX   ra   __classcell__r   r   r   r   r   &   s   
 



@)re   
__future__r   collectionsr   	functoolsr   typingr   r   r   r   narwhals.utilsr	   r
   r   r   r   r]   r`   r4   r7   narwhals.dtypesr   narwhals.typingr   rL   
BaseSchema__all__r   r   r   r   r   <module>   s,    