
    hhQ0                       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
r.d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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                      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ed,d            Zed-d            Zed.d            Zed/d            Zd0dZ	 d'd1dZd2d Zed3d#            Zed4d&            Z xZS )5r   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     F/var/www/histauto/venv/lib/python3.11/site-packages/narwhals/schema.pyr'   zSchema.__init__M   s,     2         	list[str]c                D    t          |                                           S )z#Get the column names of the schema.)listkeysr(   s    r*   nameszSchema.namesS   s    DIIKK   r+   list[DType]c                D    t          |                                           S )z!Get the data types of the schema.)r.   valuesr0   s    r*   dtypeszSchema.dtypesW   s    DKKMM"""r+   intc                     t          |           S )z(Get the number of columns in the schema.)lenr0   s    r*   r8   z
Schema.len[   s    4yyr+   r   r   c                    t          |t                    r |s
              S ddl} |j        |          }ddlm    f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  R   K   | ]!}|j          |j        j                  fV  "d S r%   )nametyper   ).0fieldclsr;   s     r*   	<genexpr>z$Schema.from_arrow.<locals>.<genexpr>   sP       
 
 Z11%*clKKL
 
 
 
 
 
r+   )
isinstancer   pyarrowr    narwhals._arrow.utilsr;   )rA   r    par;   s   `  @r*   
from_arrowzSchema.from_arrow_   s    2 fg&& 	' suu    RYv&&FBBBBBBs 
 
 
 
 

 
 
 
 
 	
r+   r   c                   |s
 |             S t                      r7t          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              3  4   K   | ]}t          |          V  d S r%   )r   )r?   dtypes     r*   rB   z*Schema.from_pandas_like.<locals>.<genexpr>   s*      !T!T5-"6"6!T!T!T!T!T!Tr+   )r   anyr4   r	   CUDFPANDAS_from_pandas_like)rA   r    impls      r*   from_pandas_likezSchema.from_pandas_like   sw    :  	355L zz'!!T!TFMMOO!T!T!TTT'N& 	
 $$VT222r+   5IntoArrowSchema | IntoPolarsSchema | IntoPandasSchemac               F   t          |          r|                     |          S t          |          r|                     |          S t	          |t
                    r!|r|                     |          n	 |             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   rG   r   from_polarsrC   r   _from_native_mappingr   	TypeError)rA   r    msgs      r*   from_nativezSchema.from_native   s    * V$$ 	*>>&)))F## 	+??6***fg&& 	I7=H3++F3333355H="6**= =28= = 	 nnr+   r   c               ~     |s
              S ddl m    f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   r:   c              3  D   K   | ]\  }}| |j                   fV  d S r%   r   )r?   r=   rJ   rA   r;   s      r*   rB   z%Schema.from_polars.<locals>.<genexpr>   sP       
 
e ++E3<@@A
 
 
 
 
 
r+   )narwhals._polars.utilsr;   items)rA   r    r;   s   ` @r*   rS   zSchema.from_polars   sr    4  	355LCCCCCCs 
 
 
 
 
%||~~
 
 
 
 
 	
r+   	pa.Schemac                x     ddl }ddlm  |j         f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  D   K   | ]\  }}| |j                   fV  d S r%   rZ   r?   r=   rJ   r`   r(   s      r*   rB   z"Schema.to_arrow.<locals>.<genexpr>   sP       
 
e ++E4=AAB
 
 
 
 
 
r+   )rD   rE   r`   r    r\   )r(   rF   r`   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 m} t          |t          j        | 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 )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                2    i | ]\  }}| |           S )rJ   rd    )r?   r=   rJ   rd   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 rk   rl   )r?   r=   rJ   backendrm   s       r*   rn   z$Schema.to_pandas.<locals>.<dictcomp>2  s@     
 
 
$eW //WEEE
 
 
r+   )narwhals._pandas_like.utilsr`   r   r	   rM   r   rC   strr\   tupler8   	itertoolsro   rp   rq   from_iterable
ValueErrorr   r/   r4   )r(   rd   r`   backendsro   rp   rq   n_usern_actual
suggestionrV   rm   s    `         @r*   	to_pandaszSchema.to_pandas  s   $ 	IHHHHH!$)0M
 
 

  J}c$B$B     #'::<<    ''x==CII%%7777777777"8}}c$iiHFu**66&&2B2BH+M+MNNPXYY J6F 6 6x 6 6(06 6 )1	6 6 )36 6 6  S//!
 
 
 
(2499;;x(X(X
 
 
 	
r+   	pl.Schemac                     ddl }ddlm t          j                                        } fd                                 D             }|dk    r |j        |          n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  D   K   | ]\  }}| |j                   fV  d S r%   rZ   rb   s      r*   rB   z#Schema.to_polars.<locals>.<genexpr>E  sP       
 
e ++E4=AAB
 
 
 
 
 
r+   )   r   r   r   )
polarsr[   r`   r	   POLARS_backend_versionr\   r   r   dict)r(   pl
pl_versionr    r`   s   `   @r*   	to_polarszSchema.to_polars7  s     	CCCCCC#*;;==

 
 
 
 
#zz||
 
 
 Y&& BIfk4<<00	
r+   nativeHMapping[str, pa.DataType] | Mapping[str, pl.DataType] | IntoPandasSchemac                  t          t          |                                                    }|\  }}t          |          r#|                     t          d|                    S t          |          r#|                     t          d|                    S t          |          r#| 	                    t          d|                    S d| dt          |           d|}t          |          )Nr   r   r   z7Expected an arrow, polars, or pandas dtype, but found `z: z`

)nextiterr\   r   rS   r   r   rP   r   rG   r   rU   )rA   r   
first_item	first_keyfirst_dtyperV   s         r*   rT   zSchema._from_native_mappingO  s    $v||~~..//
!+	;{++ 	E??4(:F#C#CDDD,, 	J''-?(H(HIII,, 	C>>$'8&"A"ABBBOO O0==O ODJO O 	 nnr+   rh   r	   c               n     ddl m |   fd|                                D                       S )Nr   r:   c              3  J   K   | ]\  }}| |j         d           fV  dS )T)allow_objectNrZ   )r?   r=   rJ   rA   rO   r;   s      r*   rB   z+Schema._from_pandas_like.<locals>.<genexpr>j  sX       
 
e ++E3<TXYYYZ
 
 
 
 
 
r+   )ru   r;   r\   )rA   r    rh   rO   r;   s   `  @@r*   rN   zSchema._from_pandas_likec  sk     	IHHHHHs 
 
 
 
 
 
%||~~
 
 
 
 
 	
r+   r%   )r    r!   r"   r#   )r"   r,   )r"   r2   )r"   r6   )r    r   r"   r   )r    r   r"   r   )r    rQ   r"   r   )r    r   r"   r   )r"   r]   )rd   re   r"   rf   )r"   r   )r   r   r"   r   )r    r   rh   r	   r"   r   )__name__
__module____qualname____doc__r
   MAINr   __annotations__r'   r1   r5   r8   classmethodrG   rP   rW   rS   rc   r   r   rT   rN   __classcell__)r)   s   @r*   r   r   -   s         : #*,H.... RV! ! ! ! ! ! !! ! ! !# # # #    #
 #
 #
 [#
J #3 #3 #3 [#3J    [@  
  
  
 [ 
D
 
 
 
( FJ3
 3
 3
 3
 3
j
 
 
 
0    [& 	
 	
 	
 [	
 	
 	
 	
 	
r+   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   r   r   rD   rF   typing_extensionsr   narwhals.dtypesr   narwhals.typingr   r   r   r   __all__rv   r   rl   r+   r*   <module>r      s    # " " " " " # # # # # # # # # # # #       & & & & & & & & T T T T T T T T T T T T                   (((((($$$$$$$$&&&&&&%%%%%%            *@
 @
 @
 @
 @
[g& @
 @
 @
 @
 @
r+   