o
    %j                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddl	Z	ddl
Zddl
mZ ddlmZ ddlmZ e	jdZe	jje	jjgZe	jd	d
d Zdd Zedd ZdS )zd
Tests multithreading behaviour for reading and
parsing files for each parser defined in parsers.py
    )	ExitStack)BytesIO)
ThreadPoolN)	DataFrame)VersionZpyarrow_xfailz0ignore:Passing a BlockManager:DeprecationWarningc                    s   | }|j dkrtd}t|jtdk r|tjjdd d d} fddt|D }t	 /fd	d
|D }
td}||j|}|d }	|D ]}
t|	|
 qOW d    d S 1 scw   Y  d S )NZpyarrowz16.0z+# ValueError: Found non-unique column index)reasond   
   c                 3   s,    | ]}d  dd t D  V  qdS )
c                 S   s&   g | ]}|d d|d d|d qS )d, .0ir   r   i/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/pandas/tests/io/parser/test_multi_thread.py
<listcomp>)   s   & zBtest_multi_thread_string_io_read_csv.<locals>.<genexpr>.<listcomp>N)joinrangeencode)r   _)max_row_ranger   r   	<genexpr>(   s
    
z7test_multi_thread_string_io_read_csv.<locals>.<genexpr>c                    s   g | ]	}  t|qS r   )enter_contextr   )r   b)stackr   r   r   /   s    z8test_multi_thread_string_io_read_csv.<locals>.<listcomp>   r   )ZenginepytestZimportorskipr   __version__ZapplymarkermarkZxfailr   r   r   r   mapread_csvtmassert_frame_equal)all_parsersrequestparserpaZ	num_filesZbytes_to_dffilespoolresultsZfirst_resultresultr   )r   r   r   $test_multi_thread_string_io_read_csv   s*   


"r,   c                    s   fdd} fddt D }td}|||}W d   n1 s)w   Y  |d j}|dd D ]}	||	_q9t|}
|
S )	a  
    Generate a DataFrame via multi-thread.

    Parameters
    ----------
    parser : BaseParser
        The parser object to use for reading the data.
    path : str
        The location of the CSV file to read.
    num_rows : int
        The number of rows to read per task.
    num_tasks : int
        The number of tasks to use for reading this DataFrame.

    Returns
    -------
    df : DataFrame
    c                    sB   | \}}|s j dd|dgdS  j ddt|d |dgdS )aj  
        Create a reader for part of the CSV.

        Parameters
        ----------
        arg : tuple
            A tuple of the following:

            * start : int
                The starting row to start for parsing CSV
            * nrows : int
                The number of rows to read.

        Returns
        -------
        df : DataFrame
        r   date)	index_colheadernrowsparse_datesN   	   )r.   r/   Zskiprowsr0   r1   )r!   int)argstartr0   )r&   pathr   r   readerN   s   
z0_generate_multi_thread_dataframe.<locals>.readerc                    s    g | ]} |    fqS r   r   r   )num_rows	num_tasksr   r   r   p   s    z4_generate_multi_thread_dataframe.<locals>.<listcomp>)Z	processesNr   r2   )r   r   r    columnspdconcat)r&   r7   r9   r:   r8   tasksr)   r*   r/   rfinal_dataframer   )r9   r:   r&   r7   r    _generate_multi_thread_dataframe:   s   "

rA   c                 C   s   d}d}| }d}t tjd|tjd|tjd|tjd|tjd|dg| dg| dg| tjd|d	d
tj|ddd
}t|}|	| t
||||}t|| W d    d S 1 sqw   Y  d S )N   0   z__thread_pool_reader__.csv   foobarbazz20000101 09:00:00s)ZperiodsfreqZint64)Zdtype)
ar   cr   erE   rF   rG   r-   r4   )r   nprandomZdefault_rngr<   Z
date_rangeZaranger"   Zensure_cleanZto_csvrA   r#   )r$   r:   r9   r&   	file_nameZdfr7   r@   r   r   r   )test_multi_thread_path_multipart_read_csv   s0   
"rP   )__doc__
contextlibr   ior   Zmultiprocessing.poolr   numpyrM   r   Zpandasr<   r   Zpandas._testingZ_testingr"   Zpandas.util.versionr   r   ZusefixturesZxfail_pyarrowZ
single_cpuZslowZ
pytestmarkfilterwarningsr,   rA   rP   r   r   r   r   <module>   s&    

F