a
    DBìj‹D  ã                   @  st  d dl mZ d dlmZmZ d dl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Zd d	lmZmZ d d
lmZ d dlmZmZ d dlmZmZmZmZ d dlm Z m!Z!m"Z" erÐd dlm#Z# ej$ej%ej&ej'ej(ej)ej)dœZ*ej&ej+dfej)ej,e
fej$ej-dfej%ej-dfej'ej-dfej.ej,dfej(ej/d fiZ0ej-dej+dej,diZ1G dd„ deƒZ2dS )é    )Úannotations)ÚTYPE_CHECKINGÚAnyN)Úinfer_dtype)ÚiNaT)ÚNoBufferPresent)Úcache_readonly)ÚBaseMaskedDtype)Ú
ArrowDtypeÚDatetimeTZDtype)Úis_string_dtype)ÚPandasBufferÚPandasBufferPyarrow)ÚColumnÚColumnBuffersÚColumnNullTypeÚ	DtypeKind)ÚArrowCTypesÚ
EndiannessÚdtype_to_arrow_c_fmt)ÚBuffer)ÚiÚuÚfÚbÚUÚMÚméÿÿÿÿzThis column is non-nullablezThis column uses NaN as nullz!This column uses a sentinel valuec                   @  sö   e Zd ZdZd0ddddœdd„Zd	d
œdd„Zed	d
œdd„ƒZedd
œdd„ƒZ	dd
œdd„Z
edd„ ƒZedd„ ƒZed	d
œdd„ƒZedd
œdd„ƒZd	d
œdd„Zd1d d!œd"d#„Zd$d
œd%d&„Zd'd
œd(d)„Zd*d
œd+d,„Zd-d
œd.d/„ZdS )2ÚPandasColumnaö  
    A column object, with only the methods and properties required by the
    interchange protocol defined.
    A column can contain one or more chunks. Each chunk can contain up to three
    buffers - a data buffer, a mask buffer (depending on null representation),
    and an offsets buffer (if variable-size binary; e.g., variable-length
    strings).
    Note: this Column object can only be produced by ``__dataframe__``, so
          doesn't need its own version or ``__column__`` protocol.
    Tz	pd.SeriesÚboolÚNone)ÚcolumnÚ
allow_copyÚreturnc                 C  sN   t |tjƒrtd|j› d�ƒ‚t |tjƒs>tdt|ƒ› d�ƒ‚|| _|| _	dS )zu
        Note: doesn't deal with extension arrays yet, just assume a regular
        Series/ndarray for now.
        z·Expected a Series, got a DataFrame. This likely happened because you called __dataframe__ on a DataFrame which, after converting column names to string, resulted in duplicated names: zD. Please rename these columns before using the interchange protocol.zColumns of type ú not handled yetN)
Ú
isinstanceÚpdZ	DataFrameÚ	TypeErrorÚcolumnsÚSeriesÚNotImplementedErrorÚtypeÚ_colÚ_allow_copy)Úselfr"   r#   © r0   ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/interchange/column.pyÚ__init__T   s    ýÿzPandasColumn.__init__Úint)r$   c                 C  s   | j jS )z2
        Size of the column, in elements.
        )r-   Úsize©r/   r0   r0   r1   r4   h   s    zPandasColumn.sizec                 C  s   dS )z7
        Offset of first element. Always zero.
        r   r0   r5   r0   r0   r1   Úoffsetn   s    zPandasColumn.offsetztuple[DtypeKind, int, str, str]c                 C  s„   | j j}t|tjƒrB| j jj}|  |j¡\}}}}tj	||t
jfS t|ƒrvt| j ƒdv rltjdt|ƒt
jfS tdƒ‚n
|  |¡S d S )N)ÚstringÚemptyé   z.Non-string object dtypes are not supported yet)r-   Údtyper&   r'   ZCategoricalDtypeÚvaluesÚcodesÚ_dtype_from_pandasdtyper   ÚCATEGORICALr   ÚNATIVEr   r   ÚSTRINGr   r+   )r/   r:   r<   Ú_ZbitwidthZc_arrow_dtype_f_strr0   r0   r1   r:   v   s.    

ûüü
zPandasColumn.dtypec                 C  s–   t  |jd¡}|du r&td|› d�ƒ‚t|tƒr:|jj}n.t|tƒrN|j	j}nt|t
ƒrb|jj}n|j}|dkr€||jtj|fS ||jd t|ƒ|fS )z/
        See `self.dtype` for details.
        Nú
Data type z& not supported by interchange protocolzbool[pyarrow]r9   )Ú	_NP_KINDSÚgetÚkindÚ
ValueErrorr&   r
   Znumpy_dtypeÚ	byteorderr   Úbaser	   Úitemsizer   ÚBOOLr   )r/   r:   rE   rG   r0   r0   r1   r=   ”   s"    





üz$PandasColumn._dtype_from_pandasdtypec                 C  s:   | j d tjkstdƒ‚| jjjdtt 	| jjj
¡ƒdœS )a:  
        If the dtype is categorical, there are two options:
        - There are only values in the data buffer.
        - There is a separate non-categorical Column encoding for categorical values.

        Raises TypeError if the dtype is not categorical

        Content of returned dict:
            - "is_ordered" : bool, whether the ordering of dictionary indices is
                             semantically meaningful.
            - "is_dictionary" : bool, whether a dictionary-style mapping of
                                categorical values to other objects exists
            - "categories" : Column representing the (implicit) mapping of indices to
                             category values (e.g. an array of cat1, cat2, ...).
                             None if not a dictionary-style categorical.
        r   zCdescribe_categorical only works on a column with categorical dtype!T)Z
is_orderedZis_dictionaryÚ
categories)r:   r   r>   r(   r-   ÚcatÚorderedr   r'   r*   rK   r5   r0   r0   r1   Údescribe_categoricalµ   s    ÿýz!PandasColumn.describe_categoricalc                 C  s¤   t | jjtƒr tj}d}||fS t | jjtƒr^| jjjj	d  
¡ d d u rTtjd fS tjdfS | jd }zt| \}}W n" tyš   td|› d�ƒ‚Y n0 ||fS )Né   r   rB   z not yet supported)r&   r-   r:   r	   r   ÚUSE_BYTEMASKr
   ÚarrayÚ	_pa_arrayÚchunksÚbuffersÚNON_NULLABLEZUSE_BITMASKÚ_NULL_DESCRIPTIONÚKeyErrorr+   )r/   Zcolumn_null_dtypeZ
null_valuerE   ÚnullÚvaluer0   r0   r1   Údescribe_nullÒ   s    


zPandasColumn.describe_nullc                 C  s   | j  ¡  ¡  ¡ S )zB
        Number of null elements. Should always be known.
        )r-   ZisnaÚsumÚitemr5   r0   r0   r1   Ú
null_countæ   s    zPandasColumn.null_countzdict[str, pd.Index]c                 C  s   d| j jiS )z8
        Store specific metadata of the column.
        zpandas.index)r-   Úindexr5   r0   r0   r1   Úmetadataí   s    zPandasColumn.metadatac                 C  s   dS )zE
        Return the number of chunks the column consists of.
        rO   r0   r5   r0   r0   r1   Ú
num_chunksô   s    zPandasColumn.num_chunksNz
int | None)Ún_chunksc                 c  sr   |rh|dkrht | jƒ}|| }|| dkr2|d7 }td|| |ƒD ]"}t| jj||| … | jƒV  qBn| V  dS )zy
        Return an iterator yielding the chunks.
        See `DataFrame.get_chunks` for details on ``n_chunks``.
        rO   r   N)Úlenr-   Úranger   Zilocr.   )r/   ra   r4   ÚstepÚstartr0   r0   r1   Ú
get_chunksú   s    
ÿ
zPandasColumn.get_chunksr   c                 C  s\   |   ¡ dddœ}z|  ¡ |d< W n ty2   Y n0 z|  ¡ |d< W n tyV   Y n0 |S )a`  
        Return a dictionary containing the underlying buffers.
        The returned dictionary has the following contents:
            - "data": a two-element tuple whose first element is a buffer
                      containing the data and whose second element is the data
                      buffer's associated dtype.
            - "validity": a two-element tuple whose first element is a buffer
                          containing mask values indicating missing data and
                          whose second element is the mask value buffer's
                          associated dtype. None if the null representation is
                          not a bit or byte mask.
            - "offsets": a two-element tuple whose first element is a buffer
                         containing the offset values for variable-size binary
                         data (e.g., variable-length strings) and whose second
                         element is the offsets buffer's associated dtype. None
                         if the data buffer does not have an associated offsets
                         buffer.
        N)ÚdataÚvalidityÚoffsetsrh   ri   )Ú_get_data_bufferÚ_get_validity_bufferr   Ú_get_offsets_buffer)r/   rT   r0   r0   r1   Úget_buffers  s    ýzPandasColumn.get_buffersz.tuple[Buffer, tuple[DtypeKind, int, str, str]]c           	      C  s†  | j d tjtjtjtjtjfv rÌ| j }| j d tjkr^t| j d ƒdkr^| jj	 
d¡ ¡ }n^| jj}t| jj tƒr||j}n@t| jj tƒr¶|jjd }t| ¡ d t|ƒd�}||fS |j}t|| jd�}n²| j d tjk�r| jjj}t|| jd�}|  |j ¡}nz| j d tjk�rj| j ¡ }tƒ }|D ]$}t|tƒ�r*| |j dd	�¡ �q*tt!j"|d
d�ƒ}| j }nt#d| jj › d�ƒ‚||fS )zZ
        Return the buffer containing the data and the buffer's associated dtype.
        r   é   é   NrO   ©Úlength)r#   úutf-8©ÚencodingZuint8)r:   rB   r%   )$r:   r   ÚINTÚUINTÚFLOATrJ   ÚDATETIMErb   r-   ÚdtZ
tz_convertÚto_numpyrQ   r&   r	   Ú_datar
   rR   rS   r   rT   Z_ndarrayr   r.   r>   r;   Ú_codesr=   r@   Ú	bytearrayÚstrÚextendÚencodeÚnpZ
frombufferr+   )	r/   r:   Znp_arrÚarrÚbufferr<   Úbufr   Úobjr0   r0   r1   rj   0  sH    û	"
þ

zPandasColumn._get_data_bufferztuple[Buffer, Any] | Nonec                 C  sp  | j \}}t| jjtƒrn| jjjjd }tj	dt
j	tjf}| ¡ d du rNdS t| ¡ d t|ƒd�}||fS t| jjtƒr¨| jjj}t|ƒ}tj	dt
j	tjf}||fS | jd tjk�r4| j ¡ }|dk}| }tjt|ƒftjd�}t|ƒD ] \}	}
t|
tƒ�r|n|||	< qðt|ƒ}tj	dt
j	tjf}||fS zt| › d�}W n t�yb   tdƒ‚Y n0 t|ƒ‚dS )	zÒ
        Return the buffer containing the mask values indicating missing data and
        the buffer's associated dtype.
        Raises NoBufferPresent if null representation is not a bit or byte mask.
        r   rO   Nrp   r9   ©Úshaper:   z! so does not have a separate maskzSee self.describe_null)rZ   r&   r-   r:   r
   rQ   rR   rS   r   rJ   r   r   r?   rT   r   rb   r	   Z_maskr   r@   rz   r�   ÚzerosZbool_Ú	enumerater~   Ú_NO_VALIDITY_BUFFERrW   r+   r   )r/   rX   Úinvalidr‚   r:   rƒ   Úmaskr„   Zvalidr   r…   Úmsgr0   r0   r1   rk   n  s>    

þ

z!PandasColumn._get_validity_bufferztuple[PandasBuffer, Any]c           	      C  s¤   | j d tjkr”| j ¡ }d}tjt|ƒd ftjd�}t	|ƒD ]6\}}t
|tƒrj|jdd�}|t|ƒ7 }|||d < q@t|ƒ}tjdtjtjf}ntdƒ‚||fS )a  
        Return the buffer containing the offset values for variable-size binary
        data (e.g., variable-length strings) and the buffer's associated dtype.
        Raises NoBufferPresent if the data buffer does not have an associated
        offsets buffer.
        r   rO   r†   rr   rs   é@   zJThis column has a fixed-length dtype so it does not have an offsets buffer)r:   r   r@   r-   rz   r�   rˆ   rb   Zint64r‰   r&   r~   r€   r   ru   r   ZINT64r   r?   r   )	r/   r;   Zptrri   r   Úvr   rƒ   r:   r0   r0   r1   rl   §  s&    

üÿz PandasColumn._get_offsets_buffer)T)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r4   Úpropertyr6   r   r:   r=   rN   rZ   r]   r_   r`   rf   rm   rj   rk   rl   r0   r0   r0   r1   r   H   s,   !

%>9r   )3Ú
__future__r   Útypingr   r   Únumpyr�   Zpandas._libs.libr   Zpandas._libs.tslibsr   Zpandas.errorsr   Zpandas.util._decoratorsr   Zpandas.core.dtypes.dtypesr	   Zpandasr'   r
   r   Zpandas.api.typesr   Zpandas.core.interchange.bufferr   r   Z*pandas.core.interchange.dataframe_protocolr   r   r   r   Zpandas.core.interchange.utilsr   r   r   r   ru   rv   rw   rJ   r@   rx   rC   ZUSE_NANZUSE_SENTINELrU   r>   rP   rV   rŠ   r   r0   r0   r0   r1   Ú<module>   sH   ùöý