a
    DBìj»  ã                   @  sÞ   d Z ddlmZ ddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlZejrZddlmZ ddd	d
dddd
ddddddddddddddddddd d!d"d#d$œZG d%d&„ d&ƒZG d'd(„ d(ƒZd)d*d+œd,d-„Zd.d/d0d1œd2d3„ZdS )4zE
Utility functions and objects for implementing the interchange API.
é    )ÚannotationsN)Úlib)Ú
ArrowDtypeÚCategoricalDtypeÚDatetimeTZDtype)ÚDtypeObjÚnÚbÚCÚSÚIÚLÚcÚiÚlÚeÚfÚgÚuÚUÚzZttsZttmZttuZttnÚtdDÚtdmztss:ztsm:ztsu:ztsn:ZtDsZtDmZtDuZtDn)ÚnullÚboolZuint8Zuint16Zuint32Zuint64Zint8Zint16Zint32Zint64Z	halffloatÚfloatÚdoubleÚstringZlarge_stringÚbinaryz	time32[s]z
time32[ms]z
time64[us]z
time64[ns]zdate32[day]z
date64[ms]ztimestamp[s]ztimestamp[ms]ztimestamp[us]ztimestamp[ns]zduration[s]zduration[ms]zduration[us]zduration[ns]c                   @  s\   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdS )ÚArrowCTypesz¹
    Enum for Apache Arrow C type format strings.

    The Arrow C data interface:
    https://arrow.apache.org/docs/format/CDataInterface.html#data-type-description-format-strings
    r   r	   r   r
   Úsr   r   r   r   r   r   r   r   r   r   r   r   zts{resolution}:{tz}ztt{resolution}N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZNULLÚBOOLZINT8ZUINT8ZINT16ZUINT16ZINT32ZUINT32ÚINT64ZUINT64ZFLOAT16ZFLOAT32ZFLOAT64ÚSTRINGZLARGE_STRINGZDATE32ZDATE64Ú	TIMESTAMPZTIME© r)   r)   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/interchange/utils.pyr   =   s(   r   c                   @  s    e Zd ZdZdZdZdZdZdS )Ú
Endiannessz.Enum indicating the byte-order of a data-type.ú<ú>ú=ú|N)r!   r"   r#   r$   ZLITTLEZBIGZNATIVEZNAr)   r)   r)   r*   r+   _   s
   r+   r   Ústr)ÚdtypeÚreturnc                 C  sR  t | tƒrtjS | t d¡kr$tjS t | tƒr¦ddl}| j	}|j
 |¡r\d|j› d|j› �S |j
 |¡rŠ|jdurŠd|jd › d|j› �S t t|ƒd¡}|dur¦|S tt| j ¡ dƒ}|durÄ|S t | tjƒrÖtjS t | d¡�rt | ¡d d }tjj|d	d
�S t | tƒ�r*tjj| jd | jd
�S t | tjƒ�r>tj S t!d| › d�ƒ‚dS )a   
    Represent pandas `dtype` as a format string in Apache Arrow C notation.

    Parameters
    ----------
    dtype : np.dtype
        Datatype of pandas DataFrame to represent.

    Returns
    -------
    str
        Format string in Apache Arrow C notation of the given `dtype`.
    ÚOr   Nzd:ú,Útsú:ÚMÚ )Ú
resolutionÚtzzConversion of z- to Arrow C format string is not implemented.)"Ú
isinstancer   r   r&   Únpr1   r'   r   ZpyarrowZpyarrow_dtypeÚtypesZ
is_decimalÚ	precisionÚscaleZis_timestampr:   ÚunitÚPYARROW_CTYPESÚgetr0   ÚgetattrÚnameÚupperÚpdZStringDtyper   Zis_np_dtypeZdatetime_datar(   Úformatr   ZBooleanDtyper%   ÚNotImplementedError)r1   ÚpaZpa_typeÚ
format_strr9   r)   r)   r*   Údtype_to_arrow_c_fmth   s:    


ÿrK   z	pd.Seriesr   zpd.Series | None)ÚseriesÚ
allow_copyr2   c                C  sX   t | jtjƒsdS | jj}t|jƒdkr,dS |s8tdƒ‚| 	¡ }tj
|| j| j| jd�S )aË  
    Rechunk a multi-chunk pyarrow array into a single-chunk array, if necessary.

    - Returns `None` if the input series is not backed by a multi-chunk pyarrow array
      (and so doesn't need rechunking)
    - Returns a single-chunk-backed-Series if the input is backed by a multi-chunk
      pyarrow array and `allow_copy` is `True`.
    - Raises a `RuntimeError` if `allow_copy` is `False` and input is a
      based by a multi-chunk pyarrow array.
    Né   zŒFound multi-chunk pyarrow array, but `allow_copy` is False. Please rechunk the array before calling this function, or set `allow_copy=True`.)r1   rD   Úindex)r;   r1   rF   r   ÚarrayZ	_pa_arrayÚlenÚchunksÚRuntimeErrorZcombine_chunksZSeriesrD   rO   )rL   rM   Zchunked_arrayZarrr)   r)   r*   Úmaybe_rechunk    s    ÿrT   )r$   Ú
__future__r   ÚtypingÚnumpyr<   Zpandas._libsr   Zpandas.core.dtypes.dtypesr   r   r   ZpandasrF   ÚTYPE_CHECKINGZpandas._typingr   rA   r   r+   rK   rT   r)   r)   r)   r*   Ú<module>   sV   â""	8