
    AHj*                       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 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r d 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*  ed      dddd       Z+ddZ,	 	 	 	 	 	 ddZ-	 	 	 	 	 	 	 	 ddZ.d dZ/d dZ0	 d!	 	 	 	 	 	 	 	 	 	 	 d"dZ1	 	 	 	 	 	 	 	 	 	 d#dZ2	 d!	 	 	 	 	 	 	 	 	 	 	 	 	 d$dZ3	 d!	 	 	 	 	 	 	 	 	 	 	 d%dZ4	 	 	 	 	 	 	 	 d&dZ5y)'    )annotations)TYPE_CHECKINGN)
deprecated)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   zQSupport for the dataframe interchange protocol is deprecated since version 1.40.0T
allow_copyc                   t        | t        j                        r| S t        | t              r| j                  S t        | d      sdt        |       d}t        |      t        | j                  |      |      S )a  
    Build a Polars DataFrame from any dataframe supporting the interchange protocol.

    .. deprecated:: 1.40.0
        Support for the Dataframe Interchange Protocol is deprecated.

    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   _dfhasattrr   	TypeError_from_dataframer   )dfr   msgs      Y/root/tools/cai/cai_env/lib/python3.12/site-packages/polars/interchange/from_dataframe.pyfrom_dataframer)      su    & "bll#		B	(vv2'1"588lmn
J/     c                   g }| j                         D ]   }t        ||      }|j                  |       " |st        | |      }|j                  |       t        j                  |d      S )Nr   F)rechunk)
get_chunks_protocol_df_chunk_to_polarsappendFconcat)r&   r   chunkschunkpolars_chunks        r(   r%   r%   <   sb    F $3EjQl#$
 3B:Nl#88FE**r*   c               v   g }t        | j                         | j                         d      D ]v  \  }}t        |j                        }|t
        k(  rt        ||      }n%|t        k(  rt        ||      }nt        |||      }|j                  |j                  |             x t        j                  |      S )NT)strictr   )zipget_columnscolumn_namesr   dtyper
   _string_column_to_seriesr   _categorical_column_to_series_column_to_seriesr/   aliasr!   r   )r&   r   columnscolumnnamer:   ss          r(   r.   r.   J   s     GBNN,boo.?M &%fll3F?(JGAd]-fLA!&%JGAqwwt}%& <<  r*   c                   | j                         }| j                  }t        g |d   | j                         |d|i}t	        |d   | ||||      }t
        j                  j                  |||      S )Ndatar   validityr   rD   rE   )get_buffersoffset_construct_data_buffersize_construct_validity_bufferr!   r   _from_buffers)r@   r:   r   buffersrH   data_buffervalidity_buffers          r(   r=   r=   [   s       "G]]F( 	 ++-)/<FK 1
VUKJO 99""5{_"UUr*   c                  | j                         dk(  rt        j                  t              S |sd}t	        |      | j                         }| j                  }|d   }|d}t        |      t        g ||d|i}|d   \  }}t        |||j                  d|      }	|	|g}
t        j                  j                  t        |
d 	      }t        |d
   | t        |||      }|&t        j                  j                  t        |
|	      }|S )Nr   r:   z string buffers must be convertedoffsetsz5cannot create String column without an offsets bufferr   rD   )rH   r   rF   rE   r   )rJ   r!   r   r
   r   rG   rH   RuntimeError_construct_offsets_bufferrI   bufsizerL   rK   )r@   r   r'   rM   rH   offsets_buffer_infooffsets_bufferbufferr:   rN   data_buffersrD   rO   s                r(   r;   r;   j   s(   {{}yyv&&0!#&&  "G]]F!),"E3. 	$1;N FOMFE(v~~aJK  0L99""6t"LD 1
VVT6jO "yy&& ' 
 Kr*   c                  | j                   }|d   sd}t        |      |d   }|j                         dk(  rt        g       }nE|j                  d   t
        j                  k7  rd}t        |      t        ||      }t        |      }| j                         }| j                  }t        g |d   | j                         |d|i}	t        |d	   | ||	||      }
|	j                  }t        j                  j                  ||	|

      }|t        k7  r;|s$| j                         dkD  rd| d}t!        |      |j#                  t              }|j#                  |      S )Nis_dictionaryz1non-dictionary categoricals are not yet supported
categoriesr   z'non-string categories are not supportedr   rD   r   rE   rF   zdata buffer must be cast from z
 to UInt32)describe_categoricalNotImplementedErrorrJ   r   r:   r   STRINGr;   rG   rH   rI   rK   r!   r   rL   r   r   cast)r@   r   categoricalr'   categories_colr:   r\   rM   rH   rN   rO   
data_dtypeouts                r(   r<   r<      so   --K'A!#&& .N!R			a	 I$4$4	47!#&&-nT
Z   "G]]F( 	 ++-)/<FK 1
VUKJO ""J
))
!
! " C
 Vfkkma/2:,jIC%c** hhv88E?r*   c                  t        |      }t        |      }| j                  ||f}|t        k(  ro|d   dk(  rg|dk(  rt	        j
                  t              S |sd}t        |      t        j
                  j                  t        ||       j                  t              S t        j
                  j                  |||       S )N      r   rQ   zBbyte-packed boolean buffer must be converted to bit-packed booleanowner)
r   r   ptrr   r!   r   r   _from_bufferr   r`   )rX   r:   lengthrH   r   polars_dtypebuffer_infor'   s           r(   rI   rI      s     )/L
 5\BL::vv.K w58q=Q;997++VC%c**yy%%e[%GLLWUU99!!,6!JJr*   c                  t        |      }t        | j                  |      |z
  }| j                  ||f}t        j
                  j                  |||       }|t        k7  r(|sd| d}t        |      |j                  t              }|S )Nrh   z!offsets buffer must be cast from z	 to Int64)
r   r   rU   rj   r!   r   rk   r	   r   r`   )	rX   r:   rH   r   rm   rl   rn   rB   r'   s	            r(   rT   rT      s     )/L*6>>5AFJF::vv.K
		|[GA u5l^9MC%c**FF5MHr*   c                  |j                   \  }}|t        j                  k(  s|j                  dk(  ry |t        j                  k(  r&| y | d   }t        |||j                         ||      S |t        j                  k(  r| y | d   }t        |||      S |t        j                  k(  r|sd}	t        |	      |j                         S |t        j                  k(  rL|sd}	t        |	      t        j                  |g      }
	 |j                         r|
j!                  |      }
||
k7  S d|}	t'        |	      # t"        $ r}d| d|}	t%        |	      |d }~ww xY w)Nr   r   zbitmask must be constructedz*invalid sentinel value for column of type z: zunsupported null type: )describe_nullr   NON_NULLABLE
null_countUSE_BITMASK'_construct_validity_buffer_from_bitmaskrJ   USE_BYTEMASK(_construct_validity_buffer_from_bytemaskUSE_NANr   
is_not_nanUSE_SENTINELr!   r   is_temporalr`   r   r$   r^   )validity_buffer_infor@   column_dtyperD   rH   r   	null_type
null_valuerX   r'   sentineles               r(   rK   rK      s|    #00IzN///63D3D3I	n00	0'%a(6Jv*
 	
 
n11	1'%a(7J:
 	
 
n,,	,/C%c**  	n11	1/C%c**99j\*	('')#==68## (	}5!#&& % 	(>|nBzn]CC.a'	(s    %D6 6	E?EEc                   | j                   ||f}t        j                  j                  t        ||       }|dk7  r|sd}t        |      | }|S )Nr   zbitmask must be inverted)rj   r!   r   rk   r   r   )rX   r   rl   rH   r   rn   rB   r'   s           r(   ru   ru   *  sR     ::vv.K
		wV<AQ,C%c**BHr*   c                   |sd}t        |      | j                  d| j                  f}t        j                  j                  t        ||       }|j                  t              }|dk7  r| }|S )Nz)bytemask must be converted into a bitmaskr   rh   )	r   rj   rU   r!   r   rk   r   r`   r   )rX   r   r   r'   rn   rB   s         r(   rw   rw   >  sf     9!#&&::q&..1K
		uk@A	wAQBHr*   )r&   r   r   boolreturnr   )r&   InterchangeDataFramer   r   r   r   )r@   r   r:   r   r   r   r   r   )r@   r   r   r   r   r   )r   )rX   r   r:   r   rl   intrH   r   r   r   r   r   )
rX   r   r:   r   rH   r   r   r   r   r   )r|   ztuple[Buffer, Dtype] | Noner@   r   r}   r   rD   r   rH   r   r   r   r   zSeries | None)rX   r   r   r   rl   r   rH   r   r   r   r   r   )rX   r   r   r   r   r   r   r   )6
__future__r   typingr   polars._reexport	_reexportr!   polars.functions	functionsr0   polars._utils.deprecationr   polars._utils.variousr   polars.datatypesr   r   r	   r
   r   r   polars.exceptionsr   polars.interchange.dataframer   polars.interchange.protocolr   r   r   polars.interchange.utilsr   r   r   polarsr   r   polars._typingr   r   r   r   r   r   r)   r%   r.   r=   r;   r<   rI   rT   rK   ru   rw    r*   r(   <module>r      s   "     0 5 H H 3 8 V V  (-VVM W CG >+!!-1!!"VV)V:>VV%P+d 	KKK K 	K K K: 
  8 3'53'3' !3' 	3'
 3' 3' 3't 	  	  ( 	
 r*   