a
    GGb<*                     @   s   d Z ddlmZmZ ddlZddlZddlZddlm	  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 G dd	 d	Zd
d ZdS )z\
Tests the TextReader class in parsers.pyx, which
is integral to the C engine in parsers.py
    )BytesIOStringION)
TextReader)	DataFrame)TextFileReaderread_csv)ensure_dtype_objsc                   @   s   e Zd Zejdddd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1S )2TestTextReaderT)Zautousec                 C   sR   |ddd| _ |ddd}tj|d| _tj| j d| _tj| j d| _d S )NioparserdataZcsvz	test1.csvz	test2.csvztest.xls)dirpathospathjoincsv1Zcsv2Zxls1)selfdatapathZcsv1_dirpath r   Elib/python3.9/site-packages/pandas/tests/io/parser/test_textreader.pysetup_method   s
    zTestTextReader.setup_methodc                 C   s@   t | jd }t|}|  W d    n1 s20    Y  d S )Nrbopenr   r   readr   freaderr   r   r   test_file_handle$   s    zTestTextReader.test_file_handlec                 C   sD   t | jd$}t|d d}|  W d    n1 s60    Y  d S Nr   headerr   r   r   r   r   test_file_handle_mmap)   s    z$TestTextReader.test_file_handle_mmapc                 C   sT   t | jd}| }W d    n1 s*0    Y  t|}t|d d}|  d S r   )r   r   r   r   r   )r   r   textsrcr   r   r   r   test_StringIO/   s
    &zTestTextReader.test_StringIOc                 C   s>   d}t t|d d}| }tttt|d dks:J d S )Nz	a
b
a
b
ar    r      )r   r   r   lensetmapidr   r   r   resultr   r   r   test_string_factorize6   s    z$TestTextReader.test_string_factorizec                 C   sb   d}t t|dd d}| }t|d tjg dtjd t|d tjg dtjd d S )	Nza,   b
a,   b
a,   b
a,   bT)Zskipinitialspacer!   r   )ar.   r.   r.   dtype   )br2   r2   r2   r   r   r   tmassert_numpy_array_equalnparrayobject_r+   r   r   r   test_skipinitialspace=   s    z$TestTextReader.test_skipinitialspacec                 C   s4   d}t t|d d}| }|d jtjks0J d S )NzTrue
False
True
Truer    r   )r   r   r   r0   r6   Zbool_r+   r   r   r   test_parse_booleansJ   s    z"TestTextReader.test_parse_booleansc                 C   sb   d}t t|dd d}| }t|d tjg dtjd t|d tjg dtjd d S )	Nza  b
a		 "b"
"a"	 	 bT)delim_whitespacer!   r   )r.   r.   r.   r/   r1   )r2   r2   r2   r3   r+   r   r   r   test_delimit_whitespaceR   s    z&TestTextReader.test_delimit_whitespacec                 C   sD   d}t t|d d}| }tjg dtjd}t|d | d S )Nza
"hello
there"
thisr    )r.   zhello
therethisr/   r   )r   r   r   r6   r7   r8   r4   r5   r   r   r   r,   expectedr   r   r   test_embedded_newline_   s
    z$TestTextReader.test_embedded_newlinec                 C   sB   d}t t|ddd d}| }tddg}t|d | d S )Nz12345,67
345,678:,)	delimiterZdecimalr!   g)\@g+ٚu@r   )r   r   r   r6   r7   r4   assert_almost_equalr>   r   r   r   test_euro_decimalh   s
    z TestTextReader.test_euro_decimalc                 C   sH   d}t t|ddd d}| }tjddgtjd}t|d | d S )	Nz123,456
12,500rA   rB   rC   Z	thousandsr!   @ 0  r/   r   )r   r   r   r6   r7   int64r4   rD   r>   r   r   r   test_integer_thousandsq   s
    z%TestTextReader.test_integer_thousandsc                 C   s<   d}t t|ddd d}| }tddg}t|| d S )Nz123.456
12.500rA   .rF   rG   rH   )r   r   r   r   r4   assert_frame_equalr>   r   r   r   test_integer_thousands_altz   s    z)TestTextReader.test_integer_thousands_altc                 C   s   d}t t|dd d}d}tjtj|d |  W d    n1 sH0    Y  t t|dd dd}| }tjg dt	d	tjg d
t	d	tjg dt	d	d}t
|| t t|dd dd}|  | }d|jv sJ d|jv sJ d S )Nz'a:b:c
d:e:f
g:h:i
j:k:l:m
l:m:n
o:p:q:rrA   rC   r!   zCError tokenizing data\. C error: Expected 3 fields in line 4, saw 4)matchr&   )rC   r!   Zon_bad_lines)r.   dglr/   )r2   ehm)cr   inr   r1   r&   r1   zSkipping line 4zSkipping line 6)r   r   pytestZraisesr   ZParserErrorr   r6   r7   objectassert_array_dicts_equalZ
readouterrerr)r   Zcapsysr   r   msgr,   r?   Zcapturedr   r   r   test_skip_bad_lines   s*    &
z"TestTextReader.test_skip_bad_linesc                 C   s   d}t t|ddd}|j}g dg}||ks2J | }tjddgtjdtjdd	gtjdtjd
dgtjdd}t|| d S )Nz%skip this
skip this
a,b,c
1,2,3
4,5,6rB   r&   rN   r.   r2   rV   r1      r/            rY   )r   r   r!   r   r6   r7   rI   r\   )r   r   r   r!   r?   Zrecsr   r   r   test_header_not_enough_lines   s    
z+TestTextReader.test_header_not_enough_linesc                 C   sF   d}t t|dd dd}| }dtjdgd tdi}t|| d S )	Nz,\"hello world"
\"hello world"
\"hello world"rB   \)rC   r!   Z
escapecharr   z"hello world"rc   r/   )r   r   r   r6   r7   r[   r\   r>   r   r   r   test_escapechar   s
    zTestTextReader.test_escapecharc                 C   s   d S Nr   r   r   r   r   test_eof_has_eol   s    zTestTextReader.test_eof_has_eolc                 C   s   d S rh   r   ri   r   r   r   test_na_substitution   s    z#TestTextReader.test_na_substitutionc                    s   d  fdd}|dd}|  }|d jdks4J tjg ddd}|d |k sZJ |d	 jd
kslJ |dd}|  }|d jdksJ tjg ddd}|d |k sJ |d	 jdksJ d S )Nza,1
aa,2
aaa,3
aaaa,4
aaaaa,5c                     s2   d| v rt | d | d< tt fdd d| S )Nr0   rB   rN   r   r   r   kwdsr   r   r   _make_reader   s    z<TestTextReader.test_numpy_string_dtype.<locals>._make_readerzS5,i4r/   r   ZS5)r.   aaaaaaaaaZaaaaar1   Zi4ZS4)r.   rq   rr   rs   rs   )r   r0   r6   r7   all)r   rp   r   r,   Z	ex_valuesr   ro   r   test_numpy_string_dtype   s    

z&TestTextReader.test_numpy_string_dtypec                    s   d  fdd}|dddd}|  }|d jdks:J |d	 jdksLJ |tjtdd}|  }|d jdksxJ |d	 jd
ksJ |tdtd
dd}|  }|d jdksJ |d	 jd
ksJ d S )Nzone,two
1,a
2,b
3,c
4,dc                     s0   d| v rt | d | d< tt fddi| S )Nr0   rC   rB   rl   rm   ro   r   r   rp      s    z4TestTextReader.test_pass_dtype.<locals>._make_readerZu1ZS1)Zoner1   r/   r   r1   O)r   r0   r6   Zuint8r[   )r   rp   r   r,   r   ro   r   test_pass_dtype   s    zTestTextReader.test_pass_dtypec                    sp   d  fdd}|dd}|  }|   }t|dks<J |d |d k sTJ |d |d k slJ d S )Nz a,b,c
1,2,3
4,5,6
7,8,9
10,11,12c                     s   t t fddi| S )NrC   rB   )r   r   rm   ro   r   r   rp     s    z1TestTextReader.test_usecols.<locals>._make_reader)r1   r&   )Zusecolsr&   r1   )r   r'   rt   )r   rp   r   r,   Zexpr   ro   r   test_usecols   s    

zTestTextReader.test_usecolsc                 C   sl   dd }d}||dd d}||dd d	}||dd d
}||dd d}||dd d}||dd d S )Nc                 [   sJ   |  dd}tt| fi | }tt|fi | }t|| d S )Nz
)replacer   r   r   r\   )r#   kwargsZ	nice_textr,   r?   r   r   r   _test  s    z/TestTextReader.test_cr_delimited.<locals>._testz a,b,c1,2,34,5,67,8,910,11,12rB   rC   z*a  b  c1  2  34  5  67  8  910  11  12T)r;   z a,b,c1,2,34,5,6,88,910,11,12zdA,B,C,D,E,F,G,H,I,J,K,L,M,N,OAAAAA,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0zA  B  C  2  34  5  6zA B C2 34 5 6r   )r   r|   r   Zsampler   r   r   test_cr_delimited  s    z TestTextReader.test_cr_delimitedc           	      C   sR  d}t t|dd }tjddgtjdtjddgtdtjd	dgtdd
}t|| tdgtj	ggdgddgd}tg dg dgt
dddgd}tg ddtj	tj	tj	gg dddtj	tj	ggt
dg dd}tdD ]j}ttdddgdd}t|| ttdt
ddd}t|| ttdt
ddd}t|| qd S )Nza,b,c
1,2,3
4,,rB   r}   r1   ra   r/   2 3rY   r2   r.   rV   )columnsindex)r1   r1   r1   r   Zabcd)r1   r&   rc   ra   rd   )   	   
            )r   rb         d   za,b
c
r   )Zskiprowsnamesenginez1,1,1,1,0
1,1,1,1,0


)r   r   z"0,1,2,3,4
5,6
7,8,9,10,11
12,13,14)r   r   r   r6   r7   rI   r[   r\   r   nanlistranger   r4   rL   )	r   r   r,   r?   r.   r2   rV   _dfr   r   r   test_empty_field_eof*  s>    
"z#TestTextReader.test_empty_field_eofc                 C   sH   t t dd g dd}t|ts&J W d    n1 s:0    Y  d S )N   r`   )Z	chunksizer!   r   )r   r   
isinstancer   )r   r   r   r   r   test_empty_csv_inputT  s
    z#TestTextReader.test_empty_csv_inputN)__name__
__module____qualname__rZ   Zfixturer   r   r"   r%   r-   r9   r:   r<   r@   rE   rJ   rM   r_   re   rg   rj   rk   ru   rw   rx   r~   r   r   r   r   r   r   r	      s0   

			*r	   c                 C   s2   |   D ]$\}}tt|t||  qd S rh   )itemsr4   r5   r6   Zasarray)leftrightkvr   r   r   r\   \  s    r\   )__doc__r
   r   r   r   Znumpyr6   rZ   Zpandas._libs.parsersZ_libsZparsersr   r   Zpandasr   Zpandas._testingZ_testingr4   Zpandas.io.parsersr   r   Z"pandas.io.parsers.c_parser_wrapperr   r	   r\   r   r   r   r   <module>   s     C