
    Owg)                         d Z ddlmZmZ ddlZddlZddlmc 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mZ ddlmZ  G d	 d
      Zd Zy)z\
Tests the TextReader class in parsers.pyx, which
is integral to the C engine in parsers.py
    )BytesIOStringION)
TextReader)ParserWarning)	DataFrame)TextFileReaderread_csv)ensure_dtype_objsc                   ~   e Zd Zej                  d        Zd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zej2                  j5                  ddddifdddifdddifdddifdddifdddifg      d         Zd! Zej2                  j5                  d" ed#            d$        Zd% Zy&)'TestTextReaderc                      |dddd      S )Niodatacsvz	test1.csv )selfdatapaths     ]/var/www/horilla/myenv/lib/python3.12/site-packages/pandas/tests/io/parser/test_textreader.pycsv_pathzTestTextReader.csv_path   s    fe[99    c                 |    t        |d      5 }t        |      }|j                          d d d        y # 1 sw Y   y xY w)Nrbopenr   readr   r   freaders       r   test_file_handlezTestTextReader.test_file_handle    s5    (D! 	Q]FKKM	 	 	s   2;c                     t        |d      5 }t        |d       }|j                          d d d        y # 1 sw Y   y xY wNr   headerr   r   s       r   test_file_handle_mmapz$TestTextReader.test_file_handle_mmap%   s8    (D! 	Q$/FKKM	 	 	s   4=c                     t        |d      5 }|j                         }d d d        t              }t        |d       }|j                          y # 1 sw Y   2xY wr!   )r   r   r   r   )r   r   r   textsrcr   s         r   test_StringIOzTestTextReader.test_StringIO+   sL    (D! 	Q668D	dmC-		 	s   AAc           	          d}t        t        |      d       }|j                         }t        t	        t        t        |d                     dk(  sJ y )Nz	a
b
a
b
ar"   r      )r   r   r   lensetmapidr   r   r   results       r   test_string_factorizez$TestTextReader.test_string_factorize2   sE    HTN483s2vay)*+q000r   c                 N   d}t        t        |      dd       }|j                         }t        j                  |d   t        j                  g dt
        j                               t        j                  |d   t        j                  g dt
        j                               y )	Nza,   b
a,   b
a,   b
a,   bT)skipinitialspacer#   r   )ar4   r4   r4   dtype   )br8   r8   r8   r   r   r   tmassert_numpy_array_equalnparrayobject_r/   s       r   test_skipinitialspacez$TestTextReader.test_skipinitialspace9   st    /HTNT$O
##1Irxx 4BJJG	
 	##1Irxx 4BJJG	
r   c                     d}t        t        |      d       }|j                         }|d   j                  t        j
                  k(  sJ y )NzTrue
False
True
Truer"   r   )r   r   r   r6   r<   bool_r/   s       r   test_parse_booleansz"TestTextReader.test_parse_booleansF   s<    (HTN48ay"((***r   c                 N   d}t        t        |      dd       }|j                         }t        j                  |d   t        j                  g dt
        j                               t        j                  |d   t        j                  g dt
        j                               y )	Nza  b
a		 "b"
"a"	 	 bT)delim_whitespacer#   r   )r4   r4   r4   r5   r7   )r8   r8   r8   r9   r/   s       r   test_delimit_whitespacez&TestTextReader.test_delimit_whitespaceN   sr    ,HTNT$O
##1IrxxrzzB	
 	##1IrxxrzzB	
r   c                     d}t        t        |      d       }|j                         }t        j                  g dt        j
                        }t        j                  |d   |       y )Nza
"hello
there"
thisr"   )r4   zhello
therethisr5   r   )r   r   r   r<   r=   r>   r:   r;   r   r   r   r0   expecteds        r   test_embedded_newlinez$TestTextReader.test_embedded_newline[   sK    (HTN48889L
##F1Ix8r   c                     d}t        t        |      ddd       }|j                         }t        j                  ddg      }t        j                  |d   |       y )Nz12345,67
345,678:,)	delimiterdecimalr#   g)\@g+ٚu@r   )r   r   r   r<   r=   r:   assert_almost_equalrH   s        r   test_euro_decimalz TestTextReader.test_euro_decimald   sM    "HTNc3tT88Xw/0
vay(3r   c                     d}t        t        |      ddd       }|j                         }t        j                  ddgt        j
                        }t        j                  |d   |       y )	Nz123,456
12,500rL   rM   rN   	thousandsr#   @ 0  r5   r   )r   r   r   r<   r=   int64r:   rP   rH   s        r   test_integer_thousandsz%TestTextReader.test_integer_thousandsm   sS     HTNcSQUV88VUO288<
vay(3r   c                     d}t        t        |      ddd       }|j                         }t        ddg      }t	        j
                  ||       y )Nz123.456
12.500rL   .rS   rU   rV   )r   r   r   r   r:   assert_frame_equalrH   s        r   test_integer_thousands_altz)TestTextReader.test_integer_thousands_altv   sI     TNcS
 fe_-
fh/r   c                    d}t        t        |      dd       }d}t        j                  t        j
                  |      5  |j                          d d d        t        t        |      dd d      }|j                         }t        j                  g dt        	      t        j                  g d
t        	      t        j                  g dt        	      d}t        ||       t        j                  t        d      5  t        t        |      dd d      }|j                          d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nz'a:b:c
d:e:f
g:h:i
j:k:l:m
l:m:n
o:p:q:rrL   rN   r#   zCError tokenizing data\. C error: Expected 3 fields in line 4, saw 4)matchr*   )rN   r#   on_bad_lines)r4   dglr5   )r8   ehm)cr   inr   r7   r*   zSkipping liner7   )r   r   pytestraisesparserParserErrorr   r<   r=   objectassert_array_dicts_equalr:   assert_produces_warningr   )r   r   r   msgr0   rI   s         r   test_skip_bad_linesz"TestTextReader.test_skip_bad_lines   s   =HTNc$GT]]6--S9 	KKM	 TNc$Q
 xx,F;xx,F;xx,F;

 	!2''_M 	#dF KKM		 		 		 	s   D2 )D>2D;>Ec                    d}t        t        |      dd      }|j                  }g dg}||k(  sJ |j                         }t	        j
                  ddgt        j                        t	        j
                  dd	gt        j                        t	        j
                  d
dgt        j                        d}t        ||       y )Nz%skip this
skip this
a,b,c
1,2,3
4,5,6rM   r*   r^   r4   r8   rg   r7      r5            rj   )r   r   r#   r   r<   r=   rW   rp   )r   r   r   r#   rI   recss         r   test_header_not_enough_linesz+TestTextReader.test_header_not_enough_lines   s    :HTNc!D#$!!!{{}xxAbhh/xxAbhh/xxAbhh/

 	!x0r   c                     d}t        t        |      dd d      }|j                         }dt        j                  dgdz  t
              i}t        ||       y )	Nz,\"hello world"
\"hello world"
\"hello world"rM   \)rN   r#   
escapecharr   z"hello world"rx   r5   )r   r   r   r<   r=   ro   rp   rH   s        r   test_escapecharzTestTextReader.test_escapechar   sN    BHTNc$SWXrxx 1A 5VDE 2r   c                      y Nr   r   s    r   test_eof_has_eolzTestTextReader.test_eof_has_eol   s    r   c                      y r   r   r   s    r   test_na_substitutionz#TestTextReader.test_na_substitution   s    r   c                    dfd} |d      }|j                         }|d   j                  dk(  sJ t        j                  g dd      }|d   |k(  j	                         sJ |d   j                  d	k(  sJ  |d
      }|j                         }|d   j                  d
k(  sJ t        j                  g dd
      }|d   |k(  j	                         sJ |d   j                  d
k(  sJ y )Nza,1
aa,2
aaa,3
aaaa,4
aaaaa,5c                  ^    d| v rt        | d         | d<   t        t              fdd d| S )Nr6   rM   r^   r
   r   r   kwdsr   s    r   _make_readerz<TestTextReader.test_numpy_string_dtype.<locals>._make_reader   s7    $ 1$w- @WhtnQDQDQQr   zS5,i4r5   r   S5)r4   aaaaaaaaaaaaaar7   i4S4)r4   r   r   r   r   )r   r6   r<   r=   all)r   r   r   r0   	ex_valuesr   s        @r   test_numpy_string_dtypez&TestTextReader.test_numpy_string_dtype   s    	R
 G,ay$&&&HH@M	q	Y&++---ay$&&&D)ay$&&&HH?tL	q	Y&++---ay$&&&r   c                    dfd} |ddd      }|j                         }|d   j                  dk(  sJ |d   j                  dk(  sJ  |t        j                  t        d      }|j                         }|d   j                  dk(  sJ |d   j                  d	k(  sJ  |t        j                  d      t        j                  d	      d      }|j                         }|d   j                  dk(  sJ |d   j                  d	k(  sJ y )
Nzone,two
1,a
2,b
3,c
4,dc                  \    d| v rt        | d         | d<   t        t              fddi| S )Nr6   rN   rM   r   r   s    r   r   z4TestTextReader.test_pass_dtype.<locals>._make_reader   s5    $ 1$w- @WhtnDDtDDr   u1S1)oner7   r5   r   r7   O)r   r6   r<   uint8ro   )r   r   r   r0   r   s       @r   test_pass_dtypezTestTextReader.test_pass_dtype   s    	E
 DT$:;ay$&&&ay$&&&BHH$@Aay$&&&ay#%%%BHHTNrxx}$MNay$&&&ay#%%%r   c                     dfd} |d      }|j                         } |       j                         }t        |      dk(  sJ |d   |d   k(  j                         sJ |d   |d   k(  j                         sJ y )Nz a,b,c
1,2,3
4,5,6
7,8,9
10,11,12c                  2    t        t              fddi| S )NrN   rM   )r   r   r   s    r   r   z1TestTextReader.test_usecols.<locals>._make_reader   s    htnDDtDDr   )r7   r*   )usecolsr*   r7   )r   r+   r   )r   r   r   r0   expr   s        @r   test_usecolszTestTextReader.test_usecols   s    	E f-n!!#6{aq	SV#((***q	SV#((***r   ztext, kwargsz a,b,c1,2,34,5,67,8,910,11,12rN   rM   z*a  b  c1  2  34  5  67  8  910  11  12rD   Tz 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 6c                     |j                  dd      }t        t        |      fi |j                         }t        t        |      fi |j                         }t	        ||       y )Nz
)replacer   r   r   rp   )r   r&   kwargs	nice_textr0   rI   s         r   test_cr_delimitedz TestTextReader.test_cr_delimited  sW    , LLv.	HTN5f5::<hy1<V<AAC 2r   c                 *   d}t        t        |      d      j                         }t        j                  ddgt        j
                        t        j                  ddgt              t        j                  d	dgt              d
}t        ||       y )Nza,b,c
1,2,3
4,,rM   )rN   r7   rv   r5   2 3rj   )r   r   r   r<   r=   rW   ro   rp   )r   r   r0   rI   s       r   test_empty_field_eofz#TestTextReader.test_empty_field_eof!  so    "HTNc:??A xxAbhh/xxb	0xxb	0

 	!2r   repeat
   c           	         t        dgt        j                  ggdgddg      }t        g dg dgt        d      ddg      }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      }t	        t        d      ddgd      }t        j                  ||       t	        t        d      t        d      d      }t        j                  ||       t	        t        d      t        d      d      }t        j                  ||       y )Nr8   r4   rg   )columnsindex)r7   r7   r7   r   abcdr7   )r7   r*   rx   rv   ry   )   	   r            )r   rw         za,b
c
r   )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<   nanlistr	   r   r:   r[   )r   r   r4   r8   rg   dfs         r   #test_empty_field_eof_mem_access_bugz2TestTextReader.test_empty_field_eof_mem_access_bug-  s    urvvh'#sCjI|\2DLQRTUPVWBFFBFFBFF+R(	 L	
 hz*QseCP
b!$12$v,s
 	b!$<=v,

 	b!$r   c                     t        t               dd g d      5 }t        |t              sJ 	 d d d        y # 1 sw Y   y xY w)N   ru   )	chunksizer#   r   )r	   r   
isinstancer   )r   r   s     r   test_empty_csv_inputz#TestTextReader.test_empty_csv_inputL  s=    J"T
 	2b.111	2 	2 	2s	   7A N) __name__
__module____qualname__rk   fixturer   r   r$   r(   r1   r?   rB   rE   rJ   rQ   rX   r\   rs   r{   r   r   r   r   r   r   markparametrizer   r   ranger   r   r   r   r   r   r      s3   ^^: :
1
+
944	041 3':&8+& [[3k35GH@#T* 4k35GH7 c" (*<d)CD #5t"<=!	
*3+*3
3 [[XuRy1% 2%<2r   r   c                     | j                         D ]D  \  }}t        j                  t        j                  |      t        j                  ||                F y r   )itemsr:   r;   r<   asarray)leftrightkvs       r   rp   rp   T  sB    

 I1
##BJJqM2::eAh3GHIr   )__doc__r   r   r   numpyr<   rk   pandas._libs.parsers_libsparsersrm   r   pandas.errorsr   pandasr   pandas._testing_testingr:   pandas.io.parsersr   r	   "pandas.io.parsers.c_parser_wrapperr
   r   rp   r   r   r   <module>r      sG   
   % % + '   Av2 v2r	Ir   