a
    d                      @  s   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 d d	lmZmZ d d
lmZ erd dlmZ G dd deZdS )    )annotations)TYPE_CHECKING)using_pyarrow_string_dtype)lib)import_optional_dependency)
is_integerN)	DataFrame)_arrow_dtype_mappingarrow_string_types_mapper)
ParserBase)
ReadBufferc                      s\   e Zd ZdZddd fddZdd Zdd	d
dZdddddZdd	ddZ  Z	S )ArrowParserWrapperz7
    Wrapper for the pyarrow engine for read_csv()
    zReadBuffer[bytes]None)srcreturnc                   s$   t  | || _|| _|   d S )N)super__init__kwdsr   _parse_kwds)selfr   r   	__class__ o/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pandas/io/parsers/arrow_parser_wrapper.pyr      s    zArrowParserWrapper.__init__c                 C  sN   | j d}|du rdn|| _| j d }t|tr:tdt| j d | _dS )z?
        Validates keywords before passing to pyarrow.
        encodingNzutf-8	na_valuesz?The pyarrow engine doesn't support passing a dict for na_values)r   getr   
isinstancedict
ValueErrorlistr   )r   r   r   r   r   r   r   %   s    

zArrowParserWrapper._parse_kwds)r   c                 C  s   dddddd}|  D ]4\}}|| jv r| j|dur| j|| j|< q| j}t|trf|g}nd}|| jd< d	d
 | j  D | _dd
 | j  D | _d| jd v | jd< | j	du | j	dur| j	n| jd | j
d| _dS )z:
        Rename some arguments to pass to pyarrow
        include_columnsnull_valuesescape_charignore_empty_linesdecimal_point)Zusecolsr   
escapecharZskip_blank_linesdecimalNtimestamp_parsersc                 S  s&   i | ]\}}|d ur|dv r||qS )N)	delimiter
quote_charr#   r$   r   .0Zoption_nameZoption_valuer   r   r   
<dictcomp>Q   s   z;ArrowParserWrapper._get_pyarrow_options.<locals>.<dictcomp>c                 S  s&   i | ]\}}|d ur|dv r||qS )N)r!   r"   Ztrue_valuesZfalse_valuesr%   r(   r   r+   r   r   r   r-   X   s    Zstrings_can_be_nullZskiprows)Zautogenerate_column_namesZ	skip_rowsr   )itemsr   r   popdate_formatr   strparse_optionsconvert_optionsheaderr   read_options)r   mappingZpandas_nameZpyarrow_namer1   r   r   r   _get_pyarrow_options3   s6    

z'ArrowParserWrapper._get_pyarrow_optionsr   )framer   c              
     s  t  j}d}| jdu rn| jdu r6| jdu r6t|| _t | j|krftt|t | j | j | _d}| j _|  j \} | jdur| j }t	| jD ]\}}t
|r j| ||< n| jvrtd| d| jdur| j|dur|| j|fn j| | j j| f\}}	|	dur | |	 |< | j|= q j|ddd | jdu r|sdgt  jj  j_| jdurt| jtr fdd| j D | _z | j W n. ty }
 zt|
W Y d}
~
n
d}
~
0 0  S )	z
        Processes data read in based on kwargs.

        Parameters
        ----------
        frame: DataFrame
            The DataFrame to process.

        Returns
        -------
        DataFrame
            The processed DataFrame.
        TNFzIndex z invalid)ZdropZinplacec                   s    i | ]\}}| j v r||qS r   )columns)r,   kvr9   r   r   r-          z>ArrowParserWrapper._finalize_pandas_output.<locals>.<dictcomp>)lenr:   r5   namesranger    Z_do_date_conversionsZ	index_colcopy	enumerater   r   Zdtyper   ZastypeZ	set_indexindexr   r   r/   	TypeError)r   r9   Znum_colsZmulti_index_named_Zindex_to_setiitemkeyZ	new_dtypeer   r=   r   _finalize_pandas_outputo   sJ    








z*ArrowParserWrapper._finalize_pandas_outputc                 C  s.  t d}t d}|   |j| j|jf i | j|jf i | j|jf i | j	d}| j
d }|tju r|j}| }t|jjD ],\}}|j|r|||||}q||}|dkr|jtjd}	nP|dkrt }
t |
| < |j|
jd}	n t r|jt d}	n| }	| |	S )z
        Reads the contents of a CSV file into a DataFrame and
        processes it according to the kwargs passed in the
        constructor.

        Returns
        -------
        DataFrame
            The DataFrame created from the CSV file.
        Zpyarrowzpyarrow.csv)r6   r3   r4   dtype_backend)Ztypes_mapperZnumpy_nullable) r   r8   Zread_csvr   ZReadOptionsr6   ZParseOptionsr3   ZConvertOptionsr4   r   r   Z
no_defaultZschemafloat64rC   typesZis_nullsetfieldZ	with_typecastZ	to_pandaspdZ
ArrowDtyper	   Z
Int64Dtypenullr   r   r
   rK   )r   paZpyarrow_csvtablerL   Z
new_schemanew_typerG   Z
arrow_typer9   Zdtype_mappingr   r   r   read   s:    



zArrowParserWrapper.read)
__name__
__module____qualname____doc__r   r   r8   rK   rW   __classcell__r   r   r   r   r      s   <Cr   )
__future__r   typingr   Zpandas._configr   Zpandas._libsr   Zpandas.compat._optionalr   Zpandas.core.dtypes.inferencer   ZpandasrR   r   Zpandas.io._utilr	   r
   Zpandas.io.parsers.base_parserr   Zpandas._typingr   r   r   r   r   r   <module>   s   