U
    sVc                     @   sL  d dl mZm Z 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Zd dlm  mZ d dlZd dlmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZ ej dd Z!ej d	d
 Z"ej#$dej%de&de&dgdej%de&de&dgdej%de&de&dgdej%de&de&dgdej%de&ddgG dd dZ'ej#$dej%dde&de&dgdej%dde&de&dgdej%dde&de&dgdej%dde&de&dgdej%dde&ddgej#(d
G dd dZ)G dd dZ*e&de&dG dd dZ+dS )    )datedatetime	timedelta)partial)BytesION)	DataFrameIndex
MultiIndexoption_context)	ExcelFileExcelWriter_OpenpyxlWriter_XlsxWriter_XlwtWriterregister_writerc              	   c   s    t | }|V  W 5 Q R X dS )z9
    Fixture to open file for use in each test case.
    N)tmensure_clean)ext	file_path r   F/tmp/pip-unpacked-wheel-xj8nt62q/pandas/tests/io/excel/test_writers.pypath#   s    r   c              	   c   s2   d| d d}t||  dV  W 5 Q R X dS )aM  
    Fixture to set engine for use in each test case.

    Rather than requiring `engine=...` to be provided explicitly as an
    argument in each test, this fixture sets a global option to dictate
    which engine should be used to write Excel files. After executing
    the test it rolls back said change to the global option.
    z	io.excel..z.writerN)stripr
   )enginer   Zoption_namer   r   r   
set_engine,   s    
r   r   .xlsxopenpyxlZxlrdZmarksz.xlsm.xlsxlwt
xlsxwriter.odsodfc                	   @   s  e Zd Zejddeejgd fdedejgd ifgdd Z	ejddedgejgd  fdeejgd fgd	d
 Z
dd Zdd Zdd Zejdddgejdddgejdddgejdddgdd Zdd Zdd ZdS )TestRoundTripzheader,expectedN   r   z
Unnamed: 0   c              	   C   sx   d}t dddgdddgdddgdd	d
gg}t|,}|j||ddd tj||dg|d}W 5 Q R X t|| d S )NZ	no_header    d         r&   ,  r%     Findexheaderr   
sheet_nameZusecolsr0   r   r   r   to_excelpd
read_excelassert_frame_equalselfr   r0   expectedfilenamedfr   resultr   r   r   !test_read_one_empty_col_no_headerH   s    (   z/TestRoundTrip.test_read_one_empty_col_no_headerc              	   C   sx   d}t dddgdddgdddgdd	d
gg}t|,}|j|dddd tj||dg|d}W 5 Q R X t|| d S )NZwith_headerr'   r(   r)   r*   r+   r&   r,   r%   r-   FTr.   r   r1   r3   r8   r   r   r   #test_read_one_empty_col_with_headerY   s    (   z1TestRoundTrip.test_read_one_empty_col_with_headerc              
   C   s   t ddgddgddggddgd	}t|}t|(}|j|d
ddd |j|ddd W 5 Q R X ddg|_t|2}tj|d
d ddgd}tj|dd ddgd}W 5 Q R X t	|| t	|| W 5 Q R X d S )Nr(   foor*   barr&   ZbazabcolumnsZData_no_headF)r0   r/   ZData_with_headr/   AB)r2   r0   names)r2   	index_colrI   )
r   r   r   r   r4   rE   r   r5   r6   r7   )r9   r   ZrefdfpthwriterreaderZxlsdf_no_headZxlsdf_with_headr   r   r   "test_set_column_names_in_parameteri   s*    "


   z0TestRoundTrip.test_set_column_names_in_parameterc           
   
      s   dd  dddg} fdd|D }t t||}t|f}t|$}| D ]\}}||| qPW 5 Q R X tj||dd	}|D ]}	t	||	 ||	  qW 5 Q R X d S )
Nc                 S   s&   dddgdddg }}t ||| gdS )N      !   r(   r*   r&   rD   )r   )Zcol_sheet_namedir   r   r   tdf   s    zDTestRoundTrip.test_creating_and_reading_multiple_sheets.<locals>.tdfZAAAZBBBZCCCc                    s   g | ]} |qS r   r   ).0srT   r   r   
<listcomp>   s     zKTestRoundTrip.test_creating_and_reading_multiple_sheets.<locals>.<listcomp>r   r2   rJ   )
dictzipr   r   r   itemsr4   r5   r6   r7   )
r9   r   sheetsdfsrK   ewZ	sheetnamer<   Zdfs_returnedrV   r   rW   r   )test_creating_and_reading_multiple_sheets   s    

z7TestRoundTrip.test_creating_and_reading_multiple_sheetsc              	   C   s   t |}tddiddiddiddid}tddiddiddiddid}|| tj|ddgdd}t || tddiddiddiddid}tddiddiddiddid	}|| tj|ddgdd}t || W 5 Q R X d S )
Nr   r(   r&      )ZOnexTwoXre   Y)Zeror'   )rb   rd   rg   )ri   zUnnamed: 4_level_1)r0   rJ   ))Begr'   ZMiddlerc   Tailrf   rm   rh   ))rj   zUnnamed: 1_level_1rk   rl   rn   r   r   r   r4   r5   r6   r7   )r9   r   r   r<   r:   actualr   r   r   &test_read_excel_multiindex_empty_level   sF    		
		
z4TestRoundTrip.test_read_excel_multiindex_empty_levelc_idx_namesTFr_idx_namesc_idx_levelsr(   r_idx_levelsc              	   C   sD  t |.}|dkr@|r@|dkr&|r@tjjdd}|j| |pJ|dk}	t dd||||}
|
| t	j
|tt|tt|d}t j|
||	d tj|
jdd d f< |
| t	j
|tt|tt|d}t j|
||	d tj|
jd	d d f< |
| t	j
|tt|tt|d}t j|
||	d W 5 Q R X d S )
Nr(   r&   z?Column index name cannot be serialized unless it's a MultiIndex)reason   )rJ   r0   )check_namesr   )r   r   pytestmarkZxfailnodeZ
add_markermakeCustomDataframer4   r5   r6   listranger7   npnaniloc)r9   r   rr   rs   rt   ru   requestrK   r{   rx   r<   Zactr   r   r   test_excel_multindex_roundtrip   sT         








z,TestRoundTrip.test_excel_multindex_roundtripc              	   C   s   t dddgtjdddd}| }|d jd|d< t|p}|| tj	|d	d
}t
|| tj	|dgd	d}t
|| dd }tj	|dg|d	d}t
|| W 5 Q R X d S )Nr(   r*   r&   z
2012-01-01)periods)coldate_stringsr   %m/%d/%Yr   rJ   )parse_datesrJ   c                 S   s   t | dS )Nr   )r   strptimerc   r   r   r   <lambda>      z;TestRoundTrip.test_read_excel_parse_dates.<locals>.<lambda>)r   date_parserrJ   )r   r5   Z
date_rangecopydtstrftimer   r   r4   r6   r7   )r9   r   r<   Zdf2rK   resr   r   r   r   test_read_excel_parse_dates   s&    
   z)TestRoundTrip.test_read_excel_parse_datesc              	   C   s   t tdtjtddddg}ttd|d}t|"}|	| tj
|ddgd}W 5 Q R X ttdt tdd	d
ddgg}t|| d S )Nr%   z
2020-01-01Z6M)startr   freqrF   r   r(   r   z(2020-01-31, 2020-07-31]z(2020-07-31, 2021-01-31]z(2021-01-31, 2021-07-31]z(2021-07-31, 2022-01-31])r	   from_arraysr   r5   Zinterval_range	Timestampr   r   r   r4   r6   r7   )r9   r   Zmidxr<   rK   r=   r:   r   r   r   "test_multiindex_interval_datetimes  s4      
z0TestRoundTrip.test_multiindex_interval_datetimes)__name__
__module____qualname__rz   r{   parametrizer   r   r   r>   r?   rN   r`   rq   r   r   r   r   r   r   r   r$   ;   s(   *
,
/1r$   z
engine,extc                   @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	e
jdejejejejgdd Ze
jdejejejg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$d0d1 Z%d2d3 Z&d4d5 Z'd6d7 Z(d8d9 Z)d:d; Z*d<d= Z+e
jd>d?d@ge
jdAdBdCdDge
jdEdBdCdDgdFdG Z,dHdI Z-dJdK Z.dLdM Z/e
jdNdOdPgdQdR Z0dSdT Z1dUdV Z2dWdX Z3dYdZ Z4d[d\ Z5d]d^ Z6d_d` Z7dadb Z8dcdd Z9dedf Z:dgdh Z;didj Z<dkdl Z=e
jdmdne>gdodp Z?dqdr Z@dsdt ZAdudv ZBdwdx ZCe
jdydzd{d|gd}d~ ZDe
jEde
jdddg dffgdd ZFdd ZGdnS )TestExcelWriterc           	   	   C   s   d}d}t j|dfd}t jd|fd}t|}t|}d}tjt|d || W 5 Q R X tjt|d || W 5 Q R X d S )Ni  i@  r(   )shapezsheet is too largematch)r   zerosr   rz   raises
ValueErrorr4   )	r9   r   Zbreaking_row_countZbreaking_col_countZrow_arrZcol_arrZrow_dfZcol_dfmsgr   r   r   test_excel_sheet_sizeO  s    z%TestExcelWriter.test_excel_sheet_sizec              	   C   sz   t tjdd}|| t|}tj|ddd}W 5 Q R X t	|| d}t
jt|d t|d W 5 Q R X d S )N
   r*   r   rY   zWorksheet named '0' not foundr   0)r   r   randomrandnr4   r   r5   r6   r   r7   rz   r   r   )r9   r   gtxlr<   r   r   r   r   test_excel_sheet_by_name_raisea  s    

z.TestExcelWriter.test_excel_sheet_by_name_raisec              	   C   s   t |8}||d | }|jd d d |_||d W 5 Q R X t|>}tj|ddd}tj|ddd}t|| t|| W 5 Q R X d S )NZData1ry   ZData2r   rY   )	r   r4   r   rE   r   r5   r6   r   r7   )r9   framer   rL   frame2rM   Zfound_dfZ	found_df2r   r   r   !test_excel_writer_context_managern  s    

z1TestExcelWriter.test_excel_writer_context_managerc                 C   s  |  }tj|d d d< ||d |j|dddgd |j|ddd |j|ddd ||d tj|dd	d
}t|| |j|ddd tj|dd d
}|j|_t|| |j|ddd tj|dd	dgd}t|| |j|ddd tj|dd	dgd}t|| |j|ddd tj|dd	ddgd}t|| ||d tj|d	d}t|| ||d tj|d	d}t|| |d }|| tj|d	d}t|	 | d S )NrG   rw   test1rH   rD   Fr0   rF   r   rY   ZNA)Zna_rep)r2   rJ   Z	na_valuesZ88X   g      V@Sheet1r   r   )
r   r   r   r4   r5   r6   r   r7   r/   Zto_frame)r9   r   r   reconsrV   r   r   r   test_roundtrip|  sJ       
zTestExcelWriter.test_roundtripc              	   C   sP   |  }d|d< ||d t|}tj|ddd}W 5 Q R X t|| d S )NrA   r@   r   r   rY   )r   r4   r   r5   r6   r   r7   )r9   r   r   Zmixed_framerM   r   r   r   r   
test_mixed  s    
zTestExcelWriter.test_mixedc              	   C   s`   |}t jt|jd d}||_||d t|}t j|ddd}W 5 Q R X t	|| d S )Nr   r   r   rY   )
r5   DatetimeIndexr   asarrayr/   r4   r   r6   r   r7   )r9   tsframer   r<   r/   rM   r   r   r   r   test_ts_frame  s    
zTestExcelWriter.test_ts_framec                 C   s^   |  }tj|d d d< ||d |j|dddgd |j|ddd |j|ddd d S )	NrG   rw   r   rH   rD   Fr   rF   )r   r   r   r4   )r9   r   r   r   r   r   test_basics_with_nan  s    z$TestExcelWriter.test_basics_with_nannp_typec           	   	   C   s   t tjjdddd|d}||d t|}tj|ddd}W 5 Q R X |tj	}t
|| tj|ddd}t
|| |t}|jt|_|jt|_t
jtd	d
 tj|dddd}W 5 Q R X t
|| d S )Nr   )r   r*   sizedtyper   r   rY   convert_float is deprecatedr   F)r2   convert_floatrJ   )r   r   r   randintr4   r   r5   r6   astypeint64r   r7   floatrE   r/   assert_produces_warningFutureWarning)	r9   r   r   r<   rM   r   Z	int_frameZrecons2Zfloat_framer   r   r   test_int_types  s,    

    zTestExcelWriter.test_int_typesc              	   C   sZ   t tjd|d}||d t|}tj|ddd|}W 5 Q R X t	
|| d S )Nr   r   r   r   rY   )r   r   r   Zrandom_sampler4   r   r5   r6   r   r   r7   )r9   r   r   r<   rM   r   r   r   r   test_float_types  s    
z TestExcelWriter.test_float_typesc              	   C   s^   t ddddgtjd}||d t|}tj|dddtj}W 5 Q R X t	|| d S )Nr(   r   TFr   r   rY   )
r   r   Zbool_r4   r   r5   r6   r   r   r7   r9   r   r<   rM   r   r   r   r   test_bool_types  s    
zTestExcelWriter.test_bool_typesc              	   C   s\   t dtjfddtj fg}||d t|}tj|ddd}W 5 Q R X t|| d S )Nr(   )r*   r&   rw   r   r   rY   )	r   r   infr4   r   r5   r6   r   r7   r   r   r   r   test_inf_roundtrip  s
    
z"TestExcelWriter.test_inf_roundtripc              	   C   s.  t jt|jd d}||_| }tj|d d d< ||d |j|dddgd |j|ddd |j|ddd	 t|}||d ||d
 W 5 Q R X t	|>}t j
|ddd}t|| t j
|d
dd}t|| W 5 Q R X dt|jkstd|jd kstd
|jd ks*td S )Nr   rG   rw   r   rH   rD   Fr   rF   Ztest2r   rY   r*   r(   )r5   r   r   r   r/   r   r   r4   r   r   r6   r   r7   lensheet_namesAssertionError)r9   r   r   r   r/   rL   rM   r   r   r   r   test_sheets  s&    

zTestExcelWriter.test_sheetsc              	   C   s   |  }tj|d d d< ||d |j|dddgd |j|ddd |j|ddd td	d
ddg}|j|d|d t|}tj|ddd}W 5 Q R X |  }||_t	
|| d S )NrG   rw   r   rH   rD   Fr   rF   ZAArf   rh   Zr   rY   )r   r   r   r4   r   r   r5   r6   rE   r   r7   )r9   r   r   Zcol_aliasesrM   rsxpr   r   r   test_colaliases#  s    
zTestExcelWriter.test_colaliasesc              	   C   s"  |  }tj|d d d< ||d |j|dddgd |j|ddd |j|ddd ttjd	d
dk}|j|ddg|d t|}tj	|ddd
tj}W 5 Q R X dg|j_|jj|jjkstttjd	d
dk}|j|ddddg|d t|}tj	|ddd
tj}W 5 Q R X dg|j_|jj|jjksHtttjd	d
dk}|j|dd|d t|}tj	|ddd
tj}W 5 Q R X dg|j_t||
t |j|dddddgd|d |  }|ddg}t|}tj	|dddgd}W 5 Q R X t|| d S )NrG   rw   r   rH   rD   Fr   rF   r   r*   r   test)Zindex_labelmerge_cellsrY   dummyZdummy2CD)rE   r/   r   r(   )r   r   r   r4   r   r   r   r   r5   r6   r   r   r/   rI   r   r   r7   bool	set_index)r9   r   r   r   r<   rM   r   r   r   r   test_roundtrip_indexlabels5  s^    







z*TestExcelWriter.test_roundtrip_indexlabelsc              	   C   sr   t tjdd}d|j_|j||d t|}tj	||j
d dd}W 5 Q R X t|| |jjdksntd S )Nr   r%   r@   r   r   rY   )r   r   r   r   r/   namer4   r   r5   r6   r   r   r7   r   )r9   r   r   r<   xfr=   r   r   r   test_excel_roundtrip_indexnamen  s    
 z.TestExcelWriter.test_excel_roundtrip_indexnamec              	   C   sz   t jt|jd d}||_| }dd |jD |_|j|d|d t|}t j|ddd}W 5 Q R X t	
|| d S )Nr   c                 S   s   g | ]}|  qS r   )r   )rU   rc   r   r   r   rX     s     zATestExcelWriter.test_excel_roundtrip_datetime.<locals>.<listcomp>r   r   r   rY   )r5   r   r   r   r/   r   r4   r   r6   r   r7   )r9   r   r   r   r/   ZtsfrM   r   r   r   r   test_excel_roundtrip_datetimez  s    
z-TestExcelWriter.test_excel_roundtrip_datetimec              
   C   s\  t tdddtdddgtddd	d
ddtddddddggddgddgd}t tdddtdddgtddd	d
ddtddddddggddgddgd}t|}t|}||d W 5 Q R X t|ddd}||d W 5 Q R X t|}tj	|ddd}	W 5 Q R X t|}
tj	|
ddd}W 5 Q R X W 5 Q R X t
|	| t
|| d S )Ni  r(      i  	      i  rw         rQ   r%   r*         ZDATEZDATETIMErf   rh   r/   rE   r   z
DD.MM.YYYYzDD.MM.YYYY HH-MM-SS)Zdate_formatZdatetime_formatr   rY   )r   r   r   r   r   r   r4   r   r5   r6   r7   )r9   r   r   r<   Zdf_expected	filename2Zwriter1Zwriter2Zreader1Zrs1Zreader2Zrs2r   r   r   test_excel_date_datetime_format  s<    ""	


$z/TestExcelWriter.test_excel_date_datetime_formatc              	   C   s   t tjjddddtjd}| }t|d d|d< t|d dt	|d< |
|d t|}tj|ddd	}W 5 Q R X t|| d S )
Nr   r      r(   r   r   r   newr   rY   )r   r   r   r   r   r   r5   cutr   strr4   r   r6   r   r7   r9   r   r<   r:   rM   r   r   r   r    test_to_excel_interval_no_labels  s    
z0TestExcelWriter.test_to_excel_interval_no_labelsc                 C   s   t tjjddddtjd}| }tj|d dddd	d
ddddddg
d}||d< tt	||d< |
|d t|}tj|ddd}W 5 Q R X t|| d S )Nr   r   r   r   r   r   rG   rH   r   r   EFGHIJ)labelsr   r   rY   )r   r   r   r   r   r   r5   r   ZSeriesr~   r4   r   r6   r   r7   )r9   r   r<   r:   Z	intervalsrM   r   r   r   r   test_to_excel_interval_labels  s      
z-TestExcelWriter.test_to_excel_interval_labelsc              	   C   s   t tjjdddddgtjd}| }|d dd |d	< |d d
d |d	< ||d t|}t	j
|ddd}W 5 Q R X t|| d S )Nr   r   r   r   rG   )rE   r   c                 S   s
   t | dS )Nseconds)r   r   r   r   r   r     r   z9TestExcelWriter.test_to_excel_timedelta.<locals>.<lambda>r   c                 S   s   t | d d S )Nr   iQ )r   total_secondsr   r   r   r   r     r   r   r   rY   )r   r   r   r   r   r   applyr4   r   r5   r6   r   r7   r   r   r   r   test_to_excel_timedelta  s      
z'TestExcelWriter.test_to_excel_timedeltac              	   C   sX   |j ddd }||d t|}tj|ddd}W 5 Q R X t||d d S )NMZperiod)kindZsht1r   rY   )	ZresampleZmeanr4   r   r5   r6   r   r7   Z	to_period)r9   r   r   r   rM   r   r   r   r   test_to_excel_periodindex  s
    
z)TestExcelWriter.test_to_excel_periodindexc              	   C   s   t t|jd dd}tj|ddgd}||_|j|ddd |j|dd	d
gd |j|d|d t|}t	j
|dddgd}W 5 Q R X t|| d S )Nr*   ry   firstsecondrI   r   Fr   rG   rH   rD   r   r   r(   rY   )r   aranger   r/   reshaper	   r   r4   r   r5   r6   r   r7   )r9   r   r   r   arrays	new_indexrM   r<   r   r   r   test_to_excel_multiindex  s    
z(TestExcelWriter.test_to_excel_multiindexc                 C   sb   t d ddgdddgtjdd}|ddg}|j||d	 tj|d
dgd}t	|| d S )Nr*   r&   r   r      rG   rH   r   rG   rH   r   r   r(   r   )
r   r   r   sampler   r4   r5   r6   r   r7   )r9   r   r   r<   Zdf1r   r   r   "test_to_excel_multiindex_nan_label  s
    $z2TestExcelWriter.test_to_excel_multiindex_nan_labelc              	   C   s   t t|jd dd}tj|ddgd}||_tdddd	g}||_d
dg}|s\d
}|j	|d|d t
|}tj|d|d
dgd}	W 5 Q R X |s|jjdddd}
dd t|
 D |_t||	 d S )Nr*   ry   r  r	  r
  )(   r(   )r  r*   )2   r(   )r  r*   r   r(   r   r   r2   r0   rJ   F)ZsparsifyZadjoinrI   c                 S   s   g | ]}d  tt|qS )r   )joinmapr   )rU   qr   r   r   rX      s     zATestExcelWriter.test_to_excel_multiindex_cols.<locals>.<listcomp>)r   r  r   r/   r  r	   r   from_tuplesrE   r4   r   r5   r6   formatr[   r   r7   )r9   r   r   r   r  r  Znew_cols_indexr0   rM   r<   Zfmr   r   r   test_to_excel_multiindex_cols  s(    
   z-TestExcelWriter.test_to_excel_multiindex_colsc              	   C   s   |j tt|j g}t||_ ddg|j _|j|d|d t|}t	j
|dddgd}W 5 Q R X t|| |j jdkstd S )	Ntimer@   r   r   r   r(   rY   )r  r@   )r/   r   r  r   r	   r   rI   r4   r   r5   r6   r   r7   r   )r9   r   r   r   r  rM   r   r   r   r   test_to_excel_multiindex_dates#  s    
z.TestExcelWriter.test_to_excel_multiindex_datesc              	   C   sz   t ddgddgddgd}| }tdd	g}||_|j|d
dd t|}tj|d
d}W 5 Q R X t	
|| d S )Nr   r   r  r  r  <   )rB   rC   c)F   P   )Z   r)   r   FrF   r2   )r   r   r	   r  r/   r4   r   r5   r6   r   r7   )r9   r   Zframe1r   Zmulti_indexrM   Zframe3r   r   r   'test_to_excel_multiindex_no_write_index0  s    
z7TestExcelWriter.test_to_excel_multiindex_no_write_indexc              	   C   st   t g dddgd}t g tjg ddgddgd}||d t|}tj|dd}W 5 Q R X tj||d	d	d
 d S )Nr   r(   r*   rD   r
  r   r   r$  F)Zcheck_index_typeZcheck_dtype)	r   r	   r  r4   r   r5   r6   r   r7   )r9   r   r:   r<   rM   r=   r   r   r   test_to_excel_empty_multiindexE  s    
   z.TestExcelWriter.test_to_excel_empty_multiindexc              	   C   s   t dddgdddggddgd	d
dgd}|j|ddd t|}tj|ddd}W 5 Q R X t dddgdddggddgd	d
dgd}t|| d S )N翶~Ϛ?9DܜJ?>D)?.4i(@33333@̤ArG   rH   rf   rh   r   r   r   %.2fZfloat_formatr   rY   Q?q=
ףp?=
ףp=?p=
ף(@)r   r4   r   r5   r6   r   r7   )r9   r   r<   rM   r=   r:   r   r   r   test_to_excel_float_formatR  s    
z*TestExcelWriter.test_to_excel_float_formatc              	   C   sp   t dddgdddggddgd	d
dgd}td| 0}|j|dd tj|ddd}t|| W 5 Q R X d S )Nu   ƒu   Ɠu   Ɣu   ƕu   Ɩu   Ɨu   AƒrH   u   XƓrh   r   r   z__tmp_to_excel_float_format__.Z	TestSheetr$  r   rY   r3   )r9   r   r<   r;   r=   r   r   r   test_to_excel_output_encodingd  s    z-TestExcelWriter.test_to_excel_output_encodingc              
   C   s   t d| }z2zt|d}W n tk
r>   td Y nX W 5 |  X tdddgddd	ggd
dgdddgd}|j|ddd t	|}t
j|ddd}W 5 Q R X W 5 Q R X tdddgddd	ggd
dgdddgd}t || d S )Nu   ƒu.wbz$No unicode file names on this systemr'  r(  r)  r*  r+  r,  rG   rH   rf   rh   r   r   r   r-  r.  r   rY   r/  r0  r1  r2  )r   r   closeopenUnicodeEncodeErrorrz   skipr   r4   r   r5   r6   r7   )r9   r   r;   fr<   rM   r=   r:   r   r   r   test_to_excel_unicode_filenameq  s(    

$z.TestExcelWriter.test_to_excel_unicode_filenameuse_headersTFr_idx_nlevelsr(   r*   r&   c_idx_nlevelsc              	      s(  d fdd	}|rdnd }|t dg||}|jdks<t|jd tjk	sPtd}	d}
tj|	|
||d	}|d
krd}tj	t
|d |||dd W 5 Q R X n|||}|r|j|	|
| fkstn|j|	d
 |
| fksttt|jD ]4}tt|jD ] }|j||f tjk	s tq qd S )NTr   c              
      sH   | j | |d t$}tj||jd |dW  5 Q R  S Q R X d S )N)r0   r   r/   r   r2   r0   )r4   r   r5   r6   r   )datar0   Z
parser_hdrr/   r   r   r   r   r   	roundtrip  s    
  z;TestExcelWriter.test_excel_010_hemstring.<locals>.roundtrip)r(   r*   )r   r   rw   r&   )r=  r>  r(   z_Writing to Excel with MultiIndex columns and no index \('index'=False\) is not yet implemented.r   FrF   )Tr   T)r   r   r   r   r   r   r   r}   rz   r   NotImplementedErrorr   r   r/   rE   )r9   r   r>  r=  r<  r   rB  Zparser_headerr   ZnrowsZncolsr<   r   rr   r   rA  r   test_excel_010_hemstring  s2    	   
z(TestExcelWriter.test_excel_010_hemstringc              
   C   s  t dddgdddgdddggdddgd}||d t dddgdddgdddggdddgd}tj|dd	d
}t|| tjtdd tj|dd	dd}W 5 Q R X t|| t ddddgddddggddddgd}||d tj|dd	d
}t ddddgddddggddddgd}t|| |j|dddd tj|dd d}t ddddgddddgg}t|| d}tjtdd2 tj	t
|d tj|dd dd W 5 Q R X W 5 Q R X d S )Nr(   r*   r&   rG   rH   rD   r   zB.1r   rY   z,the 'mangle_dupe_cols' keyword is deprecatedr   T)r2   rJ   mangle_dupe_colsr%   rw      ra      A.1Fr.   r?  z3Setting mangle_dupe_cols=False is not supported yet)r2   r0   rF  )r   r4   r5   r6   r   r7   r   r   rz   r   r   )r9   r   r<   r:   r=   r   r   r   r   test_duplicated_columns  sV    *     ( 
    z'TestExcelWriter.test_duplicated_columnsc                 C   sj   t dddgdddgd}|j|dddgd tj|ddd	}t|d |d  t|d |d  d S )
Nr(   r*   rG   rH   r   rH   rG   rD   r   r?  )r   r4   r5   r6   r   assert_series_equal)r9   r   write_frame
read_framer   r   r   test_swapped_columns  s
    z$TestExcelWriter.test_swapped_columnsc              	   C   sz   t dddgdddgd}tjtdd |j|dddgd	 W 5 Q R X tjtd
d |j|dddgd	 W 5 Q R X d S )Nr(   r*   rK  zNot all names specifiedr   r   rH   r   rD   z.'passes columns are not ALL present dataframe'r   )r   rz   r   KeyErrorr4   )r9   r   rM  r   r   r   test_invalid_columns  s     z$TestExcelWriter.test_invalid_columnsz#to_excel_index,read_excel_index_col)Tr   )FNc                 C   sd   t dddgdddgdddgd}|j|dddg|d |ddg }tj|d|d	}t|| d S )
Nr(   r*   r&   r  Zcol_subset_bugrG   rH   rE   r/   rY   r   r4   r5   r6   r   r7   )r9   r   Zto_excel_indexZread_excel_index_colrM  r:   rN  r   r   r   test_write_subset_columns  s    	"     z)TestExcelWriter.test_write_subset_columnsc                 C   sv   t dddgdddgd}||d tj|ddd}d |jd	< d |jd
< d |jd< tj|dddd}t|| d S )None#onetwo#tworK  test_cr   rY   )r(   r   r(   r(   )r*   r(   #r2   commentrJ   )r   r4   r5   r6   r   r   r7   r9   r   r<   Zresult1Zresult2r   r   r   test_comment_arg  s    


z TestExcelWriter.test_comment_argc                 C   sT   t dddgdddgd}||d tj|dd}tj|dd d}t|| d S )	NrU  rV  rW  rX  rK  rY  r$  )r2   r]  rS  r^  r   r   r   test_comment_default/  s
    z$TestExcelWriter.test_comment_defaultc                 C   sb   t dddgdddgd}||d t dd dgdd d gd}tj|dddd	}t|| d S )
NrU  rV  rW  rX  rK  rY  r[  r   r\  rS  r9   r   r<   r:   r=   r   r   r   test_comment_used<  s
    z!TestExcelWriter.test_comment_usedc                 C   sT   t ddgddgd}|j|dd t dgd	gd}tj|d
d}t|| d S )N1z#223)rB   rC   FrF   r(   r*   r[  )r]  rS  ra  r   r   r   test_comment_empty_lineJ  s
    z'TestExcelWriter.test_comment_empty_linec                 C   s   t ddddddt ddddddt ddddd	d
t ddddddt ddddddt ddddddt ddddddt ddddddt ddddddt ddddddt ddddddg}td|i}||d tj|ddd }t|d |d  d S )!Ni  r(   r   r*   r&   -   8   r%      1   rG  *   ra   9   #   r   )   r   rO            5      %   r      r   4   rG   r   r?  )r   r   r4   r5   r6   r   rL  )r9   r   Z	datetimesrM  rN  r   r   r   test_datetimesV  s     zTestExcelWriter.test_datetimesc              
   C   sn   t  ^}ttjdd}t||d}|| W 5 Q R X |d tj	|dd}t
|| W 5 Q R X d S )Nr   r*   r   r   r   )r   r   r   r   r   r   r4   seekr5   r6   r   r7   )r9   r   Zbior<   rL   Z	reread_dfr   r   r   test_bytes_iol  s    
zTestExcelWriter.test_bytes_ioc                 C   s~   t dddgdddgdddgd	d
dgd}||d tj|dddd}| }|jt|_|j	d|_t
|| d S )NrB   rC   r   er*   )rR   r:  r(   g      @ZappleZbananaZcherry)mixednumericr   r   r   r  r   )r   r4   r5   r6   r   r|  r  r   r}  r   r   r7   r9   r   r<   readr:   r   r   r   test_write_lists_dicty  s    z%TestExcelWriter.test_write_lists_dictc                 C   sF   t ddgddgd}||d tj|ddd}|}t|| d S )	Nr(   r*   r&   r%   )renderr@  r   r   r   rS  r~  r   r   r   test_render_as_column_name  s
    z*TestExcelWriter.test_render_as_column_namec                 C   sV   t ddggddgd}|ddd}|| tj|dgdgd	d
}t|| d S )Nr@   rA   col1col2rD   TF)r@   rA   r   )Ztrue_valuesZfalse_valuesrJ   )r   replacer4   r5   r6   r   r7   )r9   r   r<   r:   rN  r   r   r   !test_true_and_false_value_options  s    
   z1TestExcelWriter.test_true_and_false_value_optionsc                 C   sJ   t ddgddggddgd}|j|dd	d
 tj|dd}t|| d S )Nr(   r*   r&   r%   r  r  rD   r   rZ  )Zfreeze_panesr   r   rS  )r9   r   r:   r=   r   r   r   test_freeze_panes  s    z!TestExcelWriter.test_freeze_panesc                 C   sJ   t  }t|j|d}ttjdd}t j||d| d}t || d S Nrx  r   r   r@   )r   )r   makeDataFramer   r4   r5   r6   Zround_trip_pathlibr7   r9   r   r   r<   rL   rM   r=   r   r   r   test_path_path_lib  s
    z"TestExcelWriter.test_path_path_libc                 C   sJ   t  }t|j|d}ttjdd}t j||d| d}t || d S r  )r   r  r   r4   r5   r6   Zround_trip_localpathr7   r  r   r   r   test_path_local_path  s
    z$TestExcelWriter.test_path_local_pathc              	   C   s   t tdtdftdtdfg}ttd|d}|| tj	t
dd tj|dd	gdd
d}W 5 Q R X |jjdd |jd D dd |jd	 D gdd	gd|_|jtj|_t|| d S )NZ2018Z2018Q1Z2018Q2)r*   r*   rD   r   r   r   r(   F)r0   rJ   r   c                 S   s   g | ]}t |qS r   )r   )rU   rS   r   r   r   rX     s     zCTestExcelWriter.test_merged_cell_custom_objects.<locals>.<listcomp>)level)r	   r  r5   ZPeriodr   r   Zonesr4   r   r   r   r6   rE   Z
set_levelslevelsr/   r   float64r7   )r9   r   mir:   r=   r   r   r   test_merged_cell_custom_objects  s.    
    &z/TestExcelWriter.test_merged_cell_custom_objectsr   Nc              	   C   s   |}t jd|d}t|g|d}tjtdd || W 5 Q R X | }t|g|d}tjtdd || W 5 Q R X d S )NZ2019)tzr   zExcel does not supportr   )r5   r   r   rz   r   r   r4   Zto_pydatetime)r9   r   Ztz_aware_fixturer   r  r@  r<   r   r   r    test_raise_when_saving_timezones  s    z0TestExcelWriter.test_raise_when_saving_timezonesc                 C   sh   t ddgddgd}|j|dddgdd	 t|}t dddgdddggddd
gd}t|| d S )Nr   r(   r   rO   rK  rG   rH   FrR  rI  rD   rS  )r9   r   r<   r=   r:   r   r   r   'test_excel_duplicate_columns_with_names  s
    
"z7TestExcelWriter.test_excel_duplicate_columns_with_namesc              
   C   sJ   d}t |2}tjtt|d t|dd W 5 Q R X W 5 Q R X d S )Nz7if_sheet_exists is only valid in append mode (mode='a')r   r  )Zif_sheet_exists)r   r   rz   r   r   reescaper   )r9   r   r   r:  r   r   r   test_if_sheet_exists_raises  s    z+TestExcelWriter.test_if_sheet_exists_raisesc              
   C   sZ   t |F}t||d}t | W 5 Q R X t|}t }t || W 5 Q R X d S Nrx  )r   r   r   r   r4   r5   r6   r7   )r9   r   r   r   rL   r=   r:   r   r   r   test_excel_writer_empty_frame  s    
z-TestExcelWriter.test_excel_writer_empty_framec              	   C   sF   t |2}t j||d t|}t }t || W 5 Q R X d S r  ro   )r9   r   r   r   r=   r:   r   r   r   test_to_excel_empty_frame  s
    
z)TestExcelWriter.test_to_excel_empty_frameattrZ	cur_sheetZhandlesr   c                 C   sh   t |T}t|@}| d}t jt|d t|| W 5 Q R X t | W 5 Q R X W 5 Q R X d S Nz is not part of the public APIr   )r   r   r   r   r   getattrr   r4   )r9   r   r   r  r   rL   r   r   r   r   test_deprecated_attr  s    

z$TestExcelWriter.test_deprecated_attrz-ignore:Calling close():UserWarning:xlsxwriterz
attr, args)saver   Zwrite_cellsr   c                 C   sl   t |X}t|D}| d}t | t jt|d t|||  W 5 Q R X W 5 Q R X W 5 Q R X d S r  )r   r   r   r   r4   r   r   r  )r9   r   r   r  argsr   rL   r   r   r   r   test_deprecated_method	  s    

z&TestExcelWriter.test_deprecated_methodc                 C   sd   t |P}t|<}d}t | |j}t jt|d ||_W 5 Q R X W 5 Q R X W 5 Q R X d S )Nz:Setting the `book` attribute is not part of the public APIr   )r   r   r   r   r4   bookr   r   )r9   r   r   r   rL   r   r  r   r   r   test_deprecated_book_setter  s    
z+TestExcelWriter.test_deprecated_book_setter)Hr   r   r   r   r   r   r   r   r   r   rz   r{   r   r   Zint8Zint16Zint32r   r   Zfloat16Zfloat32r  r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r%  r&  r3  r4  r;  rE  rJ  rO  rQ  rT  r_  r`  rb  rf  rw  rz  r  r  r  r  r  r  r  objectr  r  r  r  r  r  filterwarningsr  r  r   r   r   r   r   5  s   1	

	9,	4/


		


 
r   c                   @   s   e Zd Zejdejede	ddeje
de	ddejede	ddgdd	 Zd
d Zdd Zejdejde	ddejde	ddejde	ddgdd ZdS )TestExcelWriterEngineTestsz	klass,extr   r!   r   r   r   r    c              
   C   s^   t |J}t|6}|dkr8tdr8t|tsFtnt||sFtW 5 Q R X W 5 Q R X d S )Nr   r!   )r   r   r   tdZsafe_import
isinstancer   r   )r9   klassr   r   rL   r   r   r   test_ExcelWriter_dispatch$  s
    	
z4TestExcelWriterEngineTests.test_ExcelWriter_dispatchc              	   C   s&   t jtdd td W 5 Q R X d S )Nz	No enginer   Znothing)rz   r   r   r   r9   r   r   r    test_ExcelWriter_dispatch_raises5  s    z;TestExcelWriterEngineTests.test_ExcelWriter_dispatch_raisesc                 C   s   G dd dt }t| tdd` d}t|>}t |}t||sLtW 5 Q R X tdd}|| W 5 Q R X |	  W 5 Q R X td}|j|dd W 5 Q R X |	  d S )	Nc                   @   sP   e Zd ZdZdZdZdZdZdd Zdd Z	dd	 Z
ed
d Zedd ZdS )zCTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClassF)ZxlsxZxlsr   c                 S   s   d S )Nr   r  r   r   r   r  A  s    zHTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass.bookc                 S   s   dt | _d S NT)typecalled_saver  r   r   r   _saveD  s    zITestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass._savec                 _   s   dt | _d S r  )r  called_write_cells)r9   r  kwargsr   r   r   _write_cellsG  s    zPTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass._write_cellsc                 S   s   dt | _d S r  )r  called_sheetsr  r   r   r   r]   J  s    zJTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass.sheetsc                 S   s.   | j s
t| jst| jrtd| _ d| _d S )NF)r  r   r  r  )clsr   r   r   assert_called_and_resetN  s
    


z[TestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass.assert_called_and_resetN)r   r   r   r  r  r  Z_supported_extensionsZ_enginer  r  r  propertyr]   classmethodr  r   r   r   r   
DummyClass:  s   
r  zio.excel.xlsx.writerr   zsomething.xlsxr(   zsomething.xlsrx  )
r   r   r
   r   r   r  r   r}   r4   r  )r9   r  r   filepathrL   r<   r   r   r   test_register_writer9  s    
z/TestExcelWriterEngineTests.test_register_writerr   r"   r#   c              
   C   sF   t d}tjt|d$ tdddidd W 5 Q R X W 5 Q R X d S )Nz*Cannot use both engine_kwargs and **kwargsr   r'   rB   r(   r*   )Zengine_kwargsrC   )r  r  rz   r   r   r   )r9   r   r   r   r   r   $test_engine_kwargs_and_kwargs_raisese  s    

z?TestExcelWriterEngineTests.test_engine_kwargs_and_kwargs_raisesN)r   r   r   rz   r{   r   paramr   r  
skip_if_nor   r   r  r  r  r  r   r   r   r   r  #  s$   
	,r  c                   @   s   e Zd Zdd Zdd ZdS )
TestFSPathc              
   C   s^   t dJ}tdddgi}|| t|}t|}W 5 Q R X ||ksPtW 5 Q R X d S )Nfoo.xlsxrG   r(   r*   )r   r   r   r4   r   osfspathr   )r9   r   r<   r   r=   r   r   r   test_excelfile_fspathx  s    

z TestFSPath.test_excelfile_fspathc              
   C   sD   t d0}t|}t|t|ks,tW 5 Q R X W 5 Q R X d S )Nr  )r   r   r   r  r  r   r   )r9   r   rL   r   r   r   test_excelwriter_fspath  s    
z"TestFSPath.test_excelwriter_fspathN)r   r   r   r  r  r   r   r   r   r  u  s   r  ),r   r   r   	functoolsr   ior   r  r  Znumpyr   rz   Zpandas.util._test_decoratorsutilZ_test_decoratorsr  Zpandasr5   r   r   r	   r
   Zpandas._testingZ_testingr   Zpandas.io.excelr   r   r   r   r   r   Zfixturer   r   r{   r   r  r  r$   Zusefixturesr   r  r  r   r   r   r   <module>   s    


  o  
       \R