o
    dW                     @   s  d Z ddlZddlZddlmZ ddlmZmZmZ ddl	Z	ddl
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  mZ ddlZddlmZ ddlm  mZ G dd dZeeegZ zdd	l!m"Z# e $e# W n	 e%yx   Y nw e
j&'e
j&(e)Z*G d
d dZ+ej,dd Z-G dd dZ.dd Z/ej01dddgej01dddgdd Z2dd Z3ej01deddfeddfgd d! Z4ej01d"g d#ej01dddgd$d% Z5d&d' Z6d(d) Z7d*d+ Z8d,d- Z9ej01d.ej:ej;ej<ej=ej>ej?ej@ejAejBg	d/d0 ZCdS )1z0
Tests for the pandas.io.common functionalities
    N)partial)BytesIOStringIOUnsupportedOperation)Path)is_platform_windowsc                   @   s"   e Zd ZdZdddZdd ZdS )	CustomFSPathz%For testing fspath on unknown objectsreturnNc                 C   s
   || _ d S Npath)selfr    r   `/var/www/html/visualizacion-main/env/lib/python3.10/site-packages/pandas/tests/io/test_common.py__init__    s   
zCustomFSPath.__init__c                 C   s   | j S r
   r   r   r   r   r   
__fspath__#   s   zCustomFSPath.__fspath__)r	   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    
r   )localc                   @   s"  e Zd ZdZdd Zdd Zdd Zedd	d
 Z	dd Z
dd Zejdedd Zejdeeegdd Zdd Zdd Zeddd Zdd Zejdejdedfejded fejd!ed"fejded#fej d$ed%fej!ded&fej"ded'fej#ded(fej$ded)fg	d*d+ Z%ejd,ej&j'dedfej&j(ded-fej&j)d!ed"fej&j*ded#fej&j+ded.fej&j,ded&fej&j-ded(fej&j.ded)fgd/d0 Z/ejdejdedfej0dedfejded fejd!ed"fejded#fej d$ed%fej!ded&fej"ded'fej#ded(fej$ded)fg
d1d2 Z1ejd3ejdd4fej0dd4fejdd5fejd!d6fejdd7fej d$d8fej!dd9fej"dd:fej#dd;fej$dd<fg
d=d> Z2ejd?d@i dfdAdBdCidCfdDi dfdEi dfdFi dfdGi dfdHi dfdIdJe3dKidfgdLdM Z4dNdO Z5dPS )QTestCommonIOCapabilitiesziindex,A,B,C,D
foo,2,3,4,5
bar,7,8,9,10
baz,12,13,14,15
qux,12,13,14,15
foo2,12,13,14,15
bar2,12,13,14,15
c                 C   sB   d}t |}||ksJ tj|sJ tj||ksJ d S )Nz
~/sometest)icom_expand_userosr   isabs
expanduserr   filenameexpanded_namer   r   r   test_expand_user?   s
   
z)TestCommonIOCapabilities.test_expand_userc                 C   s2   d}t |}||ksJ tj||ksJ d S )Nz/somefolder/sometest)r   r   r   r   r   r   r   r   r   test_expand_user_normal_pathG   s   
z5TestCommonIOCapabilities.test_expand_user_normal_pathc                 C   sB   t td}|dksJ t td}|tjddksJ d S )N.zfoo//barfoobar)r   stringify_pathr   r   r   join)r   rel_pathredundant_pathr   r   r   test_stringify_path_pathlibN   s   z4TestCommonIOCapabilities.test_stringify_path_pathlibzpy.pathc                 C   s8   t jdd}t j|}t|}t||ksJ d S )Nr$   r%   )r   r   r'   abspath	LocalPathr   r&   )r   r   abs_pathlpathr   r   r   test_stringify_path_localpathT   s   z6TestCommonIOCapabilities.test_stringify_path_localpathc                 C   s"   t d}t|}|dksJ d S )Nfoo/bar.csv)r   r   r&   )r   presultr   r   r   test_stringify_path_fspath[   s   
z3TestCommonIOCapabilities.test_stringify_path_fspathc              	   C   s   t d}t 4}|jd| dd}|t|ksJ W d    n1 s(w   Y  W d    d S W d    d S 1 s@w   Y  d S )Nfsspeczfile://wbmode)pytestimportorskiptmensure_cleanopenr   r&   )r   r4   r   
fsspec_objr   r   r   !test_stringify_file_and_path_like`   s   

"z:TestCommonIOCapabilities.test_stringify_file_and_path_like	path_typec                 C   s2   |\}}|d| }t j|dd}||ksJ d S )Nr0   infer)compression)r   infer_compression)r   compression_formatr?   	extensionexpectedr   rA   r   r   r    test_infer_compression_from_pathg   s   z9TestCommonIOCapabilities.test_infer_compression_from_pathc              	   C   s   t jt dH}|dt|j d }t|d}t|jj s%J t	j
||jjks1J W d    n1 s;w   Y  W d    d S W d    d S 1 sSw   Y  d S )N)dirz~/z	/sometestw)tempfileTemporaryDirectoryr   homenamer   
get_handlehandleis_absoluter   r   r   )r   r?   tmpr   handlesr   r   r   test_get_handle_with_pathn   s   "z2TestCommonIOCapabilities.test_get_handle_with_pathc              	   C   sx   t  *}t|d}|j|ksJ W d    n1 sw   Y  |jr&J W d    n1 s0w   Y  |js:J d S )Nr)r   r   rM   rN   closed)r   input_bufferrQ   r   r   r   test_get_handle_with_bufferw   s   z4TestCommonIOCapabilities.test_get_handle_with_bufferc                 C   s   d}t jt|ddd@}d}d}	 |j|}t||ks J t||k r6t|j dks1J ||7 }n||7 }q||d	ksDJ W d    d S 1 sOw   Y  d S )
N&   a,b,c
1,2,3
©,®,®
Look,a snake,🐍rbFis_text       Tr   utf-8)r   rM   r   rN   readlenencode)r   datarQ   r2   	chunksizechunkr   r   r   )test_bytesiowrapper_returns_correct_bytes   s   "zBTestCommonIOCapabilities.test_bytesiowrapper_returns_correct_bytespyarrowc                 C   s   ddl m} d}tg dg dg dd}t|}tj|dd	d
}||j	 }t
|| |jr7J W d    d S 1 sBw   Y  d S )Nr   )csvrW   )1   ©Look)2   ®za snake)3rk   u   🐍abcrX   FrY   )re   rf   pd	DataFramer   r   rM   read_csvrN   	to_pandasr:   assert_frame_equalrT   )r   rf   ra   rE   srQ   dfr   r   r   test_get_handle_pyarrow_compat   s   "z7TestCommonIOCapabilities.test_get_handle_pyarrow_compatc                 C   s   t jt| jdd}t j|dd}W d    n1 sw   Y  t t| j}t|| t jt| jdd%}t|}t||jdg  tt ||jdd   W d    d S 1 saw   Y  d S )N   )rb   T)ignore_indexr   )	rq   rs   r   data1concatr:   ru   nextiloc)r   readerr2   rE   itfirstr   r   r   test_iterator   s   "z&TestCommonIOCapabilities.test_iteratorz#reader, module, error_class, fn_extr   rf   txtxlrdxlsxfeathertablesh5dtasas7bdatjsonpicklec                 C   s   t | tjtdd| }d| d}d| d}d}d}	d	| d
| d}
d| d}d| d}d| }t j|d| d| d| d|	 d|
 d| d| d| dd || W d    d S 1 siw   Y  d S )Nra   does_not_exist.File (b')?.+does_not_exist\.'? does not exist:\[Errno 2\] No such file or directory: '.+does_not_exist\.'zExpected object or value7path_or_buf needs to be a string file path or file-like#\[Errno 2\] File .+does_not_exist\.$ does not exist: '.+does_not_exist\.<   \[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.?\[Errno 2\] File o directory non esistente: '.+does_not_exist\.+Failed to open local file.+does_not_exist\.(|)match)r8   r9   r   r   r'   HEREraises)r   r   moduleerror_classfn_extr   msg1msg2msg3msg4msg5msg6msg7msg8r   r   r   test_read_non_existent   s(   


4
"z/TestCommonIOCapabilities.test_read_non_existentz#method, module, error_class, fn_exthtmlparquetc                 C   s|   t | tg dg dg dd}tjtddd| }t j|dd	 ||| W d    d S 1 s7w   Y  d S )
N)ry         )r   r      )r   r   r\   rm   ra   missing_folderr   z@Cannot save file into a non-existent directory: .*missing_folderr   )	r8   r9   rq   rr   r   r   r'   r   r   )r   methodr   r   r   dummy_framer   r   r   r   #test_write_missing_parent_directory   s   
"z<TestCommonIOCapabilities.test_write_missing_parent_directoryc                 C   s   t | tjdd| }|tddd  d| d}d| d	}d
}	d}
d| d| d	}d| d	}d| d	}d| }t j|d| d| d|	 d|
 d| d| d| d| dd || W d    d S 1 sqw   Y  d S )N~r   r   c                 S   s   t jd| S )Nr$   )r   r   r'   )xr   r   r   <lambda>
  s    zJTestCommonIOCapabilities.test_read_expands_user_home_dir.<locals>.<lambda>r   r   r   r   z0Unexpected character found when decoding 'false'r   r   r   r   r   r   r   r   r   r   )r8   r9   r   r   r'   setattrr   r   )r   r   r   r   r   monkeypatchr   r   r   r   r   r   r   r   r   r   r   r   test_read_expands_user_home_dir   s*   


4
"z8TestCommonIOCapabilities.test_read_expands_user_home_dirzreader, module, path)iora   rf   ziris.csv)r   ra   fixed_widthzfixed_width_format.txt)r   ra   excelz
test1.xlsx)r   ra   r   zfeather-0_3_1.feather)r   ra   
legacy_hdfzdatetimetz_object.h5)r   ra   statazstata10_115.dta)r   sasra   ztest1.sas7bdat)r   r   ra   ztsframe_v012.json)r   ra   r   zcategorical.0.25.0.picklec                 C   sT   t | || }t|}||}||}|dr"t|| d S t|| d S )Nz.pickle)r8   r9   r   endswithr:   assert_categorical_equalru   )r   r   r   r   datapathmypathr2   rE   r   r   r   test_read_fspath_all   s   
 
z-TestCommonIOCapabilities.test_read_fspath_allz"writer_name, writer_kwargs, moduleto_csvto_excelengineopenpyxl
to_featherto_htmlto_jsonto_latex	to_pickleto_stata
time_stampz2019-01-01 00:00c                 C   s|  |dv r	t d td}td}tdddgi}|}|{}t | t|}	t||}
|
|fi | |
|	fi | t|dH}t|d3}|d	kritj	|fi |}tj	|fi |}t
|| n| }| }||kswJ W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )
N)r   jinja2stringfspathAry   r   rX   r   )r8   r9   r:   r;   rq   rr   r   getattrr<   
read_excelru   r^   )r   writer_namewriter_kwargsr   p1p2rw   r   r   r   writerf_strf_pathr2   rE   r   r   r   test_write_fspath_allM  s2   




 Pz.TestCommonIOCapabilities.test_write_fspath_allc           	   	   C   s   t d tdddgi}td}td}|:}|(}t|}|j|dd |j|dd tj|dd}tj|dd}W d    n1 sHw   Y  W d    n1 sWw   Y  t	|| d S )	Nr   r   ry   r   r   r   r%   )key)
r8   r9   rq   rr   r:   r;   r   to_hdfread_hdfru   )	r   rw   r   r   r   r   r   r2   rE   r   r   r   test_write_fspath_hdf5t  s   


 z/TestCommonIOCapabilities.test_write_fspath_hdf5N)6r   r   r   r{   r!   r"   r*   td
skip_if_nor/   r3   r>   r8   markparametrize
path_typesrF   strr   r   rR   rV   rd   rx   r   rq   rs   FileNotFoundErrorread_fwfr   read_featherOSErrorr   
read_stataread_sas	read_jsonread_pickler   rr   r   r   r   r   
to_parquetr   r   r   r   
read_tabler   r   to_datetimer   r   r   r   r   r   r   5   s    	














r   c                 C   s   | ddddS )Nr   ra   rf   ztest_mmap.csvr   )r   r   r   r   	mmap_file  s   r   c                   @   sX   e Zd Zdd Zdd Zdd Zdd Zej	d	d
dgej	dddgdd Z
dS )TestMMapWrapperc                 C   s   t d}dd |_t rd}t}nd}tj}tj||d t	|d W d    n1 s.w   Y  t
|}W d    n1 sBw   Y  d}tjt|d t	|d W d    d S 1 sbw   Y  d S )	NzI am not a filec                   S   s   dS )Nr   r   r   r   r   r     s    z;TestMMapWrapper.test_constructor_bad_file.<locals>.<lambda>zThe parameter is incorrectz
[Errno 22]r   TzI/O operation on closed file)r   filenor   r   mmaperrorr8   r   r   _maybe_memory_mapr<   
ValueError)r   r   non_filemsgerrtargetr   r   r   test_constructor_bad_file  s"   

"z)TestMMapWrapper.test_constructor_bad_filec              
   C   s   t |i}| }tj|ddddC}|j}t|jjtjs J |D ]}t|}|	 |	 ks2J q"t
jtdd t| W d    n1 sIw   Y  W d    n1 sXw   Y  W d    d S W d    d S 1 spw   Y  d S )NrS   T)rZ   
memory_mapz^$r   )r<   	readlinesr   rM   rN   
isinstancebufferr   r}   stripr8   r   StopIteration)r   r   r   lineswrapperswrapperline	next_liner   r   r   	test_next  s&   

"zTestMMapWrapper.test_nextc              	   C   s   t  8}t  }|| tjtdd tj|dd W d    n1 s'w   Y  W d    d S W d    d S 1 s?w   Y  d S )NzUnknown enginer   pyt)r   )	r:   r;   makeDataFramer   r8   r   r   rq   rs   r   r   rw   r   r   r   test_unknown_engine  s   

"z#TestMMapWrapper.test_unknown_enginec                 C   sZ   t  }t  }|j|dd t |tj|dd W d   dS 1 s&w   Y  dS )z\
        'encoding' shouldn't be passed to 'open' in binary mode.

        GH 35058
        zw+br6   r   	index_colN)r:   r;   r  r   ru   rq   rs   r  r   r   r   test_binary_mode  s
   
"z TestMMapWrapper.test_binary_modeencodingzutf-16zutf-32compression_bz2xzc              	   C   s   t  }t  O}t t |j|||d W d   n1 s!w   Y  d}tjt|d t	j
|||d W d   n1 sBw   Y  W d   dS W d   dS 1 sZw   Y  dS )z
        bz2 and xz do not write the byte order mark (BOM) for utf-16/32.

        https://stackoverflow.com/questions/55171439

        GH 35681
        )rA   r  Nz&UTF-\d+ stream does not start with BOMr   )r:   r  r;   assert_produces_warningUnicodeWarningr   r8   r   UnicodeErrorrq   rs   )r   r  r  rw   r   r   r   r   r   test_warning_missing_utf_bom  s   

"z,TestMMapWrapper.test_warning_missing_utf_bomN)r   r   r   r   r  r  r  r8   r   r   r  r   r   r   r   r     s    r   c                   C   s   t dsJ t dsJ t drJ t drJ t dr#J t dr*J t dr1J t dr8J t d	s?J d S )
Nzgcs://pandas/somethingelse.comzgs://pandas/somethingelse.comzhttp://pandas/somethingelse.comzrandom:pandas/somethingelse.comz/local/pathzrelative/local/pathzthis is not fsspec://urlz({'url': 'gs://pandas/somethingelse.com'}z#RFC-3986+compliant.spec://something)r   is_fsspec_urlr   r   r   r   test_is_fsspec_url  s   r  r  r]   formatrf   r   c              	   C   s   t  }t  S}tj|d| d}t|d| | W d    n1 s&w   Y  tj|d| d}|dkr@tj|dd}nt|}W d    n1 sOw   Y  W d    n1 s^w   Y  t 	|| d S )NrH   )r7   r  to_rS   rf   r   r  )
r:   r  r;   codecsr<   r   rq   rs   r   ru   )r  r  rE   r   rN   rw   r   r   r   test_codecs_encoding  s   

r   c               
   C   s  t  } t  l}t|d$}td|}| | W d    n1 s&w   Y  W d    n1 s5w   Y  t|d&}td|}tj	|dd}W d    n1 sYw   Y  W d    n1 shw   Y  W d    n1 sww   Y  t 
| | d S )Nr5   r]   rX   r   r  )r:   r  r;   r<   r  	getwriterr   	getreaderrq   rs   ru   )rE   r   rN   encodedrw   r   r   r   test_codecs_get_writer_reader  s"   
r$  zio_class,mode,msgtz*a bytes-like object is required, not 'str'ro   z%string argument expected, got 'bytes'c              	   C   s   t  }|  2}tjt|d |j|d| d W d    n1 s$w   Y  W d    d S W d    d S 1 s<w   Y  d S )Nr   rH   r6   )r:   r  r8   r   	TypeErrorr   )io_classr7   r   rE   r  r   r   r   test_explicit_encoding	  s   "r(  encoding_errors)Nstrictreplacec              	   C   s<  d}d}|dkrd| d |d  d | }t tjdd}nd	|d  d
 | d | d }t tjdd}t [}t|}|| | dkretj	t
|d ||| d W d    n1 s_w   Y  n'||| d}|j| d}	tj|	|	gi|	d gd}
t||
 W d    d S W d    d S 1 sw   Y  d S )Nz'utf-8' codec can't decode byte   rf      ,   
r   r   r  s   {"s   ": {"s   ":"s   "}}index)orientr+  r   )r)  errors)r/  )r   rq   rs   r   r:   r;   r   write_bytesr8   r   UnicodeDecodeErrordecoderr   ru   )r)  r  r   bad_encodingcontentr   r   filerw   decodedrE   r   r   r   test_encoding_errors  sF   	

" r:  c               	   C   s|   t  0} tjtdd tj| ddd W d    n1 sw   Y  W d    d S W d    d S 1 s7w   Y  d S )Nzunknown error handler namer   rH   badr1  )r:   r;   r8   r   LookupErrorr   rM   r   r   r   r   test_bad_encdoing_errors=  s   
"r=  c                  C   sN   t jtdd} td | jtjksJ W d    d S 1 s w   Y  d S )Nz\[Errno 2\]r   doesnt_exist)r8   r   r   rq   rs   errnoENOENT)r   r   r   r   test_errno_attributeD  s   
"rA  c               	   C   sz   t jtdd, t } tj| ddd W d    n1 sw   Y  W d    d S W d    d S 1 s6w   Y  d S )Nr   r   rX   T)r   )r8   r   r   r   r   rM   )r  r   r   r   test_fail_mmapK  s   "rB  c               
   C   s   G dd d} t jtddA t %}t|d}|j|   W d    n1 s+w   Y  W d    n1 s:w   Y  W d    d S W d    d S 1 sRw   Y  d S )Nc                   @   s   e Zd Zdd ZdS )z&test_close_on_error.<locals>.TestErrorc                 S   s   t d)Ntest)r   r   r   r   r   closeT  s   z,test_close_on_error.<locals>.TestError.closeN)r   r   r   rD  r   r   r   r   	TestErrorS  s    rE  rC  r   rX   )r8   r   r   r   r   rM   created_handlesappend)rE  r  rQ   r   r   r   test_close_on_errorQ  s   "rH  r   c                 C   s8   t  }t| | W d    d S 1 sw   Y  d S r
   )r   r   dump)r   r  r   r   r   test_pickle_reader]  s   "rJ  )Dr   r  r?  	functoolsr   r   r   r   r   r   r   pathlibr   r   rI   r8   pandas.compatr   pandas.util._test_decoratorsutil_test_decoratorsr   pandasrq   pandas._testing_testingr:   pandas.io.commoncommonr   r   r   r   py.pathr   r,   rG  ImportErrorr   r+   dirname__file__r   r   fixturer   r   r  r   r   r   r$  r(  r:  r=  rA  rB  rH  rs   r   r   r   r   r   r   r   r   rJ  r   r   r   r   <module>   s    
  V
O

!