o
    Fh%                     @   sN  d dl Z d dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ d dlmZ G dd dejZdZed d ZG dd	 d	ejZG d
d dejZG dd dejZdd Z dd Z!ddddZ"de de"_#dddddZ$dddddZ%de de%_#d(ddddd Z&ddd!d"d#Z'd$d%d&d'Z(dS ))    N)IpcReadOptionsIpcWriteOptions	ReadStats
WriteStatsMessageMessageReaderRecordBatchReader_ReadPandasMixinMetadataVersion	Alignmentread_messageread_record_batchread_schemaread_tensorwrite_tensorget_record_batch_sizeget_tensor_sizec                   @   s    e Zd ZdZdddddZdS )RecordBatchStreamReadera  
    Reader for the Arrow streaming binary format.

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
        If you want to use memory map use MemoryMappedFile as source.
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC deserialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.
    Noptionsmemory_poolc                C   s   t |}| j|||d d S )Nr    _ensure_default_ipc_read_options_open)selfsourcer   r    r   L/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/pyarrow/ipc.py__init__2      z RecordBatchStreamReader.__init____name__
__module____qualname____doc__r   r   r   r   r   r   "   s    r   aO  Parameters
----------
sink : str, pyarrow.NativeFile, or file-like Python object
    Either a file path, or a writable file object.
schema : pyarrow.Schema
    The Arrow schema for data to be written to the file.
options : pyarrow.ipc.IpcWriteOptions
    Options for IPC serialization.

    If None, default values will be used: the legacy format will not
    be used unless overridden by setting the environment variable
    ARROW_PRE_0_15_IPC_FORMAT=1, and the V5 metadata version will be
    used unless overridden by setting the environment variable
    ARROW_PRE_1_0_METADATA_VERSION=1.
zmetadata : dict | pyarrow.KeyValueMetadata, optional
    Key/value pairs (both must be bytes-like) that will be stored
    in the file footer and are retrievable via
    pyarrow.ipc.open_file(...).metadata.c                   @   s$   e Zd Zde ZddddZdS )RecordBatchStreamWriterz.Writer for the Arrow streaming binary format

Nr   c                C   s   t |}| j|||d d S Nr'   _get_legacy_format_defaultr   )r   sinkschemar   r   r   r   r   X   r   z RecordBatchStreamWriter.__init__)r!   r"   r#   _ipc_writer_class_docr$   r   r   r   r   r   r&   S   s
    r&   c                   @   s"   e Zd ZdZddddddZdS )RecordBatchFileReadera  
    Class for reading Arrow record batch data from the Arrow binary file format

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
        If you want to use memory map use MemoryMappedFile as source.
    footer_offset : int, default None
        If the file is embedded in some larger file, this is the byte offset to
        the very end of the file data
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC serialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.
    Nr   c                C      t |}| j||||d d S )Nfooter_offsetr   r   r   )r   r   r1   r   r   r   r   r   r   p   s   
zRecordBatchFileReader.__init__Nr    r   r   r   r   r.   ]   s
    r.   c                   @   s&   e Zd Zde ZdddddZdS )RecordBatchFileWriterz/Writer to create the Arrow binary file format

Nr   metadatac                C   r/   Nr4   r)   )r   r+   r,   r   r5   r   r   r   r   }   s   zRecordBatchFileWriter.__init__)r!   r"   r#   _ipc_file_writer_class_docr$   r   r   r   r   r   r3   w   s
    r3   c                 C   sh   | rt | tstdt|  | S tj}tttj	
dd}tttj	
ddr.tj}t||dS )Nzexpected IpcWriteOptions, got ARROW_PRE_0_15_IPC_FORMAT0ARROW_PRE_1_0_METADATA_VERSION)use_legacy_formatmetadata_version)
isinstancer   	TypeErrortyper
   V5boolintosenvirongetV4)r   r<   r;   r   r   r   r*      s   
r*   c                 C   s*   | rt | tstdt|  | pt S )Nzexpected IpcReadOptions, got )r=   r   r>   r?   r'   r   r   r   r      s   
r   r'   c                C      t | ||dS r(   )r&   )r+   r,   r   r   r   r   
new_stream   s   rH   z5Create an Arrow columnar IPC stream writer instance

zS

Returns
-------
writer : RecordBatchStreamWriter
    A writer for the given sink
r   c                C   rG   )a  
    Create reader for Arrow streaming format.

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC serialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.

    Returns
    -------
    reader : RecordBatchStreamReader
        A reader for the given source
    r   )r   )r   r   r   r   r   r   open_stream   s   rI   r4   c                C      t | |||dS r6   )r3   )r+   r,   r   r5   r   r   r   new_file   s   rK   z3Create an Arrow columnar IPC file writer instance

zQ

Returns
-------
writer : RecordBatchFileWriter
    A writer for the given sink
c                C   rJ   )a  
    Create reader for Arrow file format.

    Parameters
    ----------
    source : bytes/buffer-like, pyarrow.NativeFile, or file-like Python object
        Either an in-memory buffer, or a readable file object.
    footer_offset : int, default None
        If the file is embedded in some larger file, this is the byte offset to
        the very end of the file data.
    options : pyarrow.ipc.IpcReadOptions
        Options for IPC serialization.
        If None, default values will be used.
    memory_pool : MemoryPool, default None
        If None, default memory pool is used.

    Returns
    -------
    reader : RecordBatchFileReader
        A reader for the given source
    r0   )r.   )r   r1   r   r   r   r   r   	open_file   s   rL   nthreadspreserve_indexc                C   s`   t jj| ||d}t  }t ||j}|| W d   | S 1 s'w   Y  | S )a  
    Serialize a pandas DataFrame into a buffer protocol compatible object.

    Parameters
    ----------
    df : pandas.DataFrame
    nthreads : int, default None
        Number of threads to use for conversion to Arrow, default all CPUs.
    preserve_index : bool, default None
        The default of None will store the index as a column, except for
        RangeIndex which is stored as metadata only. If True, always
        preserve the pandas index data as a column. If False, no index
        information is saved and the result will have a default RangeIndex.

    Returns
    -------
    buf : buffer
        An object compatible with the buffer protocol.
    rM   N)paRecordBatchfrom_pandasBufferOutputStreamr&   r,   write_batchgetvalue)dfrN   rO   batchr+   writerr   r   r   serialize_pandas   s   

rY   Tuse_threadsc                C   sH   t | }t |}| }W d   n1 sw   Y  |j|dS )a  Deserialize a buffer protocol compatible object into a pandas DataFrame.

    Parameters
    ----------
    buf : buffer
        An object compatible with the buffer protocol.
    use_threads : bool, default True
        Whether to parallelize the conversion using multiple threads.

    Returns
    -------
    df : pandas.DataFrame
        The buffer deserialized as pandas DataFrame
    NrZ   )rP   BufferReaderr   read_all	to_pandas)bufr[   buffer_readerreadertabler   r   r   deserialize_pandas  s
   

rc   r2   ))rC   pyarrowrP   pyarrow.libr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   lib_RecordBatchStreamReaderr   r-   r7   _RecordBatchStreamWriterr&   _RecordBatchFileReaderr.   _RecordBatchFileWriterr3   r*   r   rH   r$   rI   rK   rL   rY   rc   r   r   r   r   <module>   s:   L


