o
    Lh                     @  s   d 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 erLddlmZ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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)partial)TYPE_CHECKINGcast)ImplementationVersion
zip_strict)IterableMapping)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: 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__ P/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/narwhals/schema.pyr   =   s   zSchema.__init__	list[str]c                 C     t |  S )z#Get the column names of the schema.)listkeysr   r   r   r   namesC      zSchema.nameslist[DType]c                 C  r   )z!Get the data types of the schema.)r    valuesr"   r   r   r   dtypesG   r$   zSchema.dtypesintc                 C  s   t | S )z(Get the number of columns in the schema.)lenr"   r   r   r   r)   K   s   z
Schema.len	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   r   .0namedtyper,   r   r   r   	<genexpr>]   
    
z"Schema.to_arrow.<locals>.<genexpr>)pyarrownarwhals._arrow.utilsr,   r   items)r   par   r3   r   to_arrowO   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   r+   )implementationversionNc                   s   i | ]\}}|| d qS )r2   r;   r   r/   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 r@   r   )r0   r1   r2   backend)rB   r   r   rC      s    )narwhals._pandas_like.utilsr,   r   r   PANDASr   
isinstancestrr8   tupler)   	itertoolsrD   rE   rF   from_iterable
ValueErrorr	   r!   r&   )r   r;   r,   backendsrD   rE   rF   n_usern_actual
suggestionmsgr   rA   r   	to_pandasb   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   Nr+   c                 3  r-   r   r.   r/   r3   r   r   r4      r5   z#Schema.to_polars.<locals>.<genexpr>)   r   r   rW   )
polarsnarwhals._polars.utilsr,   r   POLARS_backend_versionr8   r   r   dict)r   pl
pl_versionr   r   r3   r   	to_polars   s   	
zSchema.to_polarsr   )r   r   r   r   )r   r   )r   r%   )r   r(   )r   r*   )r;   r<   r   r=   )r   rW   )__name__
__module____qualname____doc__r   MAINr   __annotations__r   r#   r'   r)   r:   rV   r`   __classcell__r   r   r   r   r      s   
 



5r   )rd   
__future__r   collectionsr   	functoolsr   typingr   r   narwhals._utilsr   r   r	   collections.abcr
   r   r   r   rY   r^   r6   r9   narwhals.dtypesr   narwhals.typingr   __all__rL   r   r   r   r   r   <module>   s    