o
    i)                     @  sd  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 d dlmZmZmZ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mZmZ erpd d
lm Z m!Z! d dl"m#Z# d dlm$Z$m%Z%m&Z&m'Z' d dlm Z( ddd>ddZ)d?ddZ*d?ddZ+d@d"d#Z,dAd$d%Z-dAd&d'Z.	 dBdCd.d/Z/dDd0d1Z0	 dBdEd7d8Z1	 dBdFd:d;Z2dGd<d=Z3dS )H    )annotations)TYPE_CHECKINGN)qualified_type_name)BooleanEnumInt64StringUInt8UInt32)InvalidOperationError)PolarsDataFrame)ColumnNullTypeCopyNotAllowedError	DtypeKind)dtype_to_polars_dtypeget_buffer_length_in_elements!polars_dtype_to_data_buffer_dtype)	DataFrameSeries)PolarsDataType)BufferColumnDtypeSupportsInterchange)r   T
allow_copydfr   r   boolreturnr   c                C  sV   t | tjr| S t | tr| jS t| ds!dt| d}t|t| j	|d|dS )a  
    Build a Polars DataFrame from any dataframe supporting the interchange protocol.

    Parameters
    ----------
    df
        Object supporting the dataframe interchange protocol, i.e. must have implemented
        the `__dataframe__` method.
    allow_copy
        Allow memory to be copied to perform the conversion. If set to False, causes
        conversions that are not zero-copy to fail.
    __dataframe__z`df` of type z4 does not support the dataframe interchange protocolr   )

isinstanceplr   r   Z_dfhasattrr   	TypeError_from_dataframer   )r   r   msg r&   X/home/app/Keep/.python/lib/python3.10/site-packages/polars/interchange/from_dataframe.pyfrom_dataframe   s   


r(   InterchangeDataFramec                C  sP   g }|   D ]}t||d}|| q|s!t| |d}|| tj|ddS )Nr   F)Zrechunk)Z
get_chunks_protocol_df_chunk_to_polarsappendFconcat)r   r   chunkschunkZpolars_chunkr&   r&   r'   r$   5   s   
r$   c                C  s   g }t |  |  ddD ].\}}t|j}|tkr!t||d}n|tkr,t||d}nt	|||d}|
|| qt|S )NT)strictr   )zipZget_columnsZcolumn_namesr   dtyper   _string_column_to_seriesr   _categorical_column_to_series_column_to_seriesr+   aliasr!   r   )r   r   columnscolumnnamer2   sr&   r&   r'   r*   C   s   

r*   r8   r   r2   r   r   c                C  s\   |   }| j}tg |d |  |R d|i}t|d | ||||d}tjj|||dS )Ndatar   validityr   r;   r<   )get_buffersoffset_construct_data_buffersize_construct_validity_bufferr!   r   _from_buffers)r8   r2   r   buffersr?   data_buffervalidity_bufferr&   r&   r'   r5   T   s   r5   c                C  s   |   dkrtjtdS |sd}t||  }| j}|d }|d u r)d}t|tg ||R d|i}|d \}}t	|||j
d|d}	|	|g}
tjjt|
d d	}t|d
 | t|||d}|d urktjjt|
|d	}|S )Nr   r2   z string buffers must be convertedoffsetsz5cannot create String column without an offsets bufferr   r;   )r?   r   r=   r<   r   )rA   r!   r   r   r   r>   r?   RuntimeError_construct_offsets_bufferr@   bufsizerC   rB   )r8   r   r%   rD   r?   Zoffsets_buffer_infoZoffsets_bufferbufferr2   rE   Zdata_buffersr;   rF   r&   r&   r'   r3   c   s@   r3   c                C  s
  | j }|d sd}t||d }| dkrtg }n|jd tjkr*d}t|t||d}t|}|  }| j	}t
g |d |  |R d|i}	t|d	 | ||	||d}
|	j}tjj||	|
d
}|tkr|s{|  dkr{d| d}t||t}||S )NZis_dictionaryz1non-dictionary categoricals are not yet supported
categoriesr   z'non-string categories are not supportedr   r;   r   r<   r=   zdata buffer must be cast from z
 to UInt32)Zdescribe_categoricalNotImplementedErrorrA   r   r2   r   STRINGr3   r>   r?   r@   rB   r!   r   rC   r
   r   cast)r8   r   Zcategoricalr%   Zcategories_colr2   rM   rD   r?   rE   rF   Z
data_dtypeoutr&   r&   r'   r4      sH   


r4   rL   r   r   lengthintr?   c                C  s~   t |}t|}| j||f}|tkr6|d dkr6|dkr"tjtdS |s*d}t|tjjt|| d	tS tjj||| dS )N      r   rG   zBbyte-packed boolean buffer must be converted to bit-packed booleanowner)
r   r   ptrr   r!   r   r   _from_bufferr	   rP   )rL   r2   rR   r?   r   polars_dtypebuffer_infor%   r&   r&   r'   r@      s   r@   c          	      C  sd   t |}t| j|| }| j||f}tjj||| d}|tkr0|s+d| d}t||	t}|S )NrV   z!offsets buffer must be cast from z	 to Int64)
r   r   rK   rX   r!   r   rY   r   r   rP   )	rL   r2   r?   r   rZ   rR   r[   r:   r%   r&   r&   r'   rJ      s   
rJ   validity_buffer_infotuple[Buffer, Dtype] | Nonecolumn_dtyper;   Series | Nonec             
   C  s,  |j \}}|tjks|jdkrd S |tjkr+| d u rd S | d }t||| ||dS |tjkrA| d u r6d S | d }t|||dS |tj	krR|sNd}	t
|	| S |tjkr|s_d}	t
|	t|g}
z| ro|
|}
||
kW S  ty } zd| d|}	t|	|d }~ww d|}	t|	)Nr   r   zbitmask must be constructedz*invalid sentinel value for column of type z: zunsupported null type: )Zdescribe_nullr   ZNON_NULLABLEZ
null_countZUSE_BITMASK'_construct_validity_buffer_from_bitmaskrA   ZUSE_BYTEMASK(_construct_validity_buffer_from_bytemaskZUSE_NANr   Z
is_not_nanZUSE_SENTINELr!   r   Zis_temporalrP   r   r#   rN   )r\   r8   r^   r;   r?   r   Z	null_type
null_valuerL   r%   sentineler&   r&   r'   rB      sL   
	







rB   rb   c                C  s>   | j ||f}tjt|| }|dkr|sd}t|| }|S )Nr   zbitmask must be inverted)rX   r!   r   rY   r   r   )rL   rb   rR   r?   r   r[   r:   r%   r&   r&   r'   r`   #  s   r`   c                C  sL   |sd}t || jd| jf}tjjt|| d}|t}|dkr$| }|S )Nz)bytemask must be converted into a bitmaskr   rV   )	r   rX   rK   r!   r   rY   r	   rP   r   )rL   rb   r   r%   r[   r:   r&   r&   r'   ra   7  s   
ra   )r   r   r   r   r   r   )r   r)   r   r   r   r   )r8   r   r2   r   r   r   r   r   )r8   r   r   r   r   r   )r   )rL   r   r2   r   rR   rS   r?   rS   r   r   r   r   )
rL   r   r2   r   r?   rS   r   r   r   r   )r\   r]   r8   r   r^   r   r;   r   r?   rS   r   r   r   r_   )rL   r   rb   rS   rR   rS   r?   rS   r   r   r   r   )rL   r   rb   rS   r   r   r   r   )4
__future__r   typingr   Zpolars._reexportZ	_reexportr!   Zpolars.functionsZ	functionsr,   Zpolars._utils.variousr   Zpolars.datatypesr   r   r   r   r	   r
   Zpolars.exceptionsr   Zpolars.interchange.dataframer   Zpolars.interchange.protocolr   r   r   Zpolars.interchange.utilsr   r   r   Zpolarsr   r   Zpolars._typingr   r   r   r   r   r)   r(   r$   r*   r5   r3   r4   r@   rJ   rB   r`   ra   r&   r&   r&   r'   <module>   s:     




(2
: