ó
    - jÇ6  ã                  ó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    Ú�C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\narwhals/schema.pyÚ_supports_itemsr'   /   s   € ô �cœD¤'˜?Ó+Ð+ó    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.   é € ð â$Y‘K�Dð ¤
¨5´%× 8Ñ 8‘u¹e»gÕFÚ$Yùs   ‚/1)Úsuperr1   r'   Úitems)r/   r0   Ú	__class__s     €r&   r1   r2   b   sE   ø€ ð ‰>Ü‰GÑÕä‰GÑñ ä6EÀf×6MÑ6M F§L¡L¤NÐSYÐ$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   € ô �D—I‘I“KÓ Ð r(   c                ó4   • [        U R                  5       5      $ )z!Get the data types of the schema.)r?   ÚvaluesrA   s    r&   ÚdtypesÚSchema.dtypesz   s   € ä�D—K‘K“MÓ"Ð"r(   c                ó   • [        U 5      $ )z(Get the number of columns in the schema.)ÚlenrA   s    r&   rI   Ú
Schema.len~   s   € ä�4‹yÐ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   øé € ð 
â�ð �Z‰ZÑ1°%·*±*¸c¿l¹lÓKÕLÚùs   ƒ69)r#   r   Úpyarrowr0   Únarwhals._arrow.utilsrM   )rQ   r0   ÚparM   s   `  @r&   Ú
from_arrowÚSchema.from_arrow‚   sJ   ù€ ô2 �fœg×&Ñ&ÞÙ“u�Û à—Y’Y˜vÓ&ˆ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   é € Ð!TÂO¸5¤-°×"6Ð"6ÂOùs   ‚)r   ÚanyrE   r
   ÚCUDFÚPANDASÚ_from_pandas_like)rQ   r0   Úimpls      r&   Úfrom_pandas_likeÚSchema.from_pandas_like¨   sY   € ö: Ù“5ˆLô �z‰zœcÑ!TÀFÇMÁMÄOÓ!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Ó*Ð*Ü�fœg×&Ñ&Þ7=�3×+Ñ+¨FÓ3ÐHÁ3Ã5Ð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+   øé € ð 
â-‘�ð Ñ+¨E°3·<±<Ó@ÕAÚ-ùó   ƒ$')Únarwhals._polars.utilsrM   r<   )rQ   r0   rM   s   ` @r&   rc   ÚSchema.from_polarsï   s2   ù€ ö4 Ù“5ˆLÝ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      $ )zÏ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  ó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+   øé € ð 
â+‘�ð Ñ+¨E°4·=±=ÓAÕBÚ+ùrm   )rS   rT   rr   r0   r<   )r/   rU   rr   s   ` @r&   Úto_arrowÚSchema.to_arrow  s/   ù€ ó 	åBà�yŠyõ 
à#Ÿz™zœ|ó
ó 
ð 	
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Ø—M‘Mñ
ˆð
 Ñ ¤J¨}¼c×$BÑ$Bð $(§:¡:¤<ôâ#/‘K�Dð �o¨EÑOÒOÙ#/òð ô ˜Ó'ˆÜˆx‹=œC ›IÓ%ß7Ñ7ä" 8›}¬c°$«i�HÜÙ�u×*Ñ*©6±&¸Ó2BÀHÓ+MÓNÐPXÓYóˆJð ˜F™:Ð%NÈxÉlð [(à(0 zð 2(à(0°© }ð 5(à(2 |°1ð6ð ô ˜S“/Ð!ô ),Ø—	‘	“˜TŸ[™[›]¨H¸Tò)õ
ò)Ñ$�˜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      $ )zõ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   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+   øé € ð 
â+‘�ð Ñ+¨E°4·=±=Ó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ä#×*Ñ*×;Ñ;Ó=ˆ
õ
à#Ÿz™zœ|ó
ˆð ˜YÓ&ð �IŠI�fÓð	
ô �k¤4¨£<Ó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°Ó=Ð>¸eÀFÁ:ð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   øé € ð 
â-‘�ð Ñ+¨E°3·<±<ÀÐ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ó ð#3ðJ ðØ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(   