
    i%                       U d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ  ej6                  e      5  d dlmZmZm Z  ddd       erd dlm!Z! d dlm"Z" d dl#Z$d dl%m&Z&m'Z' d dlm(Z( nd dl)m#Z$ ddZ*ee+ef   Z,eez  ez  Z-de.d<   dgZ/ddZ0 G d de,      Z1y# 1 sw Y   [xY w)    )annotationsN)OrderedDict)Mapping)TYPE_CHECKINGLiteraloverload)PythonDataType)unstable)DataTypeDataTypeClassis_polars_dtype)parse_into_dtype)unpack_dtypes)DuplicateError)CompatLevel)&init_polars_schema_from_arrow_c_schema'polars_schema_field_from_arrow_c_schemapolars_schema_to_pycapsule)Iterable)	TypeAlias	DataFrame	LazyFrame)ArrowSchemaExportable)pyarrowc                ,    t        | j                        S N)bool__annotations__)tps    F/home/work/apex_v16/venv/lib/python3.12/site-packages/polars/schema.py_required_init_argsr"   #   s    ""##    r   SchemaInitDataTypeSchemac                    t        | t              sB| j                         s| j                         st	        |       rd| }t        |       |        } | S )Nz%dtypes must be fully-specified, got: )
isinstancer   	is_nested
is_decimalr"   	TypeError)r    msgs     r!   _check_dtyper,   -   sG    b(#<<>R]]_0CB0G9"@CC. TIr#   c                       e Zd ZdZ	 ddd	 	 	 	 	 d fdZddZddZ	 	 	 	 	 	 d fdZ e       dd	       Z	dd
Z
ddZ e       dddd       Zedd       Zedddd       Zddd dZd!dZd"dZd#dZ xZS )$r%   aA  
    Ordered mapping of column names to their data type.

    Parameters
    ----------
    schema
        The schema definition given by column names and their associated
        Polars data type. Accepts a mapping, or an iterable of tuples, or any
        object implementing the  `__arrow_c_schema__` PyCapsule interface
        (e.g. pyarrow schemas).

    Examples
    --------
    Define a schema by passing instantiated data types.

    >>> schema = pl.Schema(
    ...     {
    ...         "foo": pl.String(),
    ...         "bar": pl.Duration("us"),
    ...         "baz": pl.Array(pl.Int8, 4),
    ...     }
    ... )
    >>> schema
    Schema({'foo': String, 'bar': Duration(time_unit='us'), 'baz': Array(Int8, shape=(4,))})

    Access the data type associated with a specific column name.

    >>> schema["baz"]
    Array(Int8, shape=(4,))

    Access various schema properties using the `names`, `dtypes`, and `len` methods.

    >>> schema.names()
    ['foo', 'bar', 'baz']
    >>> schema.dtypes()
    [String, Duration(time_unit='us'), Array(Int8, shape=(4,))]
    >>> schema.len()
    3

    Import a pyarrow schema.

    >>> import pyarrow as pa
    >>> pl.Schema(pa.schema([pa.field("x", pa.int32())]))
    Schema({'x': Int32})

    Export a schema to pyarrow.

    >>> pa.schema(pl.Schema({"x": pl.Int32}))
    x: int32
    NT)check_dtypesc                  t        |d      rt        |t              st        | |       y t        |t              r|j                         n|xs d}|D ]  }t        |d      rt        |t              st        |      n|\  }}|| v rd| d}t        |      |st        | )  ||       Wt        |      rt        | )  |t        |             ||| |<    y )N__arrow_c_schema__ z7iterable passed to pl.Schema contained duplicate name '')hasattrr'   r%   r   r   itemsr   r   r   super__setitem__r   r,   )	selfschemar.   inputvnamer    r+   	__class__s	           r!   __init__zSchema.__init__k   s     6/0FF9S24@",VW"=FLb 	 A 123Jq(<S 8: D" t|OPTvUVW$S))#D"- $#D,r*:;T
!	 r#   c                    t        |t              syt        |       t        |      k7  ryt        | j	                         |j	                         d      D ]#  \  \  }}\  }}||k7  s|j                  |      r# y y)NFT)strict)r'   r   lenzipr4   is_)r7   othernm1tp1nm2tp2s         r!   __eq__zSchema.__eq__   sl    %)t9E
"&)$**,d&S 	"JS#
ccz	 r#   c                &    | j                  |       S r   )rH   )r7   rC   s     r!   __ne__zSchema.__ne__   s    ;;u%%%r#   c                N    t        t        |            }t        |   ||       y r   )r,   r   r5   r6   )r7   r;   dtyper<   s      r!   r6   zSchema.__setitem__   s$     -e45D%(r#   c                R    t        | t        j                         j                        S )z
        Export a Schema via the Arrow PyCapsule Interface.

        https://arrow.apache.org/docs/dev/format/CDataInterface/PyCapsuleInterface.html
        )r   r   newest_versionr7   s    r!   r0   zSchema.__arrow_c_schema__   s      *$0B0B0D0M0MNNr#   c                4    t        | j                               S )z
        Get the column names of the schema.

        Examples
        --------
        >>> s = pl.Schema({"x": pl.Float64(), "y": pl.Datetime(time_zone="UTC")})
        >>> s.names()
        ['x', 'y']
        )listkeysrP   s    r!   nameszSchema.names   s     DIIK  r#   c                4    t        | j                               S )z
        Get the data types of the schema.

        Examples
        --------
        >>> s = pl.Schema({"x": pl.UInt8(), "y": pl.List(pl.UInt8)})
        >>> s.dtypes()
        [UInt8, List(UInt8)]
        )rR   valuesrP   s    r!   dtypeszSchema.dtypes   s     DKKM""r#   )compat_levelc                    G d d      }t        j                   || |t        j                                     S |            S )aI  
        Convert the schema to a pyarrow schema.

        Parameters
        ----------
        compat_level
            Use a specific compatibility level
            when exporting Polars' internal data types.

        Examples
        --------
        >>> pl.Schema({"x": pl.String}).to_arrow()
        x: string_view
        c                      e Zd ZddZddZy).Schema.to_arrow.<locals>.SchemaCapsuleProviderc                     || _         || _        y r   )r8   rX   )r7   r8   rX   s      r!   r=   z7Schema.to_arrow.<locals>.SchemaCapsuleProvider.__init__   s    $$0!r#   c                V    t        | j                  | j                  j                        S r   )r   r8   rX   rO   rP   s    r!   r0   zASchema.to_arrow.<locals>.SchemaCapsuleProvider.__arrow_c_schema__   s$    1KK!2!2!;!; r#   N)r8   r%   rX   r   returnNoner^   object)__name__
__module____qualname__r=   r0   r1   r#   r!   SchemaCapsuleProviderr[      s    1r#   re   )par8   r   rN   )r7   rX   re   s      r!   to_arrowzSchema.to_arrow   sL    "	 	 yy!l.Bk((*
 	
HT
 	
r#   c                    y r   r1   r7   eagers     r!   to_framezSchema.to_frame   s    ?Br#   .)rj   c                    y r   r1   ri   s     r!   rk   zSchema.to_frame   s    DGr#   c               :    ddl m}m} |r	 ||       S  ||       S )u  
        Create an empty DataFrame (or LazyFrame) from this Schema.

        Parameters
        ----------
        eager
            If True, create a DataFrame; otherwise, create a LazyFrame.

        Examples
        --------
        >>> s = pl.Schema({"x": pl.Int32(), "y": pl.String()})
        >>> s.to_frame()
        shape: (0, 2)
        ┌─────┬─────┐
        │ x   ┆ y   │
        │ --- ┆ --- │
        │ i32 ┆ str │
        ╞═════╪═════╡
        └─────┴─────┘
        >>> s.to_frame(eager=False)  # doctest: +IGNORE_RESULT
        <LazyFrame at 0x11BC0AD80>
        r   r   )r8   )polarsr   r   )r7   rj   r   r   s       r!   rk   zSchema.to_frame   s    . 	0).y%JIT4JJr#   c                    t        |       S )z
        Get the number of schema entries.

        Examples
        --------
        >>> s = pl.Schema({"x": pl.Int32(), "y": pl.List(pl.String)})
        >>> s.len()
        2
        >>> len(s)
        2
        )r@   rP   s    r!   r@   z
Schema.len  s     4yr#   c                r    | j                         D ci c]  \  }}||j                          c}}S c c}}w )a  
        Return a dictionary of column names and Python types.

        Examples
        --------
        >>> s = pl.Schema(
        ...     {
        ...         "x": pl.Int8(),
        ...         "y": pl.String(),
        ...         "z": pl.Duration("us"),
        ...     }
        ... )
        >>> s.to_python()
        {'x': <class 'int'>, 'y':  <class 'str'>, 'z': <class 'datetime.timedelta'>}
        )r4   	to_python)r7   r;   r    s      r!   rq   zSchema.to_python  s-      6:ZZ\Brblln$BBBs   3c               |    |s"t        fd| j                         D              S t        | j                          v S )av  
        Check if the schema contains the given data type.

        Parameters
        ----------
        dtype
            The data type to search for.
        recursive
            If False, only check top-level column dtypes.
            If True, also search within nested types (List, Array, Struct).

        Examples
        --------
        >>> s = pl.Schema({"x": pl.Int64(), "y": pl.List(pl.Float64)})
        >>> s.contains_dtype(pl.Int64, recursive=False)
        True
        >>> s.contains_dtype(pl.Float64, recursive=False)
        False
        >>> s.contains_dtype(pl.Float64, recursive=True)
        True
        c              3  (   K   | ]	  }|k(    y wr   r1   ).0dtrL   s     r!   	<genexpr>z(Schema.contains_dtype.<locals>.<genexpr>:  s     ;rrU{;s   )anyrV   r   )r7   rL   	recursives    ` r!   contains_dtypezSchema.contains_dtype#  s5    , ;T[[];;;M4;;=999r#   r   )r8   zMapping[str, SchemaInitDataType] | Iterable[tuple[str, SchemaInitDataType] | ArrowSchemaExportable] | ArrowSchemaExportable | Noner.   r   r^   r_   )rC   ra   r^   r   )r;   strrL   z)DataType | DataTypeClass | PythonDataTyper^   r_   r`   )r^   z	list[str])r^   zlist[DataType])rX   zCompatLevel | Noner^   z	pa.Schema)rj   zLiteral[False]r^   r   )rj   zLiteral[True]r^   r   )rj   r   r^   zDataFrame | LazyFrame)r^   int)r^   zdict[str, type])rL   r   rx   r   r^   r   )rb   rc   rd   __doc__r=   rH   rJ   r6   r
   r0   rT   rW   rg   r   rk   r@   rq   ry   __classcell__)r<   s   @r!   r%   r%   7   s    1t    "        
  D&)) I)	) ZO O
!
# Z=A 
 
@ B B14G G(, K6C$:r#   )r    r   r^   r   )r    zDataType | DataTypeClassr^   r   )2
__future__r   
contextlibcollectionsr   collections.abcr   typingr   r   r   polars._typingr	   polars._utils.unstabler
   polars.datatypesr   r   r   polars.datatypes._parser   polars.datatypes.convertr   polars.exceptionsr   polars.interchange.protocolr   suppressImportErrorpolars._plrr   r   r   r   r   r   rf   rn   r   r   r   polars._dependenciesr"   rz   
BaseSchemar$   r   __all__r,   r%   r1   r#   r!   <module>r      s    "  # # 3 3 ) + E E 4 2 , 3Z%   ( +42$ h'
 (= 8> I I I*E:Z E:O s   "CC