o
    i!                     @  sl  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 ee d dlmZmZmZ W d   n1 scw   Y  erd 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dZ(ee)ef Z*eeB eB Z+de,d< dgZ-d!ddZ.G dd de*Z/dS )"    )annotationsN)OrderedDict)Mapping)TYPE_CHECKINGLiteraloverload)PythonDataType)unstable)DataTypeDataTypeClassis_polars_dtype)parse_into_dtype)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)pyarrowtpr   returnboolc                 C  s
   t | jS N)r   __annotations__)r    r   D/home/app/Keep/.python/lib/python3.10/site-packages/polars/schema.py_required_init_args"   s   
r!   r   SchemaInitDataTypeSchemaDataType | DataTypeClassr
   c                 C  s>   t | ts|  s|  st| rd| }t||  } | S )Nz%dtypes must be fully-specified, got: )
isinstancer
   Z	is_nestedZ
is_decimalr!   	TypeError)r   msgr   r   r    _check_dtype,   s   

r(   c                      s   e Zd ZdZ	d9ddd: fddZd;ddZd;ddZd< fddZe d=ddZ	d>ddZ
d?dd Ze dd!d@d%d&ZedAd*d+Zed,d-dBd0d+Zdd-dCd2d+ZdDd4d5ZdEd7d8Z  ZS )Fr#   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_dtypesschemaMapping[str, SchemaInitDataType] | Iterable[tuple[str, SchemaInitDataType] | ArrowSchemaExportable] | ArrowSchemaExportable | Noner)   r   r   Nonec                  s   t |drt|tst| | d S t|tr| n|pd}|D ]>}t |dr0t|ts0t|n|\}}|| v rBd| d}t||sLt	 
|| q t|rZt	 
|t| q || |< q d S )N__arrow_c_schema__r   z7iterable passed to pl.Schema contained duplicate name '')hasattrr%   r#   r   r   itemsr
   r   r   super__setitem__r   r(   )selfr*   r)   inputvnamer   r'   	__class__r   r    __init__j   s(   


zSchema.__init__otherobjectc                 C  sf   t |tsdS t| t|krdS t|  | ddD ]\\}}\}}||ks-||s0 dS qdS )NFT)strict)r%   r   lenzipr0   is_)r3   r:   Znm1Ztp1Znm2Ztp2r   r   r    __eq__   s   
&zSchema.__eq__c                 C  s   |  | S r   )r@   )r3   r:   r   r   r    __ne__   s   zSchema.__ne__r6   strdtype)DataType | DataTypeClass | PythonDataTypec                   s   t t|}t || d S r   )r(   r   r1   r2   )r3   r6   rC   r7   r   r    r2      s   zSchema.__setitem__c                 C  s   t | t 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r3   r   r   r    r-      s   zSchema.__arrow_c_schema__	list[str]c                 C     t |  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keysrG   r   r   r    names      
zSchema.nameslist[DataType]c                 C  rI   )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)]
        )rJ   valuesrG   r   r   r    dtypes   rM   zSchema.dtypes)compat_levelrQ   CompatLevel | None	pa.Schemac                C  s2   G dd d}t || |du rt 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                   @  s    e Zd ZdddZdd
dZdS )z.Schema.to_arrow.<locals>.SchemaCapsuleProviderr*   r#   rQ   r   r   r,   c                 S  s   || _ || _d S r   )r*   rQ   )r3   r*   rQ   r   r   r    r9      s   
z7Schema.to_arrow.<locals>.SchemaCapsuleProvider.__init__r;   c                 S  s   t | j| jjS r   )r   r*   rQ   rF   rG   r   r   r    r-      s   
zASchema.to_arrow.<locals>.SchemaCapsuleProvider.__arrow_c_schema__N)r*   r#   rQ   r   r   r,   r   r;   )__name__
__module____qualname__r9   r-   r   r   r   r    SchemaCapsuleProvider   s    
rX   N)par*   r   rE   )r3   rQ   rX   r   r   r    to_arrow   s   
zSchema.to_arroweagerLiteral[False]r   c                C     d S r   r   r3   r[   r   r   r    to_frame      zSchema.to_frame.)r[   Literal[True]r   c                C  r]   r   r   r^   r   r   r    r_      r`   DataFrame | LazyFramec                C  s(   ddl m}m} |r|| dS || d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   )r*   )polarsr   r   )r3   r[   r   r   r   r   r    r_      s   intc                 C  s   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=   rG   r   r   r    r=     s   z
Schema.lendict[str, type]c                 C  s   dd |   D S )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'>}
        c                 S  s   i | ]	\}}||  qS r   )	to_python).0r6   r   r   r   r    
<dictcomp>   s    z$Schema.to_python.<locals>.<dictcomp>)r0   rG   r   r   r    rf     s   zSchema.to_pythonr   )r*   r+   r)   r   r   r,   )r:   r;   r   r   )r6   rB   rC   rD   r   r,   rT   )r   rH   )r   rN   )rQ   rR   r   rS   )r[   r\   r   r   )r[   ra   r   r   )r[   r   r   rb   )r   rd   )r   re   )rU   rV   rW   __doc__r9   r@   rA   r2   r	   r-   rL   rP   rZ   r   r_   r=   rf   __classcell__r   r   r7   r    r#   6   s,    :	
"



 
)r   r   r   r   )r   r$   r   r
   )0
__future__r   
contextlibcollectionsr   collections.abcr   typingr   r   r   Zpolars._typingr   Zpolars._utils.unstabler	   Zpolars.datatypesr
   r   r   Zpolars.datatypes._parser   Zpolars.exceptionsr   Zpolars.interchange.protocolr   suppressImportErrorZpolars._plrr   r   r   r   r   r   rY   rc   r   r   r   Zpolars._dependenciesr!   rB   Z
BaseSchemar"   r   __all__r(   r#   r   r   r   r    <module>   s6    


