
    Lpj6                    2   S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
JrJr  SSKJrJrJr  SSKJrJrJrJrJrJrJr  SS	KJr  \
(       a2  SS
KJr  SSK	JrJr  SSKrSSK r!SSK"J#r#  SSK$J%r%J&r&  SSK'J(r(J)r)J*r*J+r+J,r,  S/r-SS jr. " S S\\/S4   5      r0g)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overload)ImplementationVersionqualified_type_name)get_cudfis_cudf_dtypeis_pandas_like_dtypeis_polars_data_typeis_polars_schemais_pyarrow_data_typeis_pyarrow_schema)DType)Iterable)AnyClassVarN)SupportsItems)SelfTypeIs)DTypeBackendIntoArrowSchema	IntoDTypeIntoPandasSchemaIntoPolarsSchemaSchemac                .    [        U [        [        45      $ N)
isinstancedictr   )objs    K/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/narwhals/schema.py_supports_itemsr'   /   s     cD'?++    c                  h  ^  \ rS rSr% Sr\R                  rS\S'   \	SSS jj5       r
\	SS j5       r
 S   SU 4S jjjr
SS jrSS	 jrSS
 jr\SS j5       r\SS j5       r\    SS j5       r\SS j5       rS S jr S   S!S jjrS"S jr\    S#S j5       r\      S$S j5       rSrU =r$ )%r    6   ar  Ordered mapping of column names to their data type.

Note:
    The pandas-like and dask backends allow non-string column names
    (e.g. integers or booleans). While discouraged, this is supported,
    so we cannot guarantee that the keys are strictly strings.

    See [concepts - column names](../concepts/column_names.md) for details.

Arguments:
    schema: The schema definition given by column names and their associated
        Narwhals data type. Accepts a mapping or an iterable of tuples.
        Data types that take no required arguments may also be passed
        uninstantiated, e.g. `nw.Int8` instead of `nw.Int8()`; they are
        instantiated on construction.

Examples:
    >>> 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]_versionc                    g r"    selfschemas     r&   __init__Schema.__init__^   s    ORr(   c                    g r"   r-   r.   s     r&   r1   r2   `   s    ILr(   c                   > Uc  [         TU ]  5         g [         TU ]  S [        U5      (       a  UR                  5       OU 5       5        g )Nc              3  f   #    U  H'  u  pU[        U[        5      (       a  UOU" 5       4v   M)     g 7fr"   )r#   r   ).0namedtypes      r&   	<genexpr>"Schema.__init__.<locals>.<genexpr>i   s.      $YKD 
5% 8 8uegF$Ys   /1)superr1   r'   items)r/   r0   	__class__s     r&   r1   r2   b   sE     >GG 6Ef6M6MFLLNSY$Y r(   c                4    [        U R                  5       5      $ )a?  Get the column names of the schema.

Note:
    The pandas-like and dask backends allow non-string column names
    (e.g. integers or booleans). While discouraged, this is supported,
    so the return type is not guaranteed to be `list[str]`.

    See [concepts - column names](../concepts/column_names.md) for details.
)listkeysr/   s    r&   namesSchema.namesn   s     DIIK  r(   c                4    [        U R                  5       5      $ )z!Get the data types of the schema.)r?   valuesrA   s    r&   dtypesSchema.dtypesz   s    DKKM""r(   c                    [        U 5      $ )z(Get the number of columns in the schema.)lenrA   s    r&   rI   
Schema.len~   s    4yr(   c                  ^ ^ [        U[        5      (       a$  U(       d  T " 5       $ SSKnUR                  " U5      nSSKJm  T " U U4S jU 5       5      $ )ax  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  v   >#    U  H.  nUR                   T" UR                  TR                  5      4v   M0     g 7fr"   )r7   typer+   )r6   fieldclsrM   s     r&   r9   $Schema.from_arrow.<locals>.<genexpr>   s1      
 ZZ1%**cllKLs   69)r#   r   pyarrowr0   narwhals._arrow.utilsrM   )rQ   r0   parM   s   `  @r&   
from_arrowSchema.from_arrow   sJ    2 fg&&u YYv&FB 

 
 	
r(   c                   U(       d  U " 5       $ [        5       (       a5  [        S UR                  5        5       5      (       a  [        R                  O[        R
                  nU R                  X5      $ )as  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  8   #    U  H  n[        U5      v   M     g 7fr"   )r   )r6   r8   s     r&   r9   *Schema.from_pandas_like.<locals>.<genexpr>   s     !TO5-"6"6Os   )r   anyrE   r
   CUDFPANDAS_from_pandas_like)rQ   r0   impls      r&   from_pandas_likeSchema.from_pandas_like   sY    : 5L zzc!TFMMO!TTT && 	
 $$V22r(   c               *   [        U5      (       a  U R                  U5      $ [        U5      (       a  U R                  U5      $ [	        U[
        5      (       a  U(       a  U R                  U5      $ U " 5       $ S[        U5      < SU< 3n[        U5      e)a  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   rV   r   from_polarsr#   r   _from_native_mappingr   	TypeError)rQ   r0   msgs      r&   from_nativeSchema.from_native   s    * V$$>>&))F##??6**fg&&7=3++F3H35HC"6*-T&= 	 nr(   c               p   ^ ^ U(       d  T " 5       $ SSK Jm  T " U U4S jUR                  5        5       5      $ )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   rL   c              3  R   >#    U  H  u  pUT" UTR                   5      4v   M     g 7fr"   r+   )r6   r7   r8   rQ   rM   s      r&   r9   %Schema.from_polars.<locals>.<genexpr>  s+      
- +E3<<@A-   $')narwhals._polars.utilsrM   r<   )rQ   r0   rM   s   ` @r&   rc   Schema.from_polars   s2    4 5LC 
%||~
 
 	
r(   c                p   ^ ^ SSK nSSKJm  UR                  " UU 4S jT R	                  5        5       5      $ )zConvert 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  R   >#    U  H  u  pUT" UTR                   5      4v   M     g 7fr"   rk   r6   r7   r8   rr   r/   s      r&   r9   "Schema.to_arrow.<locals>.<genexpr>   s+      
+ +E4==AB+rm   )rS   rT   rr   r0   r<   )r/   rU   rr   s   ` @r&   to_arrowSchema.to_arrow  s/     	Byy 
#zz|
 
 	
r(   c                   SSK Jn  [        U[        R                  U R
                  S9nUb  [        U[        5      (       a)  U R                  5        VVs0 s H  u  pEXC" XQS9_M     snn$ [        U5      n[        U5      [        U 5      :w  ao  SSKJnJnJn	  [        U5      [        U 5      p[        U" UR                  U" U	" U5      U5      5      U5      5      nSU
< SU< SU S	US    S
U S3n[!        U5      e[#        U R%                  5       U R'                  5       USS9 VVVs0 s H  u  pEnXC" X^S9_M     snnn$ s  snnf s  snnnf )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.

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   rq   )implementationversion)r8   dtype_backend)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()T)strict)narwhals._pandas_like.utilsrr   r   r
   r]   r+   r#   strr<   tuplerI   	itertoolsr|   r}   r~   from_iterable
ValueErrorzipr@   rE   )r/   r{   rr   to_native_dtyper7   r8   backendsr|   r}   r~   n_usern_actual
suggestionrf   backends                  r&   	to_pandasSchema.to_pandas%  sn   $ 	I!$)00MM

  J}c$B$B $(::<#/KD oEOO#/  'x=CI%77"8}c$iHu**6&2BH+MNPXYJ F:%Nxl [((0z 2((0} 5((2|16  S/! ),		T[[]HT)
)$W /EE)
 	
-,
s   E)Ec                   ^ ^ SSK nSSKJm  [        R                  R                  5       nUU 4S jT R                  5        5       nUS:  a  UR                  " U5      $ [        S[        U5      5      $ )zConvert 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   Nrq   c              3  R   >#    U  H  u  pUT" UTR                   5      4v   M     g 7fr"   rk   rt   s      r&   r9   #Schema.to_polars.<locals>.<genexpr>j  s+      
+ +E4==AB+rm   )   r   r   	pl.Schema)
polarsrn   rr   r
   POLARS_backend_versionr<   r    r   r$   )r/   pl
pl_versionr0   rr   s   `   @r&   	to_polarsSchema.to_polars\  sf     	C#**;;=

#zz|
 Y& IIf	
 k4<0	
r(   c                  [        [        UR                  5       5      5      nUu  p4[        U5      (       a  U R	                  [        SU5      5      $ [        U5      (       a  U R                  [        SU5      5      $ [        U5      (       a  U R                  [        SU5      5      $ SU S[        U5       SU< 3n[        U5      e)Nr   r   r   z7Expected an arrow, polars, or pandas dtype, but found `z: z`

)nextiterr<   r   rc   r   r   r`   r   rV   r   re   )rQ   native
first_item	first_keyfirst_dtyperf   s         r&   rd   Schema._from_native_mappingt  s     $v||~./
!+	{++??4(:F#CDD,,''-?(HII,,>>$'8&"ABB{"0=>eF:O 	 nr(   c               \   ^ ^^ SSK Jm  UmT " U UU4S jUR                  5        5       5      $ )Nr   rL   c           	   3  R   >#    U  H  u  pUT" UTR                   TS S94v   M     g7f)T)allow_objectNrk   )r6   r7   r8   rQ   r_   rM   s      r&   r9   +Schema._from_pandas_like.<locals>.<genexpr>  s0      
- +E3<<TXYZ-rm   )r   rM   r<   )rQ   r0   ry   r_   rM   s   `  @@r&   r^   Schema._from_pandas_like  s.     	I 
%||~
 
 	
r(   r-   r"   )r0   zMapping[str, IntoDType] | NonereturnNone)r0   zIterable[tuple[str, IntoDType]]r   r   )r0   z@Mapping[str, IntoDType] | Iterable[tuple[str, IntoDType]] | Noner   r   )r   z	list[str])r   zlist[DType])r   int)r0   r   r   r   )r0   r   r   r   )r0   z5IntoArrowSchema | IntoPolarsSchema | IntoPandasSchemar   r   )r0   r   r   r   )r   z	pa.Schema)r{   z%DTypeBackend | Iterable[DTypeBackend]r   zdict[str, Any])r   r   )r   zHMapping[str, pa.DataType] | Mapping[str, pl.DataType] | IntoPandasSchemar   r   )r0   r   ry   r
   r   r   )__name__
__module____qualname____firstlineno____doc__r   MAINr+   __annotations__r	   r1   rB   rF   rI   classmethodrV   r`   rg   rc   rv   r   r   rd   r^   __static_attributes____classcell__)r=   s   @r&   r    r    6   sE   #J #*,,H.R RL L TX
P
 

 

!# #
 #
J #3 #3J J	 @  
  
D
( FJ5
B5
	5
n
0 X 
	 & 	
%	
7E	
		
 	
r(   r   )r%   r   r   z%TypeIs[SupportsItems[str, IntoDType]])1r   
__future__r   collectionsr   collections.abcr   	functoolsr   typingr   r   r	   narwhals._utilsr
   r   r   narwhals.dependenciesr   r   r   r   r   r   r   narwhals.dtypesr   r   r   r   r   r   rS   rU   	_typeshedr   typing_extensionsr   r   narwhals.typingr   r   r   r   r   __all__r'   r   r    r-   r(   r&   <module>r      s|    # # #  0 0 H H   "($'.  *,\
[g& \
r(   