
    LVf]                    Z   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 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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- ddZ.	 	 	 d 	 	 	 	 	 	 	 	 	 	 	 d!dZ/ G d d      Z0 G d de0      Z1 G d de0      Z2 eed         	 	 	 	 	 	 	 d"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#d       Z3 eed         dddejh                  ejh                  ddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$d       Z5y)%z parquet compat     )annotationsN)TYPE_CHECKINGAnyLiteral)catch_warnings)using_pyarrow_string_dtype)lib)import_optional_dependencyAbstractMethodError)doc)find_stack_level)check_dtype_backend)	DataFrame
get_option)_shared_docs)arrow_string_types_mapper)	IOHandles
get_handleis_fsspec_urlis_urlstringify_path)DtypeBackendFilePath
ReadBufferStorageOptionsWriteBufferBaseImplc                $   | dk(  rt        d      } | dk(  r,t        t        g}d}|D ]  }	  |       c S  t        d|       | dk(  r
t               S | dk(  r
t               S t        d	      # t        $ r}|dt	        |      z   z  }Y d}~dd}~ww xY w)
zreturn our implementationautozio.parquet.engine z
 - NzUnable 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ImportErrorstr
ValueError)engineengine_classes
error_msgsengine_classerrs        Z/var/www/html/software/conda/envs/catlas/lib/python3.12/site-packages/pandas/io/parquet.py
get_enginer/   2   s    /0%7
* 	1L1#~%	1 C l
 	
 }	=	   
E
FF%  1gC00
1s   A++	B4B

Bstorage_optionsc                ,   t        |       }|t        dd      }t        dd      }|#t        ||j                        r|rOt	        d      |!t        ||j
                  j                        rn!t        dt        |      j                         t        |      rk|i|5t        d      }t        d      }	 |j                  j                  |       \  }}|Mt        d      } |j                  j                  |fi |xs i \  }}n|rt!        |      r|d	k7  rt        d
      d}	|sN|sLt        |t"              r<t$        j&                  j)                  |      st+        ||d|      }	d}|	j,                  }||	|fS # t        |j                  f$ r Y w xY w)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"   rbz8storage_options passed with buffer, or non-supported URLFis_textr0   )r   r
   
isinstance
FileSystemNotImplementedErrorspecAbstractFileSystemr(   type__name__r   from_uri	TypeErrorArrowInvalidcore	url_to_fsr   r'   ospathisdirr   handle)
rE   fsr0   modeis_dirpath_or_handlepa_fsr4   pahandless
             r.   _get_path_or_handlerO   T   s    $D)N	~*<I+HXFB0@0@!A)N  Jr6;;3Q3Q$Rb**+-  ^$"+I6B.|<E%*%5%5%>%>t%D"N :/9F!6!6!6"#2#8b"B 
&"8DDL STTG~s+n-
 D%
  7B&&7 r/ s   7E; ;FFc                  0    e Zd Zedd       ZddZdddZy)	r   c                :    t        | t              st        d      y )Nz+to_parquet only supports IO with DataFrames)r8   r   r(   )dfs    r.   validate_dataframezBaseImpl.validate_dataframe   s    "i(JKK )    c                    t        |       Nr   )selfrR   rE   compressionkwargss        r.   writezBaseImpl.write       !$''rT   Nc                    t        |       rV   r   )rW   rE   columnsrY   s       r.   readzBaseImpl.read   r[   rT   )rR   r   returnNone)rR   r   rV   )r_   r   )r>   
__module____qualname__staticmethodrS   rZ   r^    rT   r.   r   r      s     L L((rT   c                  z    e Zd ZddZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdddej                  ddf	 	 	 	 	 	 	 d	dZy)
r$   c                <    t        dd       dd l}dd l}|| _        y )Nr"   z(pyarrow is required for parquet support.extrar   )r
   pyarrow.parquet(pandas.core.arrays.arrow.extension_typesapi)rW   r"   pandass      r.   __init__zPyArrowImpl.__init__   s!    "G	
 	 	8rT   Nc                z   | j                  |       d|j                  dd       i}	|||	d<    | j                  j                  j                  |fi |	}
|j
                  rNdt        j                  |j
                        i}|
j                  j                  }i ||}|
j                  |      }
t        |||d|d u      \  }}}t        |t        j                        rXt        |d      rLt        |j                   t"        t$        f      r,|j                   }t        |t$              r|j'                         }	 |- | j                  j(                  j*                  |
|f|||d| n+ | j                  j(                  j,                  |
|f||d| ||j/                          y y # ||j/                          w w xY w)	Nschemapreserve_indexPANDAS_ATTRSwb)r0   rI   rJ   name)rX   partition_cols
filesystem)rX   ru   )rS   poprk   Tablefrom_pandasattrsjsondumpsro   metadatareplace_schema_metadatarO   r8   ioBufferedWriterhasattrrs   r'   bytesdecodeparquetwrite_to_datasetwrite_tableclose)rW   rR   rE   rX   indexr0   rt   ru   rY   from_pandas_kwargstabledf_metadataexisting_metadatamerged_metadatarK   rN   s                   r.   rZ   zPyArrowImpl.write   s    	#.6

8T8R-S38/0***2D1CD88)4::bhh+?@K % 5 5B!2BkBO11/BE.A+!-/
+ ~r'8'89/>..e=+00N.%0!/!6!6!8	 )1  11" !,#1)  -  ,," !,)	
  " #w" #s   7AF% %F:Fc                   d|d<   i }	|dk(  rddl m}
  |
       }|j                  |	d<   n0|dk(  rt        j                  |	d<   nt               rt               |	d<   t        d      }|d	k(  rd|	d
<   t        |||d      \  }}}	  | j                  j                  j                  |f|||d|} |j                  di |	}|d	k(  r|j                  d	d      }|j                  j                  rKd|j                  j                  v r3|j                  j                  d   }t!        j"                  |      |_        |||j'                          S S # ||j'                          w w xY w)NTuse_pandas_metadatanumpy_nullabler   )_arrow_dtype_mappingtypes_mapperr"   zmode.data_managerarraysplit_blocksr5   )r0   rI   )r]   ru   filtersF)copys   PANDAS_ATTRSrd   )pandas.io._utilr   getpd
ArrowDtyper   r   r   rO   rk   r   
read_table	to_pandas_as_managerro   r|   rz   loadsry   r   )rW   rE   r]   r   use_nullable_dtypesdtype_backendr0   ru   rY   to_pandas_kwargsr   mappingmanagerrK   rN   pa_tableresultr   s                     r.   r^   zPyArrowImpl.read   s~    )-$%,,<*,G/6{{^,i'/1}}^,')/H/J^,01g/3^,.A+	/
+	 2txx''22%	
 H (X'';*:;F'!++G%+@''"hoo&>&>>"*//":":?"KK#'::k#:FL" #w" #s   B7E E#r_   r`   snappyNNNN)rR   r   rE   zFilePath | WriteBuffer[bytes]rX   
str | Noner   bool | Noner0   StorageOptions | Nonert   list[str] | Noner_   r`   )r   boolr   DtypeBackend | lib.NoDefaultr0   r   r_   r   )r>   ra   rb   rm   rZ   r	   
no_defaultr^   rd   rT   r.   r$   r$      s    	 #+!15+/? ?  ,?   	? 
 ?  /?  )?  
? H $)69nn156 
 "6  46  /6  
6 rT   r$   c                  N    e Zd ZddZ	 	 	 	 	 d	 	 	 	 	 	 	 ddZ	 	 	 	 d	 	 	 d	dZy)
r%   c                ,    t        dd      }|| _        y )Nr#   z,fastparquet is required for parquet support.rg   )r
   rk   )rW   r#   s     r.   rm   zFastParquetImpl.__init__'  s     1!O
 rT   Nc                  	 | j                  |       d|v r|t        d      d|v r|j                  d      }|d|d<   |t        d      t	        |      }t        |      rt        d      		fd|d<   nrt        d	      t        d
      5   | j                  j                  ||f|||d| d d d        y # 1 sw Y   y xY w)Npartition_onzYCannot use both partition_on and partition_cols. Use partition_cols for partitioning datahivefile_scheme9filesystem is not implemented for the fastparquet engine.r4   c                P     j                   | dfi xs i j                         S )Nrr   )open)rE   _r4   r0   s     r.   <lambda>z'FastParquetImpl.write.<locals>.<lambda>R  s.    +&++d3.4"3df rT   	open_withz?storage_options passed with file object or non-fsspec file pathT)record)rX   write_indexr   )
rS   r(   rv   r:   r   r   r
   r   rk   rZ   )
rW   rR   rE   rX   r   rt   r0   ru   rY   r4   s
         `  @r.   rZ   zFastParquetImpl.write/  s    	#V#(BK  V##ZZ7N%$*F=!!%K 
 d#/9F#F; Q  4( 	DHHNN (!+ 	 	 	s   #B>>Cc                   i }|j                  dd      }|j                  dt        j                        }	d|d<   |rt        d      |	t        j                  urt        d      |t	        d      t        |      }d }
t        |      r1t        d      } |j                  |d	fi |xs i j                  |d
<   nJt        |t              r:t        j                  j                  |      st        |d	d|      }
|
j                   }	  | j"                  j$                  |fi |} |j&                  d||d||
|
j)                          S S # |
|
j)                          w w xY w)Nr   Fr   pandas_nullszNThe 'use_nullable_dtypes' argument is not supported for the fastparquet enginezHThe 'dtype_backend' argument is not supported for the fastparquet enginer   r4   r5   rH   r6   )r]   r   rd   )rv   r	   r   r(   r:   r   r   r
   r   rH   r8   r'   rD   rE   rF   r   rG   rk   ParquetFiler   r   )rW   rE   r]   r   r0   ru   rY   parquet_kwargsr   r   rN   r4   parquet_files                r.   r^   zFastParquetImpl.readd  sm    *,$jj)>F

?CNNC).~&%  .%  !%K  d#/9F#.6;;tT#Uo>SQS#U#X#XN4 c"277==+> !dE?G >>D	 /488//GGL)<))U'7UfU" #w" #s   1E
 
Er   r   )rR   r   rX   z*Literal['snappy', 'gzip', 'brotli'] | Noner0   r   r_   r`   )NNNN)r0   r   r_   r   )r>   ra   rb   rm   rZ   r^   rd   rT   r.   r%   r%   &  sn     CK1533 @	3 /3 
3p 150 
 /0  
0 rT   r%   )r0   r    c           	         t        |t              r|g}t        |      }	|t        j                         n|}
 |	j
                  | |
f|||||d| |,t        |
t        j                        sJ |
j                         S y)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.

        .. versionchanged:: 1.2.0

    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}

        .. versionadded:: 1.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

    kwargs
        Additional keyword arguments passed to the engine

    Returns
    -------
    bytes if no path argument is provided else None
    N)rX   r   rt   r0   ru   )r8   r'   r/   r~   BytesIOrZ   getvalue)rR   rE   r)   rX   r   r0   rt   ru   rY   implpath_or_bufs              r.   
to_parquetr     s    L .#&()fDAESWKDJJ
	  %'	 	 |+rzz222##%%rT   c           
         t        |      }	|t        j                  ur0d}
|du r|
dz  }
t        j                  |
t
        t                      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)r]   r   r0   r   r   ru   )	r/   r	   r   warningswarnFutureWarningr   r   r^   )rE   r)   r]   r0   r   r   ru   r   rY   r   msgs              r.   read_parquetr     s    r fD#..0# 	 $&XC 	c=5E5GH#&499	'/#	 	 	rT   )r)   r'   r_   r   )Nr5   F)rE   z1FilePath | ReadBuffer[bytes] | WriteBuffer[bytes]rH   r   r0   r   rI   r'   rJ   r   r_   zVtuple[FilePath | ReadBuffer[bytes] | WriteBuffer[bytes], IOHandles[bytes] | None, Any])Nr    r   NNNN)rR   r   rE   z$FilePath | WriteBuffer[bytes] | Noner)   r'   rX   r   r   r   r0   r   rt   r   ru   r   r_   zbytes | None)rE   zFilePath | ReadBuffer[bytes]r)   r'   r]   r   r0   r   r   zbool | lib.NoDefaultr   r   ru   r   r   z&list[tuple] | list[list[tuple]] | Noner_   r   )6__doc__
__future__r   r~   rz   rD   typingr   r   r   r   r   pandas._configr   pandas._libsr	   pandas.compat._optionalr
   pandas.errorsr   pandas.util._decoratorsr   pandas.util._exceptionsr   pandas.util._validatorsr   rl   r   r   r   pandas.core.shared_docsr   r   r   pandas.io.commonr   r   r   r   r   pandas._typingr   r   r   r   r   r/   rO   r   r$   r%   r   r   r   rd   rT   r.   <module>r      s    " 	  	 
  # 5  > - ' 4 7  1 5   GJ .2<'
;<'<' +<' 	<'
 <'<'~
( 
(C ( C Ln h n b \"345 26&-1'+ZZ
.Z Z 	Z
 Z +Z %Z Z Z 6Zz \"345  $-10325..6:q
&qq q +	q
 .q 0q q 4q q 6qrT   