U
    >vf                     @   s4  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 d dl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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dgej$)dG dd dZ*G dd dZ+e'de'dG dd dZ,ej$%de - dd  Z.dS )!    )datedatetime	timedelta)partial)BytesION)	DataFrameIndex
MultiIndexoption_context)	ExcelFileExcelWriter_OpenpyxlWriter_XlsxWriterregister_writer)_writersc              	   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-vdrwu74i/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
xlsxwriterz.odsZ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usecolsr,   r   r   r   to_excelpd
read_excelassert_frame_equalselfr   r,   expectedfilenamedfr   resultr   r   r   !test_read_one_empty_col_no_headerG   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   r-   r/   r4   r   r   r   #test_read_one_empty_col_with_headerX   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)r,   r+   ZData_with_headr+   AB)r.   r,   names)r.   	index_colrE   )
r   r   r   r   r0   rA   r   r1   r2   r3   )r5   r   ZrefdfpthwriterreaderZxlsdf_no_headZxlsdf_with_headr   r   r   "test_set_column_names_in_parameterh   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"   r@   )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rP   r   r   
<listcomp>   s     zKTestRoundTrip.test_creating_and_reading_multiple_sheets.<locals>.<listcomp>r   r.   rF   )
dictzipr   r   r   itemsr0   r1   r2   r3   )
r5   r   sheetsdfsrG   ewZ	sheetnamer8   Zdfs_returnedrR   r   rS   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ra   Y)Zeror#   )r^   r`   rc   )re   zUnnamed: 4_level_1r,   rF   ))Begr#   ZMiddler_   Tailrb   rj   rd   ))rg   zUnnamed: 1_level_1rh   ri   rk   r   r   r   r0   r1   r2   r3   )r5   r   r   r8   r6   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   )rF   r,   )check_namesr   )r   r   pytestmarkZxfailnodeZ
add_markermakeCustomDataframer0   r1   r2   listranger3   npnaniloc)r5   r   ro   rp   rq   rr   requestrG   rx   ru   r8   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|}|| tj	|d	d
}t
|| tj	|dgd	d}t
|| dd }tjtdd tj	|dg|d	d}W 5 Q R X t
|| tj	|dgdd	d}t
|| W 5 Q R X d S )Nr$   r&   r"   z
2012-01-01)periods)coldate_stringsr   %m/%d/%Yr   rF   )parse_datesrF   c                 S   s   t | dS )Nr   )r   strptimer_   r   r   r   <lambda>      z;TestRoundTrip.test_read_excel_parse_dates.<locals>.<lambda>zuse 'date_format' insteadmatch)r   date_parserrF   )r   date_formatrF   )r   r1   Z
date_rangecopydtstrftimer   r   r0   r2   r3   Zassert_produces_warningFutureWarning)r5   r   r8   Zdf2rG   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   freqrB   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|   r1   Zinterval_range	Timestampr   r   r   r0   r2   r3   )r5   r   Zmidxr8   rG   r9   r6   r   r   r   "test_multiindex_interval_datetimes  s4      
z0TestRoundTrip.test_multiindex_interval_datetimes)__name__
__module____qualname__rw   rx   parametrizer   r}   r~   r:   r;   rJ   r\   rn   r   r   r   r   r   r   r   r    ;   s(   *
,
/1!r    z
engine,extc                   @   sJ  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 ZCdnS )y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 larger   )r}   zerosr   rw   raises
ValueErrorr0   )	r5   r   Zbreaking_row_countZbreaking_col_countZrow_arrZcol_arrZrow_dfZcol_dfmsgr   r   r   test_excel_sheet_sizeU  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   rU   zWorksheet named '0' not foundr   0)r   r}   randomrandnr0   r   r1   r2   r   r3   rw   r   r   )r5   r   gtxlr8   r   r   r   r   test_excel_sheet_by_name_raisef  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Data1rv   ZData2r   rU   )	r   r0   r   rA   r   r1   r2   r   r3   )r5   framer   rH   frame2rI   Zfound_dfZ	found_df2r   r   r   !test_excel_writer_context_managers  s    

z1TestExcelWriter.test_excel_writer_context_managerc                 C   s  |  }tj|jd d|jdf< ||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 )Nrt   rC   test1rD   r@   Fr,   rB   r   rU   ZNA)Zna_rep)r.   rF   Z	na_valuesZ88X   g      V@Sheet1r   r   )r   r}   r~   r   rA   get_locr0   r1   r2   r   r3   r+   Zto_frame)r5   r   r   reconsrR   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 )Nr=   r<   r   r   rU   )r   r0   r   r1   r2   r   r3   )r5   r   r   Zmixed_framerI   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   rU   )
r1   DatetimeIndexr}   asarrayr+   r0   r   r2   r   r3   )r5   tsframer   r8   r+   rI   r   r   r   r   test_ts_frame  s    
zTestExcelWriter.test_ts_framec                 C   sh   |  }tj|jd d|jdf< ||d |j|dddgd |j|ddd |j|ddd d S )	Nrt   rC   r   rD   r@   Fr   rB   )r   r}   r~   r   rA   r   r0   )r5   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
|| d S )	Nr   )r   r&   sizedtyper   r   rU   )r   r}   r   randintr0   r   r1   r2   astypeint64r   r3   )r5   r   r   r8   rI   r   Z	int_frameZrecons2r   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   rU   )r   r}   r   Zrandom_sampler0   r   r1   r2   r   r   r3   )r5   r   r   r8   rI   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   rU   )
r   r}   Zbool_r0   r   r1   r2   r   r   r3   r5   r   r8   rI   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"   rt   r   r   rU   )	r   r}   infr0   r   r1   r2   r   r3   r   r   r   r   test_inf_roundtrip  s
    
z"TestExcelWriter.test_inf_roundtripc              	   C   s8  t jt|jd d}||_| }tj|jd d|j	df< |
|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s4td S )Nr   rt   rC   r   rD   r@   Fr   rB   Ztest2r   rU   r&   r$   )r1   r   r}   r   r+   r   r~   r   rA   r   r0   r   r   r2   r   r3   lensheet_namesAssertionError)r5   r   r   r   r+   rH   rI   r   r   r   r   test_sheets  s&    

zTestExcelWriter.test_sheetsc              	   C   s   |  }tj|jd d|jdf< ||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 )Nrt   rC   r   rD   r@   Fr   rB   ZAArb   rd   Zr   rU   )r   r}   r~   r   rA   r   r0   r   r   r1   r2   r   r3   )r5   r   r   Zcol_aliasesrI   rsxpr   r   r   test_colaliases  s    
zTestExcelWriter.test_colaliasesc              	   C   s,  |  }tj|jd d|jdf< ||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sRt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 )Nrt   rC   r   rD   r@   Fr   rB   r   r&   r   test)Zindex_labelmerge_cellsrU   dummyZdummy2CD)rA   r+   r   r$   )r   r}   r~   r   rA   r   r0   r   r   r   r   r1   r2   r   r   r+   rE   r   r   r3   bool	set_index)r5   r   r   r   r8   rI   r   r   r   r   test_roundtrip_indexlabels-  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   rU   )r   r}   r   r   r+   namer0   r   r1   r2   r   r   r3   r   )r5   r   r   r8   xfr9   r   r   r   test_excel_roundtrip_indexnamef  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   )rQ   r_   r   r   r   rT   {  s     zATestExcelWriter.test_excel_roundtrip_datetime.<locals>.<listcomp>r   r   r   rU   )r1   r   r}   r   r+   r   r0   r   r2   r   r3   )r5   r   r   r   r+   ZtsfrI   r   r   r   r   test_excel_roundtrip_datetimer  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  rt         rM   r!   r&         ZDATEZDATETIMErb   rd   r+   rA   r   z
DD.MM.YYYYzDD.MM.YYYY HH-MM-SS)r   Zdatetime_formatr   rU   )r   r   r   r   r   r   r0   r   r1   r2   r3   )r5   r   r   r8   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   rU   )r   r}   r   r   r   r   r1   cutr   strr0   r   r2   r   r3   r5   r   r8   r6   rI   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   rC   rD   r   r   EFGHIJ)labelsr   r   rU   )r   r}   r   r   r   r   r1   r   ZSeriesr{   r0   r   r2   r   r3   )r5   r   r8   r6   Z	intervalsrI   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   rC   )rA   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   rU   )r   r}   r   r   r   r   applyr0   r   r1   r2   r   r3   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   rU   )	ZresampleZmeanr0   r   r1   r2   r   r3   Z	to_period)r5   r   r   r   rI   r   r   r   r   test_to_excel_periodindex  s
    
z)TestExcelWriter.test_to_excel_periodindexc              	   C   s   t jt|jd 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&   r   rv   firstsecondrE   r   Fr   rC   rD   r@   r   r   r$   rU   )r}   aranger   r+   r   reshaper	   r   r0   r   r1   r2   r   r3   )r5   r   r   r   arrays	new_indexrI   r8   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      rC   rD   r   rC   rD   r   r   r$   r   )
r   r}   r   sampler   r0   r1   r2   r   r3   )r5   r   r   r8   Zdf1r   r   r   "test_to_excel_multiindex_nan_label  s
    $z2TestExcelWriter.test_to_excel_multiindex_nan_labelc              	   C   s   t jt|jd t jddd}tj|ddgd}||_tddd	d
g}||_	ddg}|sbd}|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&   r   rv   r  r  r  )(   r$   )r  r&   )2   r$   )r  r&   r   r$   r   r   r.   r,   rF   F)ZsparsifyZadjoinrE   c                 S   s   g | ]}d  tt|qS )r   )joinmapr   )rQ   qr   r   r   rT     s     zATestExcelWriter.test_to_excel_multiindex_cols.<locals>.<listcomp>)r}   r  r   r+   r   r  r	   r   from_tuplesrA   r0   r   r1   r2   formatrW   r   r3   )r5   r   r   r   r  r	  Znew_cols_indexr,   rI   r8   Zfmr   r   r   test_to_excel_multiindex_cols  s(    "
   z-TestExcelWriter.test_to_excel_multiindex_colsc              	   C   s   |j tjt|j tjd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 )
Nr   timer<   r   r   r   r$   rU   )r  r<   )r+   r}   r  r   r   r	   r   rE   r0   r   r1   r2   r   r3   r   )r5   r   r   r   r	  rI   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  <   )r>   r?   c)F   P   )Z   r%   r   FrB   r.   )r   r   r	   r  r+   r0   r   r1   r2   r   r3   )r5   r   Zframe1r   Zmulti_indexrI   Zframe3r   r   r   'test_to_excel_multiindex_no_write_index(  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&   r@   r  r   r   r  F)Zcheck_index_typeZcheck_dtype)	r   r	   r  r0   r   r1   r2   r   r3   )r5   r   r6   r8   rI   r9   r   r   r   test_to_excel_empty_multiindex=  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@̤ArC   rD   rb   rd   r   r   r   %.2fZfloat_formatr   rU   Q?q=
ףp?=
ףp=?p=
ף(@)r   r0   r   r1   r2   r   r3   )r5   r   r8   rI   r9   r6   r   r   r   test_to_excel_float_formatJ  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ƒrD   u   XƓrd   r   r   z__tmp_to_excel_float_format__.Z	TestSheetr  r   rU   r/   )r5   r   r8   r7   r9   r   r   r   test_to_excel_output_encoding\  s    z-TestExcelWriter.test_to_excel_output_encodingc              
   C   s   t d| }zt|d W 5 Q R X W n tk
rH   td Y n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'  rC   rD   rb   rd   r   r   r   r(  r)  r   rU   r*  r+  r,  r-  )r   r   openUnicodeEncodeErrorrw   skipr   r0   r   r1   r2   r3   )r5   r   r7   r8   rI   r9   r6   r   r   r   test_to_excel_unicode_filenamei  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)r,   r   r+   r   r.   r,   )r0   r   r1   r2   r   )datar,   Z
parser_hdrr+   r   r   r   r   r   	roundtrip  s    
  z;TestExcelWriter.test_excel_010_hemstring.<locals>.roundtrip)r$   r&   )r   r   rt   r"   )r6  r7  r$   z_Writing to Excel with MultiIndex columns and no index \('index'=False\) is not yet implemented.r   FrB   )Tr   T)r   r   r   r   r}   r~   r   rz   rw   r   NotImplementedErrorr|   r   r+   rA   )r5   r   r7  r6  r5  r   r;  Zparser_headerr   ZnrowsZncolsr8   r   rr  r   r:  r   test_excel_010_hemstring  s2    	   
z(TestExcelWriter.test_excel_010_hemstringc                 C   sB  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 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 S )Nr$   r&   r"   rC   rD   r@   r   zB.1r   rU   r!   rt      r]      A.1Fr*   r8  r   r0   r1   r2   r   r3   r5   r   r8   r6   r9   r   r   r   test_duplicated_columns  s(    * ( 
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&   rC   rD   r   rD   rC   r@   r   r8  )r   r0   r1   r2   r   assert_series_equal)r5   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&   rE  zNot all names specifiedr   r   rD   r   r@   z.'passes columns are not ALL present dataframe'r   )r   rw   r   KeyErrorr0   )r5   r   rG  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_bugrC   rD   rA   r+   rU   rB  )r5   r   Zto_excel_indexZread_excel_index_colrG  r6   rH  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#tworE  test_cr   rU   )r$   r   r$   r$   )r&   r$   #r.   commentrF   )r   r0   r1   r2   r   r   r3   r5   r   r8   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 )	NrN  rO  rP  rQ  rE  rR  r  )r.   rV  rB  rW  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 )
NrN  rO  rP  rQ  rE  rR  rT  r   rU  rB  rC  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)r>   r?   FrB   r$   r&   rT  )rV  rB  rC  r   r   r   test_comment_empty_line/  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   r?  *   r]   9   #   r   )   r   rK            5      %   r      r   4   rC   r   r8  )r   r   r0   r1   r2   r   rF  )r5   r   Z	datetimesrG  rH  r   r   r   test_datetimes;  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   r0   seekr1   r2   r   r3   )r5   r   Zbior8   rH   Z	reread_dfr   r   r   test_bytes_ioQ  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 )Nr>   r?   r  er&   )rN   fr$   g      @ZappleZbananaZcherry)mixednumericr   r   r   r  r   )r   r0   r1   r2   r   ru  r   r   rv  r   r   r3   r5   r   r8   readr6   r   r   r   test_write_lists_dict^  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!   )renderr9  r   r   r   rB  rw  r   r   r   test_render_as_column_namep  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<   r=   col1col2r@   TF)r<   r=   r   )Ztrue_valuesZfalse_valuesrF   )r   replacer0   r1   r2   r   r3   )r5   r   r8   r6   rH  r   r   r   !test_true_and_false_value_optionsx  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}  r@   r   rS  )Zfreeze_panesr   r   rB  )r5   r   r6   r9   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 Nrp  r   r   r<   )r   )r   makeDataFramer   r0   r1   r2   Zround_trip_pathlibr3   r5   r   r   r8   rH   rI   r9   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   r0   r1   r2   Zround_trip_localpathr3   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jddd|d}|| tj|dd	gdd
}|j	j
dd |jd D dd |jd	 D gdd	gd|_	t|| d S )NZ2018Z2018Q1Z2018Q2)r&   r&   r   r   r@   r   r$   rf   c                 S   s   g | ]}t |qS r   )r   )rQ   rO   r   r   r   rT     s     zCTestExcelWriter.test_merged_cell_custom_objects.<locals>.<listcomp>)level)r	   r  r1   ZPeriodr   r}   Zonesr0   r2   rA   Z
set_levelslevelsr   r3   )r5   r   mir6   r9   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   )r1   r   r   rw   r   r   r0   Zto_pydatetime)r5   r   Ztz_aware_fixturer   r  r9  r8   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   rK   rE  rC   rD   FrL  rA  r@   rB  )r5   r   r8   r9   r6   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   rw   r   r   reescaper   )r5   r   r   rt  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 Nrp  )r   r   r   r   r0   r1   r2   r3   )r5   r   r   r   rH   r9   r6   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  rl   )r5   r   r   r   r9   r6   r   r   r   test_to_excel_empty_frame  s
    
z)TestExcelWriter.test_to_excel_empty_frame)Dr   r   r   r   r   r   r   r   r   r   rw   rx   r   r}   Zint8Zint16Zint32r   r   Zfloat16Zfloat32Zfloat64r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r   r!  r.  r/  r4  r>  rD  rI  rK  rM  rX  rY  rZ  r^  ro  rr  ry  r{  r  r  r  r  r  objectr  r  r  r  r  r   r   r   r   r   >  s   1	

	9,	4


		r   c                
   @   sZ   e Zd Zej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dS )TestExcelWriterEngineTestsz	klass,extr   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   )r5   klassr   r   rH   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)rw   r   r   r   r5   r   r   r    test_ExcelWriter_dispatch_raises  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   book  s    zHTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass.bookc                 S   s   dt | _d S NT)typecalled_saver  r   r   r   _save  s    zITestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass._savec                 _   s   dt | _d S r  )r  called_write_cells)r5   argskwargsr   r   r   _write_cells  s    zPTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass._write_cellsc                 S   s   dt | _d S r  )r  called_sheetsr  r   r   r   rY     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_reset  s
    


z[TestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass.assert_called_and_resetN)r   r   r   r  r  r  Z_supported_extensionsZ_enginer  r  r  propertyrY   classmethodr  r   r   r   r   
DummyClass  s   
r  zio.excel.xlsx.writerr   zsomething.xlsxr$   zsomething.xlsrp  )
r   r   r
   r   r   r  r   rz   r0   r  )r5   r  r   filepathrH   r8   r   r   r   test_register_writer  s    
z/TestExcelWriterEngineTests.test_register_writerN)r   r   r   rw   rx   r   paramr   r  
skip_if_nor   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.xlsxrC   r$   r&   )r   r   r   r0   r   osfspathr   )r5   r   r8   r   r9   r   r   r   test_excelfile_fspath#  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   )r5   r   rH   r   r   r   test_excelwriter_fspath+  s    
z"TestFSPath.test_excelwriter_fspathN)r   r   r   r  r  r   r   r   r   r     s   r  r  c                 C   s6   dd t tD }dd t | D }||r2td S )Nc                 S   s   h | ]}| d s|qS _
startswithrQ   r   r   r   r   	<setcomp>4  s     
 z%test_subclass_attr.<locals>.<setcomp>c                 S   s   h | ]}| d s|qS r  r  r  r   r   r   r  5  s     
 )dirr   symmetric_differencer   )r  Z
attrs_baseZattrs_klassr   r   r   test_subclass_attr1  s    r  )/r   r   r   	functoolsr   ior   r  r  Znumpyr}   rw   Zpandas.util._test_decoratorsutilZ_test_decoratorsr  Zpandasr1   r   r   r	   r
   Zpandas._testingZ_testingr   Zpandas.io.excelr   r   r   r   r   Zpandas.io.excel._utilr   Zfixturer   r   rx   r   r  r  r    Zusefixturesr   r  r  valuesr  r   r   r   r   <module>   s   

  y
       B