
    -Ph9                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZmZ er'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 eeef         ZneZdgZ G 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                  v     e Zd ZU dZej        Zded<   	 dd fd	ZddZ	ddZ
ddZddZ	 dddZddZ x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                R    |pi }t                                          |           d S N)super__init__)selfr   	__class__s     O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/narwhals/schema.pyr   zSchema.__init__@   s,     2         	list[str]c                D    t          |                                           S )zXGet the column names of the schema.

        Returns:
            Column names.
        )listkeysr   s    r   nameszSchema.namesF   s     DIIKK   r   list[DType]c                D    t          |                                           S )z^Get the data types of the schema.

        Returns:
            Data types of schema.
        )r"   valuesr$   s    r   dtypeszSchema.dtypesN   s     DKKMM"""r   intc                     t          |           S )zbGet the number of columns in the schema.

        Returns:
            Number of columns.
        )lenr$   s    r   r,   z
Schema.lenV   s     4yyr   	pa.Schemac                x     ddl }ddlm  |j         f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  D   K   | ]\  }}| |j                   fV  d S r   r   ).0namedtyper0   r   s      r   	<genexpr>z"Schema.to_arrow.<locals>.<genexpr>o   sP       
 
e ++E4=AAB
 
 
 
 
 
r   )pyarrownarwhals._arrow.utilsr0   r   items)r   par0   s   ` @r   to_arrowzSchema.to_arrow^   sm     	BBBBBBry 
 
 
 
 
#zz||
 
 
 
 
 	
r   dtype_backend%DTypeBackend | Iterable[DTypeBackend]dict[str, Any]c                   ddl }ddlm} t          |t          j        t          |          | j                  t          t                    r!fd| 
                                D             S t                    }t          |          t          |           k    rddlm}m}m} t          |          t          |           }	}t           ||                     | ||          |	                    |	                    }
d|d|	d	| d
|d          d|
 d}t%          |          f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                2    i | ]\  }}| |           S )r5   r<    )r3   r4   r5   r<   to_native_dtypes      r   
<dictcomp>z$Schema.to_pandas.<locals>.<dictcomp>   s>       D% ooEOOO  r   )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                4    i | ]\  }}}| ||           S rD   rE   )r3   r4   r5   backendrF   s       r   rG   z$Schema.to_pandas.<locals>.<dictcomp>   s@       (D% ooEIII  r   )pandasnarwhals._pandas_like.utilsr0   r   r
   PANDASr   r   
isinstancestrr9   tupler,   	itertoolsrH   rI   rJ   from_iterable
ValueErrorzipr#   r(   )r   r<   pdr0   backendsrH   rI   rJ   n_usern_actual
suggestionmsgrF   s    `          @r   	to_pandaszSchema.to_pandast   s   * 	HHHHHH!$)0)"--M	
 
 
  J}c$B$B     #'::<<   
 ]++H8}}D		));;;;;;;;;;#&x==#d))"F++FF66(3C3CX,N,NOOQY  
: : :S[ : :,4: : -5QK	: : -7: : :  !oo%   ,/		T[[]]H,U,U   r   	pl.Schemac                     ddl }ddlm t          |           fd                                 D             }dk    r |j        |          n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  H   K   | ]\  }}| |j                    fV  dS ))rA   Nr2   )r3   r4   r5   r0   
pl_versionr   s      r   r6   z#Schema.to_polars.<locals>.<genexpr>   s`       
 
 e ((4=*  
 
 
 
 
 
r   )   r   r   r_   )polarsnarwhals._polars.utilsr0   r   r9   r   r	   dict)r   plr   r0   rb   s   `  @@r   	to_polarszSchema.to_polars   s     	CCCCCC"2&&

 
 
 
 
 
  $zz||
 
 
 Y&& BIfk4<<00	
r   r   )r   r   r   r   )r   r    )r   r&   )r   r*   )r   r-   )r<   r=   r   r>   )r   r_   )__name__
__module____qualname____doc__r   MAINr   __annotations__r   r%   r)   r,   r;   r^   rh   __classcell__)r   s   @r   r   r       s          : #*,H.... RV! ! ! ! ! ! !! ! ! !# # # #   
 
 
 
. FJ< < < < <|
 
 
 
 
 
 
 
r   )rl   
__future__r   collectionsr   	functoolsr   typingr   r   r   r	   narwhals._utilsr
   r   r   r   r   rd   rg   r7   r:   narwhals.dtypesr   narwhals.typingr   rR   
BaseSchema__all__r   rE   r   r   <module>ry      sA    # " " " " " # # # # # #       9 9 9 9 9 9 9 9 9 9 9 9 B B B B B B B B B B $$$$$$$$%%%%%%,,,,,,S%Z(JJ J*p
 p
 p
 p
 p
Z p
 p
 p
 p
 p
r   