U
    Mf                     @   sP  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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
get_option
set_option)	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-eb6vo0j3/pandas/tests/io/excel/test_writers.pypath$   s    r   c                 c   s8   d| d d}t|}t||  dV  t|| 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
   r   )enginer   Zoption_nameZprev_enginer   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usecolsr1   r   r   r   to_excelpd
read_excelassert_frame_equalselfr   r1   expectedfilenamedfr   resultr   r   r   !test_read_one_empty_col_no_headerK   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   r2   r4   r9   r   r   r   #test_read_one_empty_col_with_header\   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)r1   r0   ZData_with_headr0   AB)r3   r1   names)r3   	index_colrJ   )
r   r   r   r   r5   rF   r   r6   r7   r8   )r:   r   ZrefdfpthwriterreaderZxlsdf_no_headZxlsdf_with_headr   r   r   "test_set_column_names_in_parameterl   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'   rE   )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rU   r   r   
<listcomp>   s     zKTestRoundTrip.test_creating_and_reading_multiple_sheets.<locals>.<listcomp>r   r3   rK   )
dictzipr   r   r   itemsr5   r6   r7   r8   )
r:   r   ZsheetsdfsrL   ewZ	sheetnamer=   Zdfs_returnedrW   r   rX   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)r1   rK   ))Begr(   ZMiddlerc   Tailrf   rm   rh   ))rj   zUnnamed: 1_level_1rk   rl   rn   )r   r   r   r5   r6   r7   r8   )r:   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   )rK   r1   )check_namesr   )r   r   pytestmarkZxfailnodeZ
add_markermakeCustomDataframer5   r6   r7   listranger8   npnaniloc)r:   r   rq   rr   rs   rt   requestrL   rz   rw   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   rK   )parse_datesrK   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_parserrK   )r   r6   Z
date_rangecopydtstrftimer   r   r5   r7   r8   )r:   r   r=   Zdf2rL   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   freqrG   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~   r6   Zinterval_range	Timestampr   r   r   r5   r7   r8   )r:   r   Zmidxr=   rL   r>   r;   r   r   r   "test_multiindex_interval_datetimes  s4      
z0TestRoundTrip.test_multiindex_interval_datetimes)__name__
__module____qualname__ry   rz   parametrizer   r   r   r?   r@   rO   r`   rp   r   r   r   r   r   r   r   r%   >   s(   *
,
/1r%   z
engine,extc                   @   sH  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e
jd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&d0d1 Z'd2d3 Z(d4d5 Z)d6d7 Z*d8d9 Z+d:d; Z,e
jd<d=d>ge
jd?d@dAdBge
jdCd@dAdBgdDdE Z-dFdG Z.dHdI Z/dJdK Z0e
jdLdMdNgdOdP Z1dQdR Z2dSdT Z3dUdV Z4dWdX Z5dYdZ Z6d[d\ Z7d]d^ Z8d_d` Z9dadb Z:dcdd Z;dedf Z<dgdh Z=didj Z>e
jdkdle?gdmdn Z@dodp ZAdqdr ZBdlS )s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   ry   raises
ValueErrorr5   )	r:   r   Zbreaking_row_countZbreaking_col_countZrow_arrZcol_arrZrow_dfZcol_dfmsgr   r   r   test_excel_sheet_sizeR  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   rZ   zWorksheet named '0' not foundr   0)r   r   randomrandnr5   r   r6   r7   r   r8   ry   r   r   )r:   r   r   gtxlr=   r   r   r   r   test_excel_sheet_by_name_raised  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Data1rx   ZData2r   rZ   )	r   r5   r   rF   r   r6   r7   r   r8   )r:   framer   rM   frame2rN   Zfound_dfZ	found_df2r   r   r   !test_excel_writer_context_managerq  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 )NrH   rv   test1rI   rE   Fr1   rG   r   rZ   ZNA)Zna_rep)r3   rK   Z	na_valuesZ88X   g      V@Sheet1r   r   )
r   r   r   r5   r6   r7   r   r8   r0   Zto_frame)r:   r   r   reconsrW   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 )NrB   rA   r   r   rZ   )r   r5   r   r6   r7   r   r8   )r:   r   r   Zmixed_framerN   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   rZ   )
r6   DatetimeIndexr   asarrayr0   r5   r   r7   r   r8   )r:   tsframer   r=   r0   rN   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 )	NrH   rv   r   rI   rE   Fr   rG   )r   r   r   r5   )r:   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   rZ   convert_float is deprecatedr   F)r3   convert_floatrK   )r   r   r   randintr5   r   r6   r7   astypeint64r   r8   floatrF   r0   assert_produces_warningFutureWarning)	r:   r   r   r=   rN   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   rZ   )r   r   r   Zrandom_sampler5   r   r6   r7   r   r   r8   r:   r   r   r=   rN   r   r   r   r   test_float_types  s    
z TestExcelWriter.test_float_typesc              	   C   sZ   t ddddg|d}||d t|}tj|ddd|}W 5 Q R X t|| d S )Nr)   r   TFr   r   rZ   )r   r5   r   r6   r7   r   r   r8   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'   rv   r   r   rZ   )	r   r   infr5   r   r6   r7   r   r8   )r:   r   r=   rN   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   rH   rv   r   rI   rE   Fr   rG   Ztest2r   rZ   r+   r)   )r6   r   r   r   r0   r   r   r5   r   r   r7   r   r8   lensheet_namesAssertionError)r:   r   r   r   r0   rM   rN   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 )NrH   rv   r   rI   rE   Fr   rG   ZAArf   rh   Zr   rZ   )r   r   r   r5   r   r   r6   r7   rF   r   r8   )r:   r   r   Zcol_aliasesrN   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 )NrH   rv   r   rI   rE   Fr   rG   r   r+   r   test)Zindex_labelmerge_cellsrZ   dummyZdummy2CD)rF   r0   r   r)   )r   r   r   r5   r   r   r   r   r6   r7   r   r   r0   rJ   r   r   r8   bool	set_index)r:   r   r   r   r=   rN   r   r   r   r   test_roundtrip_indexlabels9  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&   rA   r   r   rZ   )r   r   r   r   r0   namer5   r   r6   r7   r   r   r8   r   )r:   r   r   r=   xfr>   r   r   r   test_excel_roundtrip_indexnamer  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   )rV   rc   r   r   r   rY     s     zATestExcelWriter.test_excel_roundtrip_datetime.<locals>.<listcomp>r   r   r   rZ   )r6   r   r   r   r0   r   r5   r   r7   r   r8   )r:   r   r   r   r0   ZtsfrN   r   r   r   r   test_excel_roundtrip_datetime~  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  rv         rR   r&   r+         ZDATEZDATETIMErf   rh   r0   rF   r   z
DD.MM.YYYYzDD.MM.YYYY HH-MM-SS)Zdate_formatZdatetime_formatr   rZ   )r   r   r   r   r   r   r5   r   r6   r7   r8   )r:   r   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   rZ   )r   r   r   r   r   r   r6   cutr   strr5   r   r7   r   r8   r:   r   r=   r;   rN   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   rH   rI   r   r   EFGHIJ)labelsr   r   rZ   )r   r   r   r   r   r   r6   r   ZSeriesr}   r5   r   r7   r   r8   )r:   r   r=   r;   Z	intervalsrN   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   rH   )rF   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   rZ   )r   r   r   r   r   r   applyr5   r   r6   r7   r   r8   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   rZ   )	ZresampleZmeanr5   r   r6   r7   r   r8   Z	to_period)r:   r   r   r   rN   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+   rx   firstsecondrJ   r   Fr   rH   rI   rE   r   r   r)   rZ   )r   aranger   r0   reshaper	   r   r5   r   r6   r7   r   r8   )r:   r   r   r   arrays	new_indexrN   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      rH   rI   r   rH   rI   r   r   r)   r   )
r   r   r   sampler   r5   r6   r7   r   r8   )r:   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+   rx   r  r  r	  )(   r)   )r  r+   )2   r)   )r  r+   r   r)   r   r   r3   r1   rK   F)ZsparsifyZadjoinrJ   c                 S   s   g | ]}d  tt|qS )r   )joinmapr   )rV   qr   r   r   rY   $  s     zATestExcelWriter.test_to_excel_multiindex_cols.<locals>.<listcomp>)r   r
  r   r0   r  r	   r   from_tuplesrF   r5   r   r6   r7   formatr\   r   r8   )r:   r   r   r   r  r  Znew_cols_indexr1   rN   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timerA   r   r   r   r)   rZ   )r  rA   )r0   r   r
  r   r	   r   rJ   r5   r   r6   r7   r   r8   r   )r:   r   r   r   r  rN   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  <   )rC   rD   c)F   P   )Z   r*   r   FrG   r3   )r   r   r	   r  r0   r5   r   r6   r7   r   r8   )r:   r   Zframe1r   Zmulti_indexrN   Zframe3r   r   r   'test_to_excel_multiindex_no_write_index4  s    
z7TestExcelWriter.test_to_excel_multiindex_no_write_indexc              	   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@̤ArH   rI   rf   rh   r   r   r   %.2fZfloat_formatr   rZ   Q?q=
ףp?=
ףp=?p=
ף(@)r   r5   r   r6   r7   r   r8   )r:   r   r=   rN   r>   r;   r   r   r   test_to_excel_float_formatI  s    
z*TestExcelWriter.test_to_excel_float_formatc              	   C   sr   t dddgdddggddgd	d
dgd}td| 2}|j|ddd tj|ddd}t|| W 5 Q R X d S )Nu   ƒu   Ɠu   Ɣu   ƕu   Ɩu   Ɨu   AƒrI   u   XƓrh   r   r   z__tmp_to_excel_float_format__.Z	TestSheetutf8)r3   encodingr   rZ   r4   )r:   r   r=   r<   r>   r   r   r   test_to_excel_output_encoding[  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*  rH   rI   rf   rh   r   r   r   r+  r,  r   rZ   r-  r.  r/  r0  )r   r   closeopenUnicodeEncodeErrorry   skipr   r5   r   r6   r7   r8   )	r:   r   r   r<   fr=   rN   r>   r;   r   r   r   test_to_excel_unicode_filenameh  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)r1   r   r0   r   r3   r1   )r5   r   r6   r7   r   )datar1   Z
parser_hdrr0   r   r   r   r   r   	roundtrip  s    
  z;TestExcelWriter.test_excel_010_hemstring.<locals>.roundtrip)r)   r+   )r   r   rv   r'   )r=  r>  r)   z_Writing to Excel with MultiIndex columns and no index \('index'=False\) is not yet implemented.r   FrG   )Tr   T)r   r   r   r   r   r   r   r|   ry   r   NotImplementedErrorr~   r   r0   rF   )r:   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|d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}tjt|d tj|dd dd W 5 Q R X d S )Nr)   r+   r'   rH   rI   rE   r   zB.1r   rZ   T)r3   rK   mangle_dupe_colsr&   rv      ra      A.1Fr/   r?  z3Setting mangle_dupe_cols=False is not supported yetr   )r3   r1   rF  )	r   r5   r6   r7   r   r8   ry   r   r   )r:   r   r=   r;   r>   r   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+   rH   rI   r   rI   rH   rE   r   r?  )r   r5   r6   r7   r   assert_series_equal)r:   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   rI   r   rE   z.'passes columns are not ALL present dataframe'r   )r   ry   r   KeyErrorr5   )r:   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_bugrH   rI   rF   r0   rZ   r   r5   r6   r7   r   r8   )r:   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   rZ   )r)   r   r)   r)   )r+   r)   #r3   commentrK   )r   r5   r6   r7   r   r   r8   r:   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#  )r3   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  r:   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)rC   rD   FrG   r)   r+   r[  )r]  rS  ra  r   r   r   test_comment_empty_line9  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 |d s|d!rtd" tj|ddd#}t	|d |d  d S )$Ni  r)   r   r+   r'   -   8   r&      1   rG  *   ra   9   #   r   )   r   rP            5      %   r      r   4   rH   r   xlsxZxlsmz}Defaults to openpyxl and fails with floating point error on datetimes; may be fixed on newer versions of openpyxl - GH #38644r?  )
r   r   r5   endswithry   r9  r6   r7   r   rL  )r:   r   Z	datetimesrM  rN  r   r   r   test_datetimesE  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   r5   seekr6   r7   r   r8   )r:   r   Zbior=   rM   Z	reread_dfr   r   r   test_bytes_ioa  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 )NrC   rD   r  er+   )rS   r:  r)   g      @ZappleZbananaZcherry)mixednumericr   r   r   r  r   )r   r5   r6   r7   r   r~  r  r   r  r   r   r8   r:   r   r=   readr;   r   r   r   test_write_lists_dictn  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 )NrA   rB   col1col2rE   TF)rA   rB   r   )Ztrue_valuesZfalse_valuesrK   )r   replacer5   r6   r7   r   r8   )r:   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  rE   r   rZ  )Zfreeze_panesr   r   rS  )r:   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 Nrz  r   r   rA   )r   )r   makeDataFramer   r5   r6   r7   Zround_trip_pathlibr8   r:   r   r   r=   rM   rN   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   r5   r6   r7   Zround_trip_localpathr8   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+   rE   r   r   r   r)   F)r1   rK   r   c                 S   s   g | ]}t |qS r   )r   )rV   rT   r   r   r   rY     s     zCTestExcelWriter.test_merged_cell_custom_objects.<locals>.<listcomp>)level)r	   r  r6   ZPeriodr   r   Zonesr5   r   r   r   r7   rF   Z
set_levelslevelsr0   r   float64r8   )r:   r   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   )r6   r   r   ry   r   r   r5   Zto_pydatetime)r:   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   rP   rK  rH   rI   FrR  rI  rE   rS  )r:   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   ry   r   r   reescaper   )r:   r   r   r:  r   r   r   test_if_sheet_exists_raises  s    z+TestExcelWriter.test_if_sheet_exists_raises)Cr   r   r   r   r   r   r   r   r   r   ry   rz   r   r   Zint8Zint16Zint32r   r   Zfloat16Zfloat32r  r   Zbool8Zbool_r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r$  r1  r4  r;  rE  rJ  rO  rQ  rT  r_  r`  rb  rf  ry  r|  r  r  r  r  r  r  r  objectr  r  r  r   r   r   r   r   8  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   )r:   klassr   r   rM   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)ry   r   r   r   r:   r   r   r    test_ExcelWriter_dispatch_raises  s    z;TestExcelWriterEngineTests.test_ExcelWriter_dispatch_raisesc                    s   g  g G  fdddt } fdd}tdd d}t|Nt| t }t||sjtW 5 Q R X tdd|fd	d
 W 5 Q R X td|fdd
 W 5 Q R X W 5 Q R X d S )Nc                       s8   e Zd ZdZdZddgZdZ fddZfddZd	S )
zCTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClassFrw  Zxlsr   c                    s     d d S NTappendr  )called_saver   r   save  s    zHTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass.savec                    s     d d S r  r  )r:   argskwargs)called_write_cellsr   r   write_cells  s    zOTestExcelWriterEngineTests.test_register_writer.<locals>.DummyClass.write_cellsN)	r   r   r   r  r  Zsupported_extensionsr   r  r  r   r  r  r   r   
DummyClass  s   r  c                    s>   |   t  dkstt dks&t d d = d d = d S )Nr)   )r   r   )funcr  r   r   check_called
  s
    
zETestExcelWriterEngineTests.test_register_writer.<locals>.check_calledzio.excel.xlsx.writerr   zsomething.xlsxr)   c                      s
     S )Nr5   r   r=   filepathr   r   r     r   zATestExcelWriterEngineTests.test_register_writer.<locals>.<lambda>zsomething.xlsc                      s    j ddS )Nr   rz  r  r   r  r   r   r     r   )	r   r6   Zoption_contextr   r   r   r  r   r|   )r:   r  r  r   rM   r   )r  r  r=   r  r   test_register_writer  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(   rC   r)   r+   )Zengine_kwargsrD   )r  r  ry   r   r   r   )r:   r   r   r   r   r   $test_engine_kwargs_and_kwargs_raises  s    

z?TestExcelWriterEngineTests.test_engine_kwargs_and_kwargs_raisesN)r   r   r   ry   rz   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.xlsxrH   r)   r+   )r   r   r   r5   r   osfspathr   )r:   r   r=   r   r>   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   )r:   r   rM   r   r   r   test_excelwriter_fspath7  s    
z"TestFSPath.test_excelwriter_fspathN)r   r   r   r  r  r   r   r   r   r  ,  s   r  )-r   r   r   	functoolsr   ior   r  r  Znumpyr   ry   Zpandas.util._test_decoratorsutilZ_test_decoratorsr  Zpandasr6   r   r   r	   r
   r   Zpandas._testingZ_testingr   Zpandas.io.excelr   r   r   r   r   r   Zfixturer   r   rz   r   r  r  r%   Zusefixturesr   r  r  r   r   r   r   <module>   s    


  o  
       I