o
    „ôjY\  ã                   @  s®  d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	 ddl
Z
ddl
mZ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 ddlmZ ddlmZ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' er†ddl(m)Z)m*Z*m+Z+m,Z,m-Z- dEdd„Z.			dFdGd$d%„Z/G d&d„ dƒZ0G d'd(„ d(e0ƒZ1G d)d*„ d*e0ƒZ2eed d+�		,	-				dHdId9d:„ƒZ3eed d+�d,ddej4ej4ddfdJdCdD„ƒZ5dS )Kz parquet compat é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚLiteral)Úcatch_warningsÚfilterwarnings)Ú_get_option)Úlib)Úimport_optional_dependency©ÚAbstractMethodError)Údoc)Úfind_stack_level)Úcheck_dtype_backend)Ú	DataFrameÚ
get_option)Ú_shared_docs)Úarrow_table_to_pandas)Ú	IOHandlesÚ
get_handleÚis_fsspec_urlÚis_urlÚstringify_path)ÚDtypeBackendÚFilePathÚ
ReadBufferÚStorageOptionsÚWriteBufferÚengineÚstrÚreturnÚBaseImplc                 C  s    | dkrt dƒ} | dkr>ttg}d}|D ]"}z|ƒ W   S  ty6 } z|dt|ƒ 7 }W Y d}~qd}~ww td|› �ƒ‚| dkrEtƒ S | dkrLtƒ S td	ƒ‚)
zreturn our implementationÚautozio.parquet.engineÚ z
 - NzÉUnable to find a usable engine; tried using: 'pyarrow', 'fastparquet'.
A suitable version of pyarrow or fastparquet is required for parquet support.
Trying to import the above resulted in these errors:ÚpyarrowÚfastparquetz.engine must be one of 'pyarrow', 'fastparquet')r   ÚPyArrowImplÚFastParquetImplÚImportErrorr   Ú
ValueError)r   Zengine_classesÚ
error_msgsZengine_classÚerr© r,   úd/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/pandas/io/parquet.pyÚ
get_engine4   s,   €ÿúÿ
r.   ÚrbFÚpathú1FilePath | ReadBuffer[bytes] | WriteBuffer[bytes]Úfsr   Ústorage_optionsúStorageOptions | NoneÚmodeÚis_dirÚboolúVtuple[FilePath | ReadBuffer[bytes] | WriteBuffer[bytes], IOHandles[bytes] | None, Any]c           
   	   C  s\  t | ƒ}|dur;tddd�}tddd�}|dur%t||jƒr%|r$tdƒ‚n|dur1t||jjƒr1n
tdt|ƒj	› �ƒ‚t
|ƒr}|du r}|du rftdƒ}tdƒ}z
|j | ¡\}}W n t|jfye   Y nw |du r|tdƒ}|jj|fi |pwi ¤Ž\}}n|r‹t|ƒr‡|d	kr‹td
ƒ‚d}	|s©|s©t|tƒr©tj |¡s©t||d|d�}	d}|	j}||	|fS )zFile handling for PyArrow.Nz
pyarrow.fsÚignore)ÚerrorsÚfsspecz8storage_options not supported with a pyarrow FileSystem.z9filesystem must be a pyarrow or fsspec FileSystem, not a r$   r/   z8storage_options passed with buffer, or non-supported URLF©Úis_textr3   )r   r
   Ú
isinstanceÚ
FileSystemÚNotImplementedErrorÚspecZAbstractFileSystemr)   ÚtypeÚ__name__r   Úfrom_uriÚ	TypeErrorÚArrowInvalidÚcoreZ	url_to_fsr   r   Úosr0   Úisdirr   Úhandle)
r0   r2   r3   r5   r6   Úpath_or_handleÚpa_fsr;   ÚpaÚhandlesr,   r,   r-   Ú_get_path_or_handleV   sf   
ÿÿÿÿÿÿ
ÿ€ÿþý
ü	ÿ
rO   c                   @  s0   e Zd Zeddd„ƒZddd„Zddd
d„Zd	S )r!   Údfr   r    ÚNonec                 C  s   t | tƒs	tdƒ‚d S )Nz+to_parquet only supports IO with DataFrames)r>   r   r)   )rP   r,   r,   r-   Úvalidate_dataframe–   s   
ÿzBaseImpl.validate_dataframec                 K  ó   t | ƒ‚©Nr   )ÚselfrP   r0   ÚcompressionÚkwargsr,   r,   r-   Úwrite›   ó   zBaseImpl.writeNc                 K  rS   rT   r   )rU   r0   ÚcolumnsrW   r,   r,   r-   Úreadž   rY   zBaseImpl.read)rP   r   r    rQ   )rP   r   rT   )r    r   )rC   Ú
__module__Ú__qualname__ÚstaticmethodrR   rX   r[   r,   r,   r,   r-   r!   •   s
    
c                   @  sF   e Zd Zddd„Z					dddd„Zdddejddfddd„ZdS ) r&   r    rQ   c                 C  s&   t ddd� dd l}dd l}|| _d S )Nr$   z(pyarrow is required for parquet support.©Úextrar   )r
   Zpyarrow.parquetÚ(pandas.core.arrays.arrow.extension_typesÚapi)rU   r$   Úpandasr,   r,   r-   Ú__init__£   s   ÿ
zPyArrowImpl.__init__ÚsnappyNrP   r   r0   úFilePath | WriteBuffer[bytes]rV   ú
str | NoneÚindexúbool | Noner3   r4   Úpartition_colsúlist[str] | Nonec                 K  sL  |   |¡ d| dd ¡i}	|d ur||	d< | jjj|fi |	¤Ž}
|jr:dt |j¡i}|
jj	}i |¥|¥}|
 
|¡}
t|||d|d ud�\}}}t|tjƒrjt|dƒrjt|jttfƒrjt|jtƒrg|j ¡ }n|j}z1|d ur€| jjj|
|f|||dœ|¤Ž n| jjj|
|f||dœ|¤Ž W |d urš| ¡  d S d S |d ur¥| ¡  w w )	NÚschemaÚpreserve_indexZPANDAS_ATTRSÚwb)r3   r5   r6   Úname)rV   rj   Ú
filesystem)rV   rp   )rR   Úpoprb   ÚTableÚfrom_pandasÚattrsÚjsonÚdumpsrl   ÚmetadataÚreplace_schema_metadatarO   r>   ÚioÚBufferedWriterÚhasattrro   r   ÚbytesÚdecodeÚparquetZwrite_to_datasetÚwrite_tableÚclose)rU   rP   r0   rV   rh   r3   rj   rp   rW   Zfrom_pandas_kwargsÚtableÚdf_metadataZexisting_metadataZmerged_metadatarK   rN   r,   r,   r-   rX   ®   sj   

û
ÿþýþûú
þüû€ÿ
ÿzPyArrowImpl.writeFÚuse_nullable_dtypesr7   Údtype_backendúDtypeBackend | lib.NoDefaultc                 K  s  d|d< i }	t ddd�}
|
dkrd|	d< t|||dd�\}}}z\| jjj|f|||d	œ|¤Ž}tƒ � td
dtƒ t|||	d�}W d   ƒ n1 sJw   Y  |
dkrZ|j	ddd�}|j
jrpd|j
jv rp|j
jd }t |¡|_|W |d ur{| ¡  S S |d ur…| ¡  w w )NTZuse_pandas_metadatazmode.data_manager)ÚsilentÚarrayÚsplit_blocksr/   )r3   r5   )rZ   rp   Úfiltersr9   zmake_block is deprecated)r„   Úto_pandas_kwargsF)Úcopys   PANDAS_ATTRS)r   rO   rb   r~   Ú
read_tabler   r   ÚDeprecationWarningr   Ú_as_managerrl   rw   ru   Úloadsrt   r€   )rU   r0   rZ   r‰   rƒ   r„   r3   rp   rW   rŠ   ÚmanagerrK   rN   Úpa_tableÚresultr‚   r,   r,   r-   r[   ð   sZ   üÿüûýýú

ÿ
ÿzPyArrowImpl.read©r    rQ   ©re   NNNN)rP   r   r0   rf   rV   rg   rh   ri   r3   r4   rj   rk   r    rQ   )rƒ   r7   r„   r…   r3   r4   r    r   )rC   r\   r]   rd   rX   r	   Ú
no_defaultr[   r,   r,   r,   r-   r&   ¢   s    
øEør&   c                   @  s@   e Zd Zddd„Z					dddd„Z				dddd„ZdS )r'   r    rQ   c                 C  s   t ddd�}|| _d S )Nr%   z,fastparquet is required for parquet support.r_   )r
   rb   )rU   r%   r,   r,   r-   rd   +  s   ÿ
zFastParquetImpl.__init__re   NrP   r   rV   ú*Literal['snappy', 'gzip', 'brotli'] | Noner3   r4   c           	        sÚ   |   |¡ d|v r|d urtdƒ‚d|v r| d¡}|d ur"d|d< |d ur*tdƒ‚t|ƒ}t|ƒr@tdƒ‰ ‡ ‡fdd„|d	< nˆrFtd
ƒ‚tdd�� | jj	||f|||dœ|¤Ž W d   ƒ d S 1 sfw   Y  d S )NÚpartition_onzYCannot use both partition_on and partition_cols. Use partition_cols for partitioning dataZhiveZfile_schemeú9filesystem is not implemented for the fastparquet engine.r;   c                   s   ˆ j | dfi ˆp	i ¤Ž  ¡ S )Nrn   )Úopen)r0   Ú_©r;   r3   r,   r-   Ú<lambda>V  s    ÿÿz'FastParquetImpl.write.<locals>.<lambda>Z	open_withz?storage_options passed with file object or non-fsspec file pathT)Úrecord)rV   Úwrite_indexr—   )
rR   r)   rq   r@   r   r   r
   r   rb   rX   )	rU   rP   r0   rV   rh   rj   r3   rp   rW   r,   r›   r-   rX   3  sB   
ÿ
ÿÿþûú"ÿzFastParquetImpl.writec                 K  s  i }|  dd¡}|  dtj¡}	d|d< |rtdƒ‚|	tjur"tdƒ‚|d ur*tdƒ‚t|ƒ}d }
t|ƒrHtdƒ}|j|d	fi |pAi ¤Žj	|d
< nt
|tƒr^tj |¡s^t|d	d|d�}
|
j}z| jj|fi |¤Ž}|jd||dœ|¤ŽW |
d ur}|
 ¡  S S |
d ur‡|
 ¡  w w )Nrƒ   Fr„   Zpandas_nullszNThe 'use_nullable_dtypes' argument is not supported for the fastparquet enginezHThe 'dtype_backend' argument is not supported for the fastparquet enginer˜   r;   r/   r2   r<   )rZ   r‰   r,   )rq   r	   r•   r)   r@   r   r   r
   r™   r2   r>   r   rH   r0   rI   r   rJ   rb   ZParquetFileÚ	to_pandasr€   )rU   r0   rZ   r‰   r3   rp   rW   Zparquet_kwargsrƒ   r„   rN   r;   Zparquet_filer,   r,   r-   r[   h  sD   	ÿ
ÿÿ ÿ

ÿ
ÿzFastParquetImpl.readr“   r”   )rP   r   rV   r–   r3   r4   r    rQ   )NNNN)r3   r4   r    r   )rC   r\   r]   rd   rX   r[   r,   r,   r,   r-   r'   *  s    
ø8úr'   )r3   r"   re   rP   r   ú$FilePath | WriteBuffer[bytes] | NonerV   rg   rh   ri   rj   rk   rp   úbytes | Nonec                 K  sp   t |tƒr|g}t|ƒ}	|du rt ¡ n|}
|	j| |
f|||||dœ|¤Ž |du r6t |
tjƒs2J ‚|
 ¡ S dS )a†	  
    Write a DataFrame to the parquet format.

    Parameters
    ----------
    df : DataFrame
    path : str, path object, file-like object, or None, default None
        String, path object (implementing ``os.PathLike[str]``), or file-like
        object implementing a binary ``write()`` function. If None, the result is
        returned as bytes. If a string, it will be used as Root Directory path
        when writing a partitioned dataset. The engine fastparquet does not
        accept file-like objects.
    engine : {{'auto', 'pyarrow', 'fastparquet'}}, default 'auto'
        Parquet library to use. If 'auto', then the option
        ``io.parquet.engine`` is used. The default ``io.parquet.engine``
        behavior is to try 'pyarrow', falling back to 'fastparquet' if
        'pyarrow' is unavailable.

        When using the ``'pyarrow'`` engine and no storage options are provided
        and a filesystem is implemented by both ``pyarrow.fs`` and ``fsspec``
        (e.g. "s3://"), then the ``pyarrow.fs`` filesystem is attempted first.
        Use the filesystem keyword with an instantiated fsspec filesystem
        if you wish to use its implementation.
    compression : {{'snappy', 'gzip', 'brotli', 'lz4', 'zstd', None}},
        default 'snappy'. Name of the compression to use. Use ``None``
        for no compression.
    index : bool, default None
        If ``True``, include the dataframe's index(es) in the file output. If
        ``False``, they will not be written to the file.
        If ``None``, similar to ``True`` the dataframe's index(es)
        will be saved. However, instead of being saved as values,
        the RangeIndex will be stored as a range in the metadata so it
        doesn't require much space and is faster. Other indexes will
        be included as columns in the file output.
    partition_cols : str or list, optional, default None
        Column names by which to partition the dataset.
        Columns are partitioned in the order they are given.
        Must be None if path is not a string.
    {storage_options}

    filesystem : fsspec or pyarrow filesystem, default None
        Filesystem object to use when reading the parquet file. Only implemented
        for ``engine="pyarrow"``.

        .. versionadded:: 2.1.0

    kwargs
        Additional keyword arguments passed to the engine

    Returns
    -------
    bytes if no path argument is provided else None
    N)rV   rh   rj   r3   rp   )r>   r   r.   ry   ÚBytesIOrX   Úgetvalue)rP   r0   r   rV   rh   r3   rj   rp   rW   ÚimplÚpath_or_bufr,   r,   r-   Ú
to_parquet›  s(   
Aþùør¦   úFilePath | ReadBuffer[bytes]rZ   rƒ   úbool | lib.NoDefaultr„   r…   r‰   ú&list[tuple] | list[list[tuple]] | Nonec              	   K  sf   t |ƒ}	|tjurd}
|du r|
d7 }
tj|
ttƒ d� nd}t|ƒ |	j| f||||||dœ|¤ŽS )a¢  
    Load a parquet object from the file path, returning a DataFrame.

    Parameters
    ----------
    path : str, path object or file-like object
        String, path object (implementing ``os.PathLike[str]``), or file-like
        object implementing a binary ``read()`` function.
        The string could be a URL. Valid URL schemes include http, ftp, s3,
        gs, and file. For file URLs, a host is expected. A local file could be:
        ``file://localhost/path/to/table.parquet``.
        A file URL can also be a path to a directory that contains multiple
        partitioned parquet files. Both pyarrow and fastparquet support
        paths to directories as well as file URLs. A directory path could be:
        ``file://localhost/path/to/tables`` or ``s3://bucket/partition_dir``.
    engine : {{'auto', 'pyarrow', 'fastparquet'}}, default 'auto'
        Parquet library to use. If 'auto', then the option
        ``io.parquet.engine`` is used. The default ``io.parquet.engine``
        behavior is to try 'pyarrow', falling back to 'fastparquet' if
        'pyarrow' is unavailable.

        When using the ``'pyarrow'`` engine and no storage options are provided
        and a filesystem is implemented by both ``pyarrow.fs`` and ``fsspec``
        (e.g. "s3://"), then the ``pyarrow.fs`` filesystem is attempted first.
        Use the filesystem keyword with an instantiated fsspec filesystem
        if you wish to use its implementation.
    columns : list, default=None
        If not None, only these columns will be read from the file.
    {storage_options}

        .. versionadded:: 1.3.0

    use_nullable_dtypes : bool, default False
        If True, use dtypes that use ``pd.NA`` as missing value indicator
        for the resulting DataFrame. (only applicable for the ``pyarrow``
        engine)
        As new dtypes are added that support ``pd.NA`` in the future, the
        output with this option will change to use those dtypes.
        Note: this is an experimental option, and behaviour (e.g. additional
        support dtypes) may change without notice.

        .. deprecated:: 2.0

    dtype_backend : {{'numpy_nullable', 'pyarrow'}}, default 'numpy_nullable'
        Back-end data type applied to the resultant :class:`DataFrame`
        (still experimental). Behaviour is as follows:

        * ``"numpy_nullable"``: returns nullable-dtype-backed :class:`DataFrame`
          (default).
        * ``"pyarrow"``: returns pyarrow-backed nullable :class:`ArrowDtype`
          DataFrame.

        .. versionadded:: 2.0

    filesystem : fsspec or pyarrow filesystem, default None
        Filesystem object to use when reading the parquet file. Only implemented
        for ``engine="pyarrow"``.

        .. versionadded:: 2.1.0

    filters : List[Tuple] or List[List[Tuple]], default None
        To filter out data.
        Filter syntax: [[(column, op, val), ...],...]
        where op is [==, =, >, >=, <, <=, !=, in, not in]
        The innermost tuples are transposed into a set of filters applied
        through an `AND` operation.
        The outer list combines these sets of filters through an `OR`
        operation.
        A single list of tuples can also be used, meaning that no `OR`
        operation between set of filters is to be conducted.

        Using this argument will NOT result in row-wise filtering of the final
        partitions unless ``engine="pyarrow"`` is also specified.  For
        other engines, filtering is only performed at the partition level, that is,
        to prevent the loading of some row-groups and/or files.

        .. versionadded:: 2.1.0

    **kwargs
        Any additional kwargs are passed to the engine.

    Returns
    -------
    DataFrame

    See Also
    --------
    DataFrame.to_parquet : Create a parquet object that serializes a DataFrame.

    Examples
    --------
    >>> original_df = pd.DataFrame(
    ...     {{"foo": range(5), "bar": range(5, 10)}}
    ...    )
    >>> original_df
       foo  bar
    0    0    5
    1    1    6
    2    2    7
    3    3    8
    4    4    9
    >>> df_parquet_bytes = original_df.to_parquet()
    >>> from io import BytesIO
    >>> restored_df = pd.read_parquet(BytesIO(df_parquet_bytes))
    >>> restored_df
       foo  bar
    0    0    5
    1    1    6
    2    2    7
    3    3    8
    4    4    9
    >>> restored_df.equals(original_df)
    True
    >>> restored_bar = pd.read_parquet(BytesIO(df_parquet_bytes), columns=["bar"])
    >>> restored_bar
        bar
    0    5
    1    6
    2    7
    3    8
    4    9
    >>> restored_bar.equals(original_df[['bar']])
    True

    The function uses `kwargs` that are passed directly to the engine.
    In the following example, we use the `filters` argument of the pyarrow
    engine to filter the rows of the DataFrame.

    Since `pyarrow` is the default engine, we can omit the `engine` argument.
    Note that the `filters` argument is implemented by the `pyarrow` engine,
    which can benefit from multithreading and also potentially be more
    economical in terms of memory.

    >>> sel = [("foo", ">", 2)]
    >>> restored_part = pd.read_parquet(BytesIO(df_parquet_bytes), filters=sel)
    >>> restored_part
        foo  bar
    0    3    8
    1    4    9
    zYThe argument 'use_nullable_dtypes' is deprecated and will be removed in a future version.TzFUse dtype_backend='numpy_nullable' instead of use_nullable_dtype=True.)Ú
stacklevelF)rZ   r‰   r3   rƒ   r„   rp   )	r.   r	   r•   ÚwarningsÚwarnÚFutureWarningr   r   r[   )r0   r   rZ   r3   rƒ   r„   rp   r‰   rW   r¤   Úmsgr,   r,   r-   Úread_parquetô  s0    
ÿÿÿùør¯   )r   r   r    r!   )Nr/   F)r0   r1   r2   r   r3   r4   r5   r   r6   r7   r    r8   )Nr"   re   NNNN)rP   r   r0   r    r   r   rV   rg   rh   ri   r3   r4   rj   rk   rp   r   r    r¡   )r0   r§   r   r   rZ   rk   r3   r4   rƒ   r¨   r„   r…   rp   r   r‰   r©   r    r   )6Ú__doc__Ú
__future__r   ry   ru   rH   Útypingr   r   r   r«   r   r   Úpandas._config.configr   Úpandas._libsr	   Úpandas.compat._optionalr
   Úpandas.errorsr   Úpandas.util._decoratorsr   Úpandas.util._exceptionsr   Úpandas.util._validatorsr   rc   r   r   Úpandas.core.shared_docsr   Úpandas.io._utilr   Úpandas.io.commonr   r   r   r   r   Úpandas._typingr   r   r   r   r   r.   rO   r!   r&   r'   r¦   r•   r¯   r,   r,   r,   r-   Ú<module>   s`    
	%û? 	qøXø