Ë
    ìmxiýL  ã                  óª  — U d dl mZ d dlmZmZ d dlmZmZmZ d dl	Z
d dlZd dlmZ d dlmZ eZeZeZej&                   e
j(                  «        e
j*                  «        e
j,                  «        e
j.                  «       dœej0                   e
j2                  «        e
j4                  «        e
j6                  «        e
j8                  «       dœej:                   e
j<                  «        e
j>                  «        e
j@                  «       dœejB                   e
jD                  «        e
j2                  «       dœejF                  d	 e
jH                  «       iiZ%d
e&d<   ddd„Z'ddd„Z(	 d	 	 	 	 	 dd„Z)	 d	 	 	 	 	 dd„Z*	 d	 	 	 	 	 dd„Z+	 d	 	 	 	 	 dd„Z,d„ Z-d„ Z.	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z/	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z0	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d d„Z1y)!é    )Úannotations)ÚAnyÚTuple)Ú	DtypeKindÚColumnBuffersÚColumnNullTypeN)ÚDtype)é   é   é    é@   )r   r   r   )é   r
   r
   zdict[DtypeKind, dict[int, Any]]Ú_PYARROW_DTYPESc                ó  — t        | t        j                  «      r| S t        | t        j                  «      r t        j                  j	                  | g«      S t        | d«      st        d«      ‚t        | j                  |¬«      |¬«      S )a.  
    Build a ``pa.Table`` from any DataFrame supporting the interchange protocol.

    Parameters
    ----------
    df : DataFrameObject
        Object supporting the interchange protocol, i.e. `__dataframe__`
        method.
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.Table

    Examples
    --------
    >>> import pyarrow
    >>> from pyarrow.interchange import from_dataframe

    Convert a pandas dataframe to a pyarrow table:

    >>> import pandas as pd
    >>> df = pd.DataFrame({
    ...         "n_attendees": [100, 10, 1],
    ...         "country": ["Italy", "Spain", "Slovenia"],
    ...     })
    >>> df
       n_attendees   country
    0          100     Italy
    1           10     Spain
    2            1  Slovenia
    >>> from_dataframe(df)
    pyarrow.Table
    n_attendees: int64
    country: large_string
    ----
    n_attendees: [[100,10,1]]
    country: [["Italy","Spain","Slovenia"]]
    Ú__dataframe__z#`df` does not support __dataframe__)Ú
allow_copy)	Ú
isinstanceÚpaÚTableÚRecordBatchÚfrom_batchesÚhasattrÚ
ValueErrorÚ_from_dataframer   )Údfr   s     úY/home/htdocs/ttos/venv/lib/python3.12/site-packages/pyarrow/interchange/from_dataframe.pyÚfrom_dataframer   ?   ss   € ôT �"”b—h‘hÔØˆ	Ü	�BœŸ™Ô	'Ü�x‰x×$Ñ$ b TÓ*Ð*ä�2�Ô'ÜÐ>Ó?Ð?ä˜2×+Ñ+°zÐ+ÓBØ&0ô2ð 2ó    c                óä   — g }| j                  «       D ]  }t        ||«      }|j                  |«       Œ! |st        | «      }|j                  |«       t        j                  j                  |«      S )a   
    Build a ``pa.Table`` from the DataFrame interchange object.

    Parameters
    ----------
    df : DataFrameObject
        Object supporting the interchange protocol, i.e. `__dataframe__`
        method.
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.Table
    )Ú
get_chunksÚprotocol_df_chunk_to_pyarrowÚappendr   r   r   )r   r   ÚbatchesÚchunkÚbatchs        r   r   r   u   sf   € ð" €GØ—‘“ò ˆÜ,¨U°JÓ?ˆØ�‰�uÕðñ Ü,¨RÓ0ˆØ�‰�uÔä�8‰8× Ñ  Ó)Ð)r   c                óz  — i }| j                  «       D �]  }t        |t        «      st        d|› d�«      ‚||v rt        d|› d�«      ‚| j	                  |«      }|j
                  d   }|t        j                  t        j                  t        j                  t        j                  t        j                  fv rt        ||«      ||<   Œµ|t        j                  k(  rt        ||«      ||<   ŒØ|t        j                  k(  rt!        ||«      ||<   Œût#        d|› d�«      ‚ t$        j&                  j)                  |«      S )a¤  
    Convert interchange protocol chunk to ``pa.RecordBatch``.

    Parameters
    ----------
    df : DataFrameObject
        Object supporting the interchange protocol, i.e. `__dataframe__`
        method.
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.RecordBatch
    zColumn z is not a stringz is not uniquer   z
Data type z not handled yet)Úcolumn_namesr   Ústrr   Úget_column_by_nameÚdtyper   ÚINTÚUINTÚFLOATÚSTRINGÚDATETIMEÚcolumn_to_arrayÚBOOLÚbool_column_to_arrayÚCATEGORICALÚ categorical_column_to_dictionaryÚNotImplementedErrorr   r   Úfrom_pydict)r   r   ÚcolumnsÚnameÚcolr*   s         r   r!   r!   ’   s$  € ð* $&€GØ—‘Ó!ó LˆÜ˜$¤Ô$Ü˜w t fÐ,<Ð=Ó>Ð>Ø�7‰?Ü˜w t f¨NÐ;Ó<Ð<Ø×#Ñ# DÓ)ˆØ—	‘	˜!‘ˆØÜ�M‰MÜ�N‰NÜ�O‰OÜ×ÑÜ×Ñð
ñ 
ô ,¨C°Ó<ˆG�DŠMØ”i—n‘nÒ$Ü0°°jÓAˆG�DŠMØ”i×+Ñ+Ò+Ü<¸SÀ*ÓMˆG�DŠMä%¨
°5°'Ð9IÐ&JÓKÐKð)Lô, �>‰>×%Ñ% gÓ.Ð.r   c                ó¢   — | j                  «       }| j                  }t        ||| j                  «       | j                  | j
                  |«      }|S )a’  
    Convert a column holding one of the primitive dtypes to a PyArrow array.
    A primitive type is one of: int, uint, float, bool (1 bit).

    Parameters
    ----------
    col : ColumnObject
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.Array
    )Úget_buffersr*   Úbuffers_to_arrayÚsizeÚdescribe_nullÚoffset)r9   r   ÚbuffersÚ	data_typeÚdatas        r   r0   r0   Á   sJ   € ð& �o‰oÓ€GØ—	‘	€IÜ˜G YØŸH™H›JØ×-Ñ-ØŸJ™JØ&ó	(€Dð
 €Kr   c                ó4  — | j                  «       }|d   d   d   }|dk(  r|st        d«      ‚| j                  }t        ||| j	                  «       | j
                  | j                  «      }|dk(  r(t        j                  |t        j                  «       «      }|S )aD  
    Convert a column holding boolean dtype to a PyArrow array.

    Parameters
    ----------
    col : ColumnObject
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.Array
    rB   r   r
   zfBoolean column will be casted from uint8 and a copy is required which is forbidden by allow_copy=False)r;   ÚRuntimeErrorr*   r<   r=   r>   r?   ÚpcÚcastr   Úbool_)r9   r   r@   r=   rA   rB   s         r   r2   r2   Þ   s—   € ð$ �o‰oÓ€GØ�6‰?˜1Ñ˜aÑ €Dð ˆq‚y™ÜðAó
ð 	
ð
 —	‘	€IÜ˜G YØŸH™H›JØ×-Ñ-ØŸJ™Jó(€Dð ˆq‚yÜ�w‰w�tœRŸX™X›ZÓ(ˆà€Kr   c                óJ  — |st        d«      ‚| j                  }|d   st        d«      ‚|d   }t        |«      }| j	                  «       }|d   \  }}t        ||| j                  «       | j                  | j                  «      }t        j                  j                  ||«      }	|	S )aV  
    Convert a column holding categorical data to a pa.DictionaryArray.

    Parameters
    ----------
    col : ColumnObject
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.DictionaryArray
    zjCategorical column will be casted from uint8 and a copy is required which is forbidden by allow_copy=FalseÚis_dictionaryz-Non-dictionary categoricals not supported yetÚ
categoriesrB   )rD   Údescribe_categoricalr5   r0   r;   r<   r=   r>   r?   r   ÚDictionaryArrayÚfrom_arrays)
r9   r   ÚcategoricalÚ
cat_columnÚ
dictionaryr@   Ú_rA   ÚindicesÚ
dict_arrays
             r   r4   r4     s·   € ñ$ ÜðAó
ð 	
ð
 ×*Ñ*€Kà�Ò'Ü!Ø;ó=ð 	=ð ˜\Ñ*€JÜ  Ó,€Jð �o‰oÓ€GØ˜6‘?�L€A€yÜ˜w¨	Ø"Ÿx™x›zØ"×0Ñ0Ø"Ÿz™zó+€Gô ×#Ñ#×/Ñ/°¸ÓD€JàÐr   c                ó®   — t        j                  d| «      }|r0|j                  d«      |j                  d«      }}|dk7  r|dz  }||fS t        d| › �«      ‚)z4Parse datetime `format_str` to interpret the `data`.zts([smun]):(.*)r   é   Úsz DateTime kind is not supported: )ÚreÚmatchÚgroupr5   )Ú
format_strÚtimestamp_metaÚunitÚtzs       r   Úparse_datetime_format_strr^   5  se   € ô —X‘XÐ0°*Ó=€NÙØ!×'Ñ'¨Ó*¨N×,@Ñ,@ÀÓ,CˆbˆØ�3Š;ð �C‰KˆDà�Rˆxˆä
Ð @ÀÀÐMÓ
NÐNr   c                óò   — | \  }}}}|t         j                  k(  r%t        |«      \  }}t        j                  ||¬«      S t
        j                  |i «      j                  |d«      }|r|S t        d| › d�«      ‚)z+Map column date type to pyarrow date type. )r]   NzConversion for ú is not yet supported.)r   r/   r^   r   Ú	timestampr   Úgetr5   )rA   ÚkindÚ	bit_widthÚf_stringrQ   r\   r]   Úpa_dtypes           r   Úmap_date_typerg   G  s‚   € à#,Ñ €Dˆ)�X˜qàŒy×!Ñ!Ò!Ü,¨XÓ6‰ˆˆbÜ�|‰|˜D RÔ(Ð(ä"×&Ñ& t¨RÓ0×4Ñ4°YÀÓEˆñ ØˆOä%Ø! ) Ð,BÐCóEð Er   c                óÐ  — | d   \  }}	 | d   \  }}		 | d   \  }
}t        j                  |j                  |j                  |¬«      }|rt        |	||||«      }nt        ||||||«      }t        |«      }|
r¦\  }}}}t        j                  |
j                  |
j                  |
¬«      }|d   dk(  rt        j                  «       }n.|dk(  rt        j                  «       }nt        j                  «       }t        j                  j                  |||||g|¬	«      }|S t        j                  j                  ||||g|¬	«      }|S # t         $ r d}Y �Œ@w xY w# t         $ r d}
Y �ŒIw xY w)
a$  
    Build a PyArrow array from the passed buffer.

    Parameters
    ----------
    buffer : ColumnBuffers
        Dictionary containing tuples of underlying buffers and
        their associated dtype.
    data_type : Tuple[DtypeKind, int, str, str],
        Dtype description of the column as a tuple ``(kind, bit-width, format string,
        endianness)``.
    length : int
        The number of values in the array.
    describe_null: ColumnNullType
        Null representation the column dtype uses,
        as a tuple ``(kind, value)``
    offset : int, default: 0
        Number of elements to offset from the start of the buffer.
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.Array

    Notes
    -----
    The returned array doesn't own the memory. The caller of this function
    is responsible for keeping the memory owner object alive as long as
    the returned PyArrow array is being used.
    rB   ÚvalidityNÚoffsets©ÚbaserU   ÚUr   ©r?   )Ú	TypeErrorr   Úforeign_bufferÚptrÚbufsizeÚvalidity_buffer_from_maskÚvalidity_buffer_nan_sentinelrg   Úlarge_stringÚstringÚArrayÚfrom_buffers)r@   rA   Úlengthr>   r?   r   Ú	data_buffrQ   Úvalidity_buffÚvalidity_dtypeÚoffset_buffÚoffset_dtypeÚdata_pa_bufferÚvalidity_pa_buffÚ
data_dtypeÚoffset_bit_widthÚoffset_pa_bufferÚstring_typeÚarrays                      r   r<   r<   Y  s­  € ðP ˜6‘?�L€IˆqðØ(/°
Ñ(;Ñ%ˆ�~ðØ$+¨IÑ$6Ñ!ˆ�\ô
 ×&Ñ& y§}¡}°i×6GÑ6GØ,5ô7€Nñ Ü4°]Ø5CØ5BØ5;Ø5;Ø5?óAÑô 8¸Ø8AØ8EØ8>Ø8>Ø8BóDÐô ˜yÓ)€JáØ$0Ñ!ˆÐ˜Q ô ×,Ñ,¨[¯_©_Ø-8×-@Ñ-@Ø2=ô?Ðð �Q‰<˜3ÒÜŸ/™/Ó+‰Kà 2Ò%Ü Ÿo™oÓ/‘ä Ÿi™i›k�Ü—‘×%Ñ%ØØØÐ/°Ð@Øð	 &ó 
ˆð €Lô —‘×%Ñ%ØØØ˜~Ð.Øð	 &ó 
ˆð €Løôw ò Ø‹ðûô ò Ø‹ðús"   ŠE “E ÅEÅEÅE%Å$E%c                óŠ  — |\  }}|\  }}	}	}	|t         j                  k(  sJ ‚|t        j                  k(  ry|t        j                  k(  s|t        j
                  k(  �r|dk(  �rt        j                  | j                  | j                  | ¬«      }
|t        j                  k(  rm|st        d«      ‚t        j                  j                  t        j                  «       |d|
g|¬«      }t        j                  |t        j                   «       «      }n7t        j                  j                  t        j                   «       |d|
g|¬«      }|dk(  rt        j"                  |«      }|j%                  «       d   S |t        j
                  k(  r1|dk(  r,t        j                  | j                  | j                  | ¬«      S t'        |› d�«      ‚)a  
    Build a PyArrow buffer from the passed mask buffer.

    Parameters
    ----------
    validity_buff : BufferObject
        Tuple of underlying validity buffer and associated dtype.
    validity_dtype : Dtype
        Dtype description as a tuple ``(kind, bit-width, format string,
        endianness)``.
    describe_null : ColumnNullType
        Null representation the column dtype uses,
        as a tuple ``(kind, value)``
    length : int
        The number of values in the array.
    offset : int, default: 0
        Number of elements to offset from the start of the buffer.
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.Buffer
    Nr   rk   úYTo create a bitmask a copy of the data is required which is forbidden by allow_copy=Falsern   r   ú* null representation is not yet supported.)r   r1   r   ÚNON_NULLABLEÚUSE_BYTEMASKÚUSE_BITMASKr   rp   rq   rr   rD   rw   rx   Úint8rE   rF   rG   Úinvertr@   r5   )r{   r|   r>   ry   r?   r   Ú	null_kindÚsentinel_valÚvalidity_kindrQ   ÚbuffÚmaskÚ	mask_bools                r   rs   rs   Â  s§  € ðB ,Ñ€Iˆ|Ø+Ñ€M�1�a˜ØœIŸN™NÒ*Ð*Ð*à”N×/Ñ/Ò/ð à	”n×1Ñ1Ò	1Ø”^×/Ñ/Ó/°LÀAÓ4Eä× Ñ  ×!2Ñ!2Ø!.×!6Ñ!6Ø&3ô5ˆð œ×3Ñ3Ò3ÙÜ"ðFóð ô —8‘8×(Ñ(¬¯©«°FØ*.°¨Ø06ð )ó 8ˆDô Ÿ™ ¤b§h¡h£jÓ1‰IäŸ™×-Ñ-¬b¯h©h«j¸&Ø/3°T¨lØ5;ð .ó =ˆIð ˜1ÒÜŸ	™	 )Ó,ˆIà× Ñ Ó" 1Ñ%Ð%à	”n×0Ñ0Ò	0°\ÀQÒ5FÜ× Ñ  ×!2Ñ!2Ø!.×!6Ñ!6Ø&3ô5ð 	5ô "ØˆoÐGÐHóJð 	Jr   c                ó$  — |\  }}}}t        |«      }	|\  }
}|
t        j                  k(  r˜|st        d«      ‚|t        j
                  k(  r|dk(  rt        |› d|
› d�«      ‚t        j                  j                  |	|d| g|¬«      }t        j                  |«      }t        j                  |«      }|j                  «       d   S |
t        j                  k(  rš|st        d«      ‚|t        j                  k(  rt        j                   «       }n|	}t        j                  j                  ||d| g|¬«      }t        j"                  ||«      }t        j                  |«      }|j                  «       d   S |
t        j$                  k(  ryt        |› d�«      ‚)	aþ  
    Build a PyArrow buffer from NaN or sentinel values.

    Parameters
    ----------
    data_pa_buffer : pa.Buffer
        PyArrow buffer for the column data.
    data_type : Dtype
        Dtype description as a tuple ``(kind, bit-width, format string,
        endianness)``.
    describe_null : ColumnNullType
        Null representation the column dtype uses,
        as a tuple ``(kind, value)``
    length : int
        The number of values in the array.
    offset : int, default: 0
        Number of elements to offset from the start of the buffer.
    allow_copy : bool, default: True
        Whether to allow copying the memory to perform the conversion
        (if false then zero-copy approach is requested).

    Returns
    -------
    pa.Buffer
    r‡   r   z with r`   Nrn   r   rˆ   )rg   r   ÚUSE_NANrD   r   r-   r5   r   rw   rx   rE   Úis_nanr�   r@   ÚUSE_SENTINELr/   Úint64Úequalr‰   )r   rA   r>   ry   r?   r   rc   rd   rQ   r�   rŽ   r�   Úpyarrow_datar’   Úsentinel_dtypeÚsentinel_arrr“   s                    r   rt   rt     s«  € ðB &Ñ€Dˆ)�Q˜Ü˜yÓ)€JØ+Ñ€Iˆ|ð ”N×*Ñ*Ò*ÙÜðBóð ð
 ”9—?‘?Ò" y°B¢ô &Ø�+˜V I ;Ð.DÐEóGð Gô Ÿ8™8×0Ñ0ØØØ�~Ð&Øð	 1ó ˆLô —9‘9˜\Ó*ˆDÜ—9‘9˜T“?ˆDØ—<‘<“> !Ñ$Ð$ð 
”n×1Ñ1Ò	1ÙÜðBóð ð
 ”9×%Ñ%Ò%ÜŸX™X›Z‰Nà'ˆNÜ—x‘x×,Ñ,¨^Ø-3Ø.2°NÐ-CØ4:ð -ó <ˆô —x‘x ¨lÓ;ˆÜ—I‘I˜lÓ+ˆ	Ø× Ñ Ó" 1Ñ%Ð%à	”n×1Ñ1Ò	1Øä!ØˆoÐGÐHóJð 	Jr   )T)r   ÚDataFrameObjectÚreturnzpa.Table)r   r�   )r   r�   r   Úboolrž   zpa.RecordBatch)r9   ÚColumnObjectr   rŸ   rž   úpa.Array)r9   r    r   rŸ   rž   zpa.DictionaryArray)r   T)r@   r   rA   zTuple[DtypeKind, int, str, str]ry   Úintr>   r   r?   r¢   r   rŸ   rž   r¡   )r{   ÚBufferObjectr|   r	   r>   r   ry   r¢   r?   r¢   r   rŸ   rž   ú	pa.Buffer)r   r£   rA   r	   r>   r   ry   r¢   r?   r¢   r   rŸ   rž   r¤   )2Ú
__future__r   Útypingr   r   Úpyarrow.interchange.columnr   r   r   Úpyarrowr   rW   Úpyarrow.computeÚcomputerE   r	   r�   r    r£   r+   rŒ   Úint16Úint32r˜   r,   Úuint8Úuint16Úuint32Úuint64r-   Úfloat16Úfloat32Úfloat64r1   rG   r.   rv   r   Ú__annotations__r   r   r!   r0   r2   r4   r^   rg   r<   rs   rt   © r   r   ú<module>r¶      sã  ðö$ #÷÷
ñ ó Û 	å Ý ,ð
 €Ø€Ø€ð ‡M�M�w�r—w‘w“yØ ˜Ÿ™›
Ø ˜Ÿ™›
Ø ˜Ÿ™›
ñ$ð ‡N�N˜˜Ÿ™›
Ø"˜Ÿ™›Ø"˜Ÿ™›Ø"˜Ÿ™›ñ&ð ‡O�O˜*˜"Ÿ*™*›,Ø$˜"Ÿ*™*›,Ø$˜"Ÿ*™*›,ñ(ð ‡N�N˜˜Ÿ™›
Ø ˜Ÿ™›
ñ$à×Ñ�q˜)˜"Ÿ)™)›+Ð&ð4€Ð0ó ô$32ôl*ð> ð,/Øð,/àð,/ð ó,/ðb ðØ	ðàðð óð> ð$Ø	ð$àð$ð ó$ðR ð-Ø	ð-àð-ð ó-ò`Oò$Eð. ØðfØðfà.ðfð ðfð "ð	fð
 ðfð ðfð ófð\ ØðLJØðLJàðLJð "ðLJð ð	LJð
 ðLJð ðLJð óLJðh ØðUJØ ðUJàðUJð "ðUJð ð	UJð
 ðUJð ðUJð ôUJr   