3
Ud1                @   s  d dl Z d dlZd dlmZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Zd dlZd dlmZ d dlZd dljZd dlmZmZ d dlmZ d dlmZ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d Z$G dd dZ%ej&j'ddddddgdd Z(ej&j'ddddddgej&j'dddgej&j'dddgdd Z)ej&j'd d!dgej&j'd"dd#d!gd$d% Z*ej&j'ddddddgd&d' Z+d(d) Z,d*d+ Z-d,d- Z.d.d/ Z/dS )0    N)datetime)is_categorical_dtype)	DataFrameSeries)read_csv)CategoricalConversionWarningInvalidColumnNamePossiblePrecisionLossStataMissingValueStataReaderStataWriterUTF8
read_statac               C   s*   t jddddgddddgd	d
ddgdS )N            g      ?g      @g      ;@g     @T@ZAtlantaZ
BirminghamZ
CincinnatiZDetroit)abc)pdr    r   r   N/var/www/html/virt/lib64/python3.6/site-packages/pandas/tests/io/test_stata.pymixed_frame!   s    

r   c             C   s   | dddS )Niodatastatar   )datapathr   r   r   dirpath,   s    r   c             C   s&   t jj| d}t|dd}d|j_|S )Nzstata5_114.dtaT)convert_datesindex)ospathjoinr   r   name)r   	dta14_114
parsed_114r   r   r   r%   1   s    r%   c               @   s  e Zd Zejdddd Zdd Zdd Zejj	d	d
ddddgdd Z
ejj	dddgdd Zdd Zejj	dddddgdd Zejj	dddd d!gd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zejj	d	d
ddddgd,d- Zd.d/ Zd0d1 Zejj	d	d
ddddgd2d3 Zd4d5 Zejj	d	d
ddddgd6d7 Zd8d9 Zejj	d	d
ddddgejj	dd:d;d<d=gd>d? Zejj	dd@dAdBdCgdDdE Zejj	d	d
ddddgdFdG Zejj	d	d
ddddgdHdI ZdJdK Zejj	d	d
ddddgdLdM ZdNdO Z dPdQ Z!dRdS Z"dTdU Z#dVdW Z$dXdY Z%dZd[ Z&d\d] Z'ejj	d	d
ddddgejj	d^d_d`gdadb Z(dcdd Z)dedf Z*dgdh Z+didj Z,ejj	ddkdldmgdndo Z-dpdq Z.drds Z/dtdu Z0ejj	d	d
ddddgejj1dvdwdx Z2dydz Z3ejj	d	d
ddddgd{d| Z4ejj	dd}d~gdd Z5ejj	dddgdd Z6ejj	dd}d~gdd Z7ejj	ddddd!d=dCddmdd~dgejj	dddgejj	dddgejj	dddgdd Z8e9e:e:dddZ;dd Z<ejj	dddd d<dBddldd}dg
ejj	dddgejj	dddgejj	dddgdd Z=dd Z>ejj	d	d
ddddgdd Z?ejj	d	d
ddddgdd Z@ejj	d	d
dgdd ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKejj	dddgdd ZLdd ZMejj	ddddddddgddƄ ZNddȄ ZOddʄ ZPdd̄ ZQejj	d	d
ddddgdd΄ ZRddЄ ZSdd҄ ZTddԄ ZUejj	d	d
ddddgddք ZVejj	d	d
ddddgdd؄ ZWddڄ ZXdd܄ ZYejjZddބ Z[ejj	d	dddgdd Z\dd Z]dS )	TestStataT)Zautousec             C   s<  |ddd| _ tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d	| _	tjj| j d
| _
tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d| _tjj| j d | _ tjj| j d!| _!tjj| j d"| _"tjj| j d#| _#tjj| j d$| _$tjj| j d%| _%tjj| j d&| _&tjj| j d'| _'tjj| j d(| _(tjj| j d)| _)tjj| j d*| _*tjj| j d+| _+tjj| j d,| _,tjj| j d-| _-tjj| j d.| _.tjj| j d/| _/tjj| j d0| _0d S )1Nr   r   r   zstata1_114.dtazstata1_117.dtazstata2_113.dtazstata2_114.dtazstata2_115.dtazstata2_117.dtazstata3_113.dtazstata3_114.dtazstata3_115.dtazstata3_117.dtaz
stata3.csvzstata4_113.dtazstata4_114.dtazstata4_115.dtazstata4_117.dtazstata1_encoding.dtazstata1_encoding_118.dtaz
stata5.csvzstata5_113.dtazstata5_114.dtazstata5_115.dtazstata5_117.dtaz
stata6.csvzstata6_113.dtazstata6_114.dtazstata6_115.dtazstata6_117.dtazstata7_115.dtazstata7_117.dtazstata8_113.dtazstata8_115.dtazstata8_117.dtazstata9_115.dtazstata9_117.dtazstata10_115.dtazstata10_117.dtazstata11_115.dtazstata11_117.dtazstata12_117.dtazstata14_118.dtazstata15.dtazstata7_111.dtazstata16_118.dtazstata1_119.dta.gzzstata13_dates.dta)1r   r    r!   r"   dta1_114dta1_117Zdta2_113dta2_114dta2_115dta2_117dta3_113dta3_114dta3_115dta3_117csv3dta4_113dta4_114dta4_115dta4_117dta_encodingdta_encoding_118csv14	dta14_113r$   	dta14_115	dta14_117csv15	dta15_113	dta15_114	dta15_115	dta15_117	dta16_115	dta16_117	dta17_113	dta17_115	dta17_117	dta18_115	dta18_117	dta19_115	dta19_117	dta20_115	dta20_117	dta21_117	dta22_118dta23	dta24_111	dta25_118	dta26_119stata_dates)selfr   r   r   r   setup_method:   s\    zTestStata.setup_methodc             C   s   t |ddS )NT)r   )r   )rR   filer   r   r   read_dta{   s    zTestStata.read_dtac             C   s   t |ddS )NT)Zparse_dates)r   )rR   rT   r   r   r   r      s    zTestStata.read_csvversionr   u   v   w   Nc             C   sH   t dgd}tj *}|j|d|d t|}tj|| W d Q R X d S )Nunit)columnsF)write_indexrV   )r   tmensure_cleanto_statar   assert_frame_equal)rR   rV   Zempty_dsr!   Z	empty_ds2r   r   r   test_read_empty_dta   s
    
zTestStata.test_read_empty_dtarT   r'   r(   c             C   sd   t | |}| j|}ttjtjtjtjtjfgdddddgd}|d jtj|d< tj|| d S )N
float_missdouble_miss	byte_missint_miss	long_miss)r\   )	getattrrU   r   npnanastypefloat32r^   ra   )rR   rT   parsedexpectedr   r   r   test_read_dta1   s    

zTestStata.test_read_dta1c             C   s  t jtdddddddtdd	dtdd	d
tdd	d	tddd	tdd	d	tdd	d	ftddddddd(tdddtdddtdd	d	tddd	tdd	d	tdd	d	ftjtjtjtjtjtjtjtjfgddddddddgd }|d jd!|d< tjd"d#T}tjd$ | j	| j
}| j	| j}| j	| j}d%d& |D }t|dksNtW d Q R X tj||d"d' tj||d"d' tj||d"d' d S ))Ni                 l   *}ai  r      i     i        r   i i  
   r   i     i  Z
datetime_cZdatetime_big_cdateZweekly_dateZmonthly_dateZquarterly_dateZhalf_yearly_dateZyearly_date)r\   OT)recordalwaysc             S   s   g | ]}|j tkr|qS r   )categoryUserWarning).0xr   r   r   
<listcomp>   s    z,TestStata.test_read_dta2.<locals>.<listcomp>)check_datetimelike_compatiZl)r   from_recordsr   r   NaTrk   warningscatch_warningssimplefilterrU   r)   r*   r+   lenAssertionErrorr^   ra   )rR   rn   wr%   
parsed_115
parsed_117r   r   r   test_read_dta2   sJ    









$

zTestStata.test_read_dta2r,   r-   r.   r/   c             C   sd   t | |}| j|}| j| j}|jtj}|d jtj|d< |d jtj|d< t	j
|| d S )Nyearquarter)rh   rU   r   r0   rk   ri   rl   int16int8r^   ra   )rR   rT   rm   rn   r   r   r   test_read_dta3   s    

zTestStata.test_read_dta3r1   r2   r3   r4   c             C   s.  t | |}| j|}tjdddddgdddddgdddddgddd	ddgd
ddtjd
gdd
dtjdgdddtjdgdddtjdgdddtjdgdddtjdgg
dddddgd}xl|D ]d}|| j }tj|d |j  }|dkr|}|j	dj
}|j|dd}|jjd dd |||< qW tj|| d S )Nonetentwoninethreeeightfoursevenr   fivesix   rz   rv   ru   	   fully_labeledfully_labeled2incompletely_labeledlabeled_with_missingsfloat_labelled)r\   r   T)ordered)inplace)rh   rU   r   r   ri   rj   copyZasarrayZnotnark   _valuesset_categories
categoriesrenamer^   ra   )rR   rT   rm   rn   colorigr   catr   r   r   test_read_dta4   s8    



zTestStata.test_read_dta4c             C   sL   | j | j}tjdddgdddgddd	ggd
ddgd}tj||dd d S )Nr   abcZ	abcdefghir   ZcbaZqwertywertyqwerty]    Zstrlr   yz)r\   F)check_dtype)rU   rK   r   r   r^   ra   )rR   r   rn   r   r   r   test_read_dta12  s    
zTestStata.test_read_dta12c             C   s  | j | j}|d jd|d< tjdddddddgd	d
dtjtjtjtjgdddddddgdddd*dddgddddddd+ggdddddddgd }|d jtj|d< x"|jD ]}t	j
|| ||  qW t| j>}|j }d!d"d#d$d%d&d'd(}t	j|| |jd)kstW d Q R X d S ),NBytesr|   ZCatZBogotau   Bogotár   g      ?u   option b ÜnicodeZDogZBostonu   UzunköprüZPlaneZRomeu   Tromsør   g        zoption aZPotatoZTokyou	   Elâzığr   g      @r   gT?g      @ThingsCitiesUnicode_Cities_StrlIntsFloatsLongs)r\   u'   Here are some strls with Ünicode charsz	long datazHere are some thingsz	byte datazint datazHere are some citiesz
float data)r   r   r   r   r   r   r   u   This is a  Ünicode data labelgUUUUUU?)rU   rL   rk   r   r   ri   rj   rl   r\   r^   Zassert_almost_equalr   variable_labelsZassert_dict_equal
data_labelr   )rR   Z
parsed_118rn   r   rdrZvlZvl_expectedr   r   r   test_read_dta18#  s:    
zTestStata.test_read_dta18c          
   C   st   t tjtjtjtjtjfgdddddgd}d|j_tj .}|j|d  | j|}tj	|j
d| W d Q R X d S )Nrc   rd   re   rf   rg   )r\   r   )r   ri   rj   r   r#   r^   r_   r`   rU   ra   	set_index)rR   originalr!   written_and_read_againr   r   r   test_read_write_dta5K  s    

zTestStata.test_read_write_dta5c             C   s   | j | j}d|j_|jjtj|_|d jtj|d< |d jtj|d< tj 2}|j	|d  | j
|}tj|jd|dd W d Q R X d S )Nr   r   r   F)check_index_type)r   r0   r   r#   rk   ri   int32r^   r_   r`   rU   ra   r   )rR   r   r!   r   r   r   r   test_write_dta6W  s    

zTestStata.test_write_dta6c             C   s   t ddddtjdggdddddgd	}t|d td
|d< d|j_|jjtj|_|d jtj|d< t	j
 :}|j|ddi|d | j|}t	j|jd|dd W d Q R X d S )Nstringobjectr   g?z
2003-12-25integerfloatingr   )r   r\   )dtyper   tc)rV   F)r   )r   ri   
datetime64r   r   r   r#   rk   r   r^   r_   r`   rU   ra   r   )rR   rV   r   r!   r   r   r   r   test_read_write_dta10g  s    

zTestStata.test_read_write_dta10c             C   s<   t j *}ttjjddtdd}|j| W d Q R X d S )Nry   r   AB)r\   )r^   r_   r   ri   randomrandnlistr`   )rR   r!   dfr   r   r   test_stata_doc_examples|  s    
z!TestStata.test_stata_doc_examplesc             C   sv   t jjd tjt jjddtdd}t j|jdddf< |j	 }t
j }|j|d	d
 W d Q R X t
j|| d S )Ni  r   r   Zabcd)r\   r   r   r   F)r]   )ri   r   seedr   r   r   r   rj   locr   r^   r_   r`   ra   )rR   r   Zdf_copyr!   r   r   r   test_write_preserves_original  s    
z'TestStata.test_write_preserves_originalc             C   s~   t | j}t | j}|jd }|jd }||ks4tt|tsBttj *}|j|d|d t |}tj	|| W d Q R X d S )Nr   F)r]   rV   )
r   r5   	kreis1849r   
isinstancestrr^   r_   r`   ra   )rR   rV   rawencodedresultrn   r!   reread_encodedr   r   r   test_encoding  s    




zTestStata.test_encodingc             C   s   t dgddddgd	}t dgdd
ddgd	}d|j_|jtj}tj J}tjt	j
jj |j|d  W d Q R X | j|}tj|jd| W d Q R X d S )Nr   r   r   r   Zgoodu   bädZ8numberZ%astringwithmorethan32characters______)r\   Zb_dZ_8number astringwithmorethan32characters_r   )r   r   r   r   )r   r   r   r   )r   r   r#   rk   ri   r   r^   r_   assert_produces_warningr   r   r   r   r`   rU   ra   r   )rR   r   	formattedr!   r   r   r   r   test_read_write_dta11  s     


zTestStata.test_read_write_dta11c             C   s   t dgddd	d
ddgd}t dgddddddgd}d|j_|jtj}tj f}tj	dd2}tj
dt |j|d |d t|dkstW d Q R X | j|}tj|jd| W d Q R X d S )Nr   r   r   r   r   rz   Z!astringwithmorethan32characters_1Z!astringwithmorethan32characters_2+-shortdelete)r\   r   Z _0astringwithmorethan32character_Z_1_Z_short_deleter   T)r}   r~   )rV   )r   r   r   r   r   rz   )r   r   r   r   r   rz   )r   r   r#   rk   ri   r   r^   r_   r   r   r   r   r`   r   r   rU   ra   r   )rR   rV   r   r   r!   r   r   r   r   r   test_read_write_dta12  s2    



zTestStata.test_read_write_dta12c          
   C   s   t d	tjd}t d
tjd}t dtjd}t|||d}d|j_|}|d jtj	|d< t
j ,}|j| | j|}t
j|jd| W d Q R X d S )Nr   r   )r      !   )r   r   int64r   r   i   i   l        )r   ri   r   r   r   r   r   r#   rk   float64r^   r_   r`   rU   ra   r   )rR   s1s2s3r   r   r!   r   r   r   r   test_read_write_dta13  s    


zTestStata.test_read_write_dta13r8   r$   r9   r:   c       
      C   s   t | |}| j|}d|j_| j| j}dddddg}x"|D ]}|| jddd||< q<W |d jtj	|d< t
j|d	 d
d|d	< tj|| tj 6}|j|d	di|d | j|}	tj|	jd| W d Q R X d S )Nr   byte_int_long_float_double_T)r   numericdate_tdZcoerce)errorstd)rV   )rh   rU   r   r#   r   r7   _convertrk   ri   rl   r   to_datetimer^   ra   r_   r`   r   )
rR   rT   r%   rV   rm   rn   colsr   r!   r   r   r   r   test_read_write_reread_dta14  s    




z&TestStata.test_read_write_reread_dta14r<   r=   r>   r?   c             C   s   | j | j}|d jtj|d< |d jtj|d< |d jtj|d< |d jtj|d< |d jtj|d< |d j	t
jd	d|d< t| |}| j|}tj|| d S )
Nr   r   r   r   r   r   %Y-%m-%d)args)r  )r   r;   rk   ri   r   r   r   rl   r   applyr   strptimerh   rU   r^   ra   )rR   rT   rn   rm   r   r   r   test_read_write_reread_dta15  s    

z&TestStata.test_read_write_reread_dta15c             C   s~   t dgdgd}tddddd}d	}tj H}|j||||d
 t|"}|jdksXt|j|ksftW d Q R X W d Q R X d S )Nr   variable)r\   i  r            zThis is a data file.)
time_stampr   rV   z29 Feb 2000 14:21)r   )	r   r   r^   r_   r`   r   r  r   r   )rR   rV   r   r  r   r!   readerr   r   r   test_timestamp_and_label  s    

z"TestStata.test_timestamp_and_labelc             C   sZ   t dgdgd}d}tj 4}d}tjt|d |j|||d W d Q R X W d Q R X d S )	Nr   r	  )r\   z01 Jan 2000, 00:00:00z"time_stamp should be datetime type)match)r  rV   )r   )r   r^   r_   pytestraises
ValueErrorr`   )rR   rV   r   r  r!   msgr   r   r   test_invalid_timestamp!  s    
z TestStata.test_invalid_timestampc             C   s   t tjtjdd}d|j_tj d}tjt	 |j
| W d Q R X | j|}|jd}t|j}dd }t|||_tj|| W d Q R X d S )Ng      9@r   r   c             S   s   t | d S )Nr   )int)r   r   r   r   <lambda>5  s    z5TestStata.test_numeric_column_names.<locals>.<lambda>)r   r   )r   ri   reshapearanger   r#   r^   r_   r   r   r`   rU   r   r   r\   mapra   )rR   r   r!   r   r\   Zconvert_col_namer   r   r   test_numeric_column_names*  s    



z#TestStata.test_numeric_column_namesc             C   s   t tjdtjd}t tjdtjd}tj|d d d< tj|dd d< t||d}d|j_t	j
 4}|j||d | j|}|jd}t	j|| W d Q R X d S )Ng      @)r   r   r   )r   r   r   )rV   )r   ri   r  rl   r   rj   r   r   r#   r^   r_   r`   rU   r   ra   )rR   rV   r   r   r   r!   r   r   r   r   test_nan_to_missing_value9  s    


z#TestStata.test_nan_to_missing_valuec             C   s   ddg}t tjtjdd|d}d|j_tj D}|j|dd	 | j	|}t
jt|jjd
 |d  W d Q R X W d Q R X d S )Nr   r   g      $@r   r   )r\   Zindex_not_writtenF)r]   )r  )r   r   )r   ri   r  r  r   r#   r^   r_   r`   rU   r  r  KeyError)rR   r\   r   r!   r   r   r   r   test_no_indexG  s    

zTestStata.test_no_indexc          
   C   sr   t ddg}t ddgtjd}t||d}d|j_tj ,}|j| | j	|}tj
|jd| W d Q R X d S )Nr   zA longer stringg      ?g       @)r   )r   r   r   )r   ri   r   r   r   r#   r^   r_   r`   rU   ra   r   )rR   r   r   r   r!   r   r   r   r   test_string_no_datesQ  s    


zTestStata.test_string_no_datesc       	      C   s   t ddgtjd}t ddgtjd}t ddgtjd}t ddgtjd}t||||d}d	|j_tj	 }tj
t |j| W d Q R X | j|}|j }t |d
 tjd|d
< t |d tjd|d< t |d tjd|d< tj|jd	| W d Q R X d S )Nr   c   )r      r      ?   )s0r   r   r   r   r   r   r   i   i  l            l    )r   ri   r   r   r   r   r   r#   r^   r_   r   r	   r`   rU   r   r   r   ra   r   )	rR   r$  r   r   r   r   r!   r   modifiedr   r   r   test_large_value_conversion[  s    

z%TestStata.test_large_value_conversionc             C   s   t tddddddg}d|j_tj X}tjt |j|dd	i W d Q R X | j	|}|j
 }d
g|_tj|jd| W d Q R X d S )Ni  rp   rq   rr   rs   rt   r   r   r   Z_0)r   r   r   r#   r^   r_   r   r   r`   rU   r   r\   ra   r   )rR   r   r!   r   r%  r   r   r   test_dates_invalid_columnm  s    

z#TestStata.test_dates_invalid_columnc             C   s   t jj| jd}tj|}dddd
gddddgddddgg}tj|}dddd	g|_|d jt	j
|d< |d jt	j|d< |d jt	j|d< |d	 jt	j|d	< tj|jd| d S )NzS4_EDUC1.dtar   r   r   r   ZclustnumZpri_schlZpsch_numZpsch_disr(  r(  )r    r!   r"   r   r   r   r   r\   rk   ri   r   r   rl   r^   ra   head)rR   dpathr   Zdf0r   r   r   test_105y  s    
"
zTestStata.test_105c             C   s4   t jj| jd}t|}|j i ks(t|j  d S )NzS4_EDUC1.dta)r    r!   r"   r   r   value_labelsr   close)rR   r*  r  r   r   r   test_value_labels_old_format  s    z&TestStata.test_value_labels_old_formatc       	   
   C   s   dddddddg}dd	 |D }t d
dddddgt| }t|g|d}d|j_t d
dddddt d
ddt d
ddt d
ddt d
ddt d
ddt d
ddg}t|g|d}d|j_tj .}|j|| | j|}tj	|j
d| W d Q R X d S )Nr   r   Ztwr^   Ztqthtyc             S   s   i | ]
}||qS r   r   )r   r   r   r   r   
<dictcomp>  s    z6TestStata.test_date_export_formats.<locals>.<dictcomp>i  rp   rt   rr   rs   )r\   r   rq   r   ry   rv   )r   r   r   r   r#   r^   r_   r`   rU   ra   r   )	rR   r\   Zconversionsr   r   expected_valuesrn   r!   r   r   r   r   test_date_export_formats  s$    






z"TestStata.test_date_export_formatsc          
   C   sr   t dgd ggdgd}t dgdggdgd}d|j_tj ,}|j| | j|}tj|jd| W d Q R X d S )N1Zfoo)r\   r   r   )	r   r   r#   r^   r_   r`   rU   ra   r   )rR   r   rn   r!   r   r   r   r   test_write_missing_strings  s    


z$TestStata.test_write_missing_strings	byteorder><c             C   sJ  t dddgtjd}t dddgtjd}t dddgtjd}t dddgtjd}t dddgtjd}t dddgtjd}t dddgtjd}	t|||||||	d}
d|
j_|
j	 }tj
tj
tjtjtjtjtjf}x,t|j|D ]\}}|| j|||<  qW tj 6}|
j|||d | j|}|jd}tj|| W d Q R X d S )Nr   r   T)r   d      r   r"     rx       )r$  r   r   r   s4s5s6r   )r6  rV   i   i  i   i  l        il        l    )r   ri   Zbool_Zuint8Zuint16Zuint32r   r   r#   r   r   r   r   r   zipr\   rk   r^   r_   r`   rU   r   ra   )rR   r6  rV   r$  r   r   r   r=  r>  r?  r   rn   Zexpected_typesr   tr!   r   r   r   r   test_bool_uint  s2    


zTestStata.test_bool_uintc             C   s   t | j}|j }W d Q R X t | j}|j }W d Q R X d}d}xH|j D ]<\}}||ksbt||| ksrt||ks~t||ksNtqNW d S )	Nvar1var2var3label1label2label3)rC  rD  rE  )rF  rG  rH  )r   r@   r   rA   itemsr   )rR   r   Zsr_115Zsr_117keyslabelskvr   r   r   test_variable_labels  s    zTestStata.test_variable_labelsc             C   s   d
}i }x2|D ]*}t d| d| d| g|dt| < qW t|}tj }|j|dd	 t|p}|j}|j}|j	}	xVt
||	|D ]F\}
}}t|
dd  t|dd kstt|
dd  |kstqW W d Q R X W d Q R X d S )Nr   r9     r   r   r   sF)r]   )r   r9  rO  )r   r   r   r^   r_   r`   r   typlistZvarlistZfmtlistr@  r  r   )rR   str_lensrP  str_lenr   r!   srrR  	variablesformatsr	  fmttypr   r   r   test_minimal_size_col  s    
(

$zTestStata.test_minimal_size_colc             C   s   d
}i }x2|D ]*}t d| d| d| g|dt| < qW t|}d}tjt|d	$ tj }|j| W d Q R X W d Q R X d S )Nr   rO    r   r   r   rP  zFixed width strings in Stata \.dta files are limited to 244 \(or fewer\)\ncharacters\.  Column 's500' does not satisfy this restriction\. Use the\n'version=117' parameter to write the newer \(Stata 13 and later\) format\.)r  )r   rO  r[  )	r   r   r   r  r  r  r^   r_   r`   )rR   rS  rP  rT  r   r  r!   r   r   r   test_excessively_long_string  s    
(
z&TestStata.test_excessively_long_stringc             C   sN  d}t dggdgd}tj *}|j| t|}|j}W d Q R X W d Q R X dd td	D }|jd
d xN|D ]F}|| d }x4td
dD ]&}	t|d |	 }
|
j	||	 kst
qW qrW ttjddd
 }
|
j	dkst
ttjddd
 }
|
j	dkst
ttjddd
 }
|
j	dks&t
ttjddd
 }
|
j	dksJt
d S )Nr   hlg        r   )r\   c             S   s   g | ]}d t d|  qS ).a   )chr)r   ir   r   r   r     s    z:TestStata.test_missing_value_generator.<locals>.<listcomp>   r   r_  r      z<fs      s     z.zz<ds         s        )r   r]  r^  )r   r^   r_   r`   r   ZVALID_RANGErangeinsertr
   r   r   structunpack)rR   typesr   r!   r   Zvalid_ranger2  rA  offsetrb  valr   r   r   test_missing_value_generator  s.    



z&TestStata.test_missing_value_generatorrB   rC   rD   c                s   dddddg}t d}t|jj g }x2tdD ]&  fdd	td
D }|j| q2W t||d}tt| |dd}t	j
|| d S )NZint8_Zint16_Zint32_Zfloat32_Zfloat64_e   rd  c                s    g | ]}t  |d    qS )rd  )r
   )r   j)rb  rJ  r   r   r   +  s    z;TestStata.test_missing_value_conversion.<locals>.<listcomp>r   )r\   T)Zconvert_missing)r
   sortedZMISSING_VALUESrJ  re  appendr   r   rh   r^   ra   )rR   rT   r\   Zsmvr   rowrn   rm   r   )rb  rJ  r   test_missing_value_conversion$  s    z'TestStata.test_missing_value_conversionc             C   s  ddddddg}ddddd	d
g}ddddddg}ddddddg}ddddddg}ddddddg}g }xt t|D ]}g }	xt dD ]|}
|
dkr|	jt|| || || || || ||  q|
dkr|	jt|| dd q|	jt|| || ||  qW |j|	 qrW |jtjgd  dddddddg}tddd|d d< tddd|d d< tddd|d d	< tddd|d d< tdd	d|d	 d< tdd	d |d	 d< |d	 d	< tddd |d	 d< |d	 d< tddd |d d<  |d d< |d d	< tddd |d d< |d d< t||td }t| j	}t| j
}tj||d!d" tj||d!d" d#d$ |D }tj :}d%|j_|j|| | j|}tj|jd%|d!d" W d Q R X d S )&Ni  i  i'  r9  i  i  r   rw   r   r   rx      rr   r   ;   rv   rz   Zdate_tcr   Zdate_twZdate_tmZdate_tqZdate_thZdate_ty   r   r   ry   r   r;  i  )r\   r   T)r   c             S   s   i | ]}|dd |qS )r   Nr(  r   )r   r   r   r   r   r1  `  s    z,TestStata.test_big_dates.<locals>.<dictcomp>r   )re  r   rp  r   r   r   r   r   r   rE   rF   r^   ra   r_   r   r#   r`   rU   r   )rR   yrmoddhrmmssrn   rb  rq  rn  r\   r   r   Zdate_conversionr!   r   r   r   r   test_big_dates2  s^    2"  , 



zTestStata.test_big_datesc             C   s   | j | j}|d jtj|d< |d jtj|d< |d jtj|d< |d jtj|d< |d jtj|d< |d j	t
jdd|d< t| jd	d
}tj|| t| jd	dd}| j | j}|d j	t
jdd|d< tj|| d S )Nr   r   r   r   r   r   %Y-%m-%d)r  T)r   F)r   Zpreserve_dtypes)r}  )r}  )r   r;   rk   ri   r   r   r   rl   r   r  r   r  r   r?   r^   ra   )rR   rn   Zno_conversion
conversionr   r   r   test_dtype_conversionl  s     zTestStata.test_dtype_conversionc             C   sX  | j | j}|d jtj|d< |d jtj|d< |d jtj|d< |d jtj|d< |d jtj|d< |d j	t
jdd|d< dddg}|| }t| jd	|d
}tj|| dddg}|| }t| jd	|d
}tj|| d}tjt|d ddg}t| jd	|d
 W d Q R X d}tjt|d" ddddg}t| jd	|d
 W d Q R X d S )Nr   r   r   r   r   r   %Y-%m-%d)r  T)r   r\   z"columns contains duplicate entries)r  zEThe following columns were not found in the Stata data set: not_found	not_found)r  )r   r;   rk   ri   r   r   r   rl   r   r  r   r  r   r?   r^   ra   r  r  r  )rR   rn   r\   ZdroppedZ	reorderedr  r   r   r   test_drop_column  s0    

zTestStata.test_drop_columnz;ignore:\nStata value:pandas.io.stata.ValueLabelTypeMismatchc       	         s  t jddddddgddddddgdddddd	gd
ddd
d
dgdddtjddgdddtjddgdd
dtjddgdddtjddgdddtjddgdddtjddgg
ddddddgd  j }tj fdd D dd d|j_|d j	t
|d< |d j	t
|d< x^|D ]V}|| j }|jd j}|j }|dkrD|j|d!d"}|jjd d!d# |||< qW tj 4} j||d$ | j|}|jd}tj|| W d Q R X d S )%Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz   rv   ru   r   ry   r   r   r   r   r   Z	unlabeled)r\   c                s   g | ]} | j d qS )r   )rk   )r   r   )r   r   r   r     s    z6TestStata.test_categorical_writing.<locals>.<listcomp>)axisr   r   T)r   )r   )rV   )r   r   ri   rj   r   r   concatr   r#   r  r   rk   r   Z
as_orderedr   r   r   r^   r_   r`   rU   r   ra   )	rR   rV   rn   r   r   r   r!   r   resr   )r   r   test_categorical_writing  sH    





z"TestStata.test_categorical_writingc                s   t jjdd gdd gdd gdd ggdgd t j fdd	 D d
d tj .}d}tjt|d  j	| W d Q R X W d Q R X t jjdgdgdgdgd
ggdgd t j fdd	 D d
d tj
t jjj  j	| W d Q R X d S )Nr   i'  r   r   dZToo_long)r\   c                s   g | ]} | j d qS )r   )rk   )r   r   )r   r   r   r     s    zBTestStata.test_categorical_warnings_and_errors.<locals>.<listcomp>r   )r  zbStata value labels for a single variable must have a combined length less than 32,000 characters\.)r  c                s   g | ]} | j d qS )r   )rk   )r   r   )r   r   r   r     s    )r   r   r   r  r^   r_   r  r  r  r`   r   r   r   ZValueLabelTypeMismatch)rR   r!   r  r   )r   r   $test_categorical_warnings_and_errors  s    "

 z.TestStata.test_categorical_warnings_and_errorsc       
         s   dd t dD }|jtjg tjj|dgd tj fdd D dd d	 j_	t
j t} j||d
 | j|}|jd	} j }x6|D ].}|| j}|j j}	|j|	dd}|||< qW t
j|| W d Q R X d S )Nc             S   s   g | ]}d t | gqS )r   )r   )r   rb  r   r   r   r     s    zHTestStata.test_categorical_with_stata_missing_values.<locals>.<listcomp>x   Zmany_labels)r\   c                s   g | ]} | j d qS )r   )rk   )r   r   )r   r   r   r     s    r   )r  r   )rV   T)r   )re  rp  ri   rj   r   r   r   r  r   r#   r^   r_   r`   rU   r   r   r   remove_unused_categoriesr   r   ra   )
rR   rV   valuesr!   r   r  rn   r   r   Znew_catsr   )r   r   *test_categorical_with_stata_missing_values  s"    





z4TestStata.test_categorical_with_stata_missing_valuesrG   rH   c       	      C   s  dddddddgt jdfdd	dddddgt jdd d d fdddddddgt jdd
dddgfdddddddgt jddfdddddgt jdd
dddgfdddddddgt jdfdddddddgt jdfg}g }xR|D ]J\}}}}|r|j|tjj||ddf q|j|tj|t jdf qW t	j
t|}t| |}t|}tj|| xV|D ]N}t|| jr`tj|| jj|| jj tj|| jj|| jj q`W d S )NTr   r   r   r   r  er   reverser   Znoorderr   r   r   r   r   Zfloat_missingFZnolabelg      ?g       @g      @g      @g      @Zint32_mixed)r   )r   rQ  rQ  rQ  )ri   r  arrayrp  r   Categorical
from_codesr   rl   r   	from_dictdictrh   r   r^   ra   r   r   assert_series_equalr   codesassert_index_equalr   )	rR   rT   rn   r  Zis_catr   rK  r  rm   r   r   r   test_categorical_order  s,    $$ 

z TestStata.test_categorical_orderrI   rJ   c          
   C   s   t t| |}|jddd}tj|jd |_ddddddddddg
}d	d
dddg}tjj	||dd}tj
|dd}tj||d  d S )NZsrhfirst)Zna_positionr   r   r   r   r   ZPoorZFairZGoodz	Very goodZ	ExcellentT)r  r   r   )r#   rQ  rQ  )r   rh   Zsort_valuesri   r  shaper   r   r  r  r   r^   r  )rR   rT   rm   r  r   r   rn   r   r   r   test_categorical_sorting6  s    z"TestStata.test_categorical_sortingc             C   sb   t | |}t|}t|dd}x>|D ]6}t|| js8q$|| jjsHt|| jj s$tq$W d S )NF)order_categoricals)rh   r   r   r   r   r   r   )rR   rT   rm   Zparsed_unorderedr   r   r   r   test_categorical_orderingG  s    

z#TestStata.test_categorical_orderingr+   rA   rF   	chunksizer   r   convert_categoricalsFr   c             C   s   t | |}tjdd}tjd t|||d}W d Q R X t|d||d}d}	xtdD ]}
tjdd6}tjd y|j|}W n tk
r   P Y nX W d Q R X |j|	|	| d d f j	 }| j
|}tj||ddd	 |	|7 }	qXW |j  d S )
NT)r}   r~   )r  r   )iteratorr  r   r   r   F)r   r   )rh   r   r   r   r   re  readStopIterationilocr   _convert_categoricalr^   ra   r-  )rR   rT   r  r  r   fnamer   rm   itrposrn  chunk
from_framer   r   r   test_read_chunks_117S  s4    



zTestStata.test_read_chunks_117)r  returnc             C   sZ   xT| D ]L}| | }t |jr|jj }|jjtkrJtj|jj}|j|}|| |< qW | S )zX
        Emulate the categorical casting behavior we expect from roundtripping.
        )	r   r   r   r  r   r   r   Indexr   )r  r   Zserr   r   r   r   r   r    s    



zTestStata._convert_categoricalc          &   C   s@  | j }t|}t|dd.}|jd}tj|jddd d f | W d Q R X t|dd0}t|}tj|jddd d f |d  W d Q R X t|dd.}|jd}tj|jddd d f | W d Q R X t|dd,}|j }tj|jddd d f | W d Q R X t|dd}tj	|}W d Q R X tj|| d S )NT)r  r   r   )r  r   )
r/   r   r  r^   ra   r  r   Z	get_chunkr   r  )rR   r  rm   r  r  Zfrom_chunksr   r   r   test_iterator  s"    
(,
((zTestStata.test_iteratorr*   r@   rE   c             C   s   t | |}tjdd}tjd t|||d}W d Q R X t|d||d}d}	xtdD ]}
tjdd6}tjd y|j|}W n tk
r   P Y nX W d Q R X |j|	|	| d d f j	 }| j
|}tj||ddd	 |	|7 }	qXW |j  d S )
NT)r}   r~   )r  r   )r  r   r  r   r   F)r   r   )rh   r   r   r   r   re  r  r  r  r   r  r^   ra   r-  )rR   rT   r  r  r   r  r   rm   r  r  rn  r  r  r   r   r   test_read_chunks_115  s4    



zTestStata.test_read_chunks_115c       
      C   s   | j }dddg}d}t||d}t|ddf}d}xZtd	D ]N}|j||d}|d krXP |j||| d d f }	tj|	|d
d ||7 }q<W W d Q R X d S )Nr   Zcpim1r   )r\   T)r  r   r   F)r   )r/   r   re  r  r  r^   ra   )
rR   r  r\   r  rm   r  r  rn  r  r  r   r   r   test_read_chunks_columns  s    
z"TestStata.test_read_chunks_columnsc             C   s   d|j _dddd}tj L}|j|||d t|}|j }W d Q R X ddddd}||ksdtW d Q R X d	|d< tj >}|j|||d t|}|j }W d Q R X ||kstW d Q R X d S )
Nr   z	City RankzCity ExponentCity)r   r   r   )r   rV   r   )r   r   r   r   z	The Index)r   r#   r^   r_   r`   r   r   r   )rR   rV   r   r   r!   rU  Zread_labelsZexpected_labelsr   r   r   test_write_variable_labels  s"    



z$TestStata.test_write_variable_labelsc             C   s^   d|j _dd ddd}tj 4}d}tjt|d |j|||d	 W d Q R X W d Q R X d S )
Nr   z	very longry   zCity Exponentr  )r   r   r   z.Variable labels must be 80 characters or fewer)r  )r   rV   )r   r#   r^   r_   r  r  r  r`   )rR   rV   r   r   r!   r  r   r   r   test_invalid_variable_labels  s    
z&TestStata.test_invalid_variable_labelsc             C   sb   d|j _dd ddd}d|d< tj 0}tjtd	d
 |j|||d W d Q R X W d Q R X d S )Nr   z	very longry   zCity Exponentr  )r   r   r   u   invalid character Œr   z,Variable labels must contain only characters)r  )r   rV   )r   r#   r^   r_   r  r  r  r`   )rR   rV   r   r   r!   r   r   r   $test_invalid_variable_label_encoding  s    
z.TestStata.test_invalid_variable_label_encodingc              C   s   ddddddg}dddj |d	}d
}tjt|d( tj }|j||d W d Q R X W d Q R X dddd	}d}tjt|d( tj }|j||d W d Q R X W d Q R X d S )Nu   Ρu   Αu   Νu   Δu   Σz	City RankzCity Exponentr   )r   r   r   zKVariable labels must contain only characters that can be encoded in Latin-1)r  )r   zqA very, very, very long variable label that is too long for Stata which means that it has more than 80 charactersz.Variable labels must be 80 characters or fewer)r"   r  r  r  r^   r_   r`   )rR   r   r  Zvariable_labels_utf8r  r!   Zvariable_labels_longr   r   r    test_write_variable_label_errors,  s    
"
z*TestStata.test_write_variable_label_errorsc             C   s  t jdddddddt jdddddddt jdd	d
d	d
d	dg}tjdddgdddg|d}tj }|j|dd t|dd}tj|| |j|dddid t|dd}tj|| |j	j
 jd}|j|d|did t|dd}tj|| W d Q R X d S )Ni  rw   rx   i.  i  r  iR  i  rv   r   i  g      ?g       @g      @applebananacherry)numsstrsdatesF)r]   T)r   r  r   )r]   r   )dtr   r   r   r^   r_   r`   r   ra   r\   tolistr   )rR   r  r   r!   rereaddirectZ	dates_idxr   r   r   test_default_date_conversionJ  s$    

z&TestStata.test_default_date_conversionc             C   sR   t jddd	gi}d}tjt|d$ tj }|j| W d Q R X W d Q R X d S )
Nr   r   y               @r   y              @z"Data type complex128 not supported)r  y      ?       @y       @      @)r   r   r  r  NotImplementedErrorr^   r_   r`   )rR   r   r  r!   r   r   r   test_unsupported_typeg  s
    
zTestStata.test_unsupported_typec             C   s  t jdddddddt jdddddddt jdd	d
d	d
d	dg}tjdddgdddg|d}d}tjt|d, tj }|j	|ddid W d Q R X W d Q R X tj
dddd}tjdddgdddg|d}tjtdd$ tj }|j	| W d Q R X W d Q R X d S )Ni  rw   rx   i.  i  r  iR  i  rv   r   i  g      ?g       @g      @r  r  r  )r  r  r  zFormat %tC not implemented)r  r  ZtC)r   z1-1-1990r   zAsia/Hong_Kong)ZperiodstzzData type datetime64)r  r   r   r   r  r  r  r^   r_   r`   Z
date_range)rR   r  r   r  r!   r   r   r   test_unsupported_datetypeo  s&    

&

z#TestStata.test_unsupported_datetypec             C   s0   d}t jt|d t| jdd W d Q R X d S )Na*  
Value labels for column ethnicsn are not unique. These cannot be converted to
pandas categoricals.

Either read the file with `convert_categoricals` set to False or use the
low level interface in `StataReader` to separately read the values and the
value_labels.

The repeated labels are:
-+
wolof
)r  T)r  )r  r  r  r   rM   )rR   r  r   r   r   test_repeated_column_labels  s    z%TestStata.test_repeated_column_labelsc             C   s   t | j}tjdddddddtjddg
ddddtjdddddg
dtjddddddddg
dd	d
dddddddg
d}|ddddg }tj|| d S )Nr   r   r   r   r   r   rz   r   r   r   r  r  r   gr]  rb  rn  )r   r   r   r   r   r   r   r   )r   rN   r   r   ri   NaNr^   ra   )rR   r   r   r   r   r   test_stata_111  s    
zTestStata.test_stata_111c             C   s   t dtjtjjdgdtjtjjtjtjjgd}d}tjt|d$ t	j
 }|j| W d Q R X W d Q R X tj|jd	< d}tjt|d$ t	j
 }|j| W d Q R X W d Q R X d S )
Ng        g      )ColumnOkColumnTooBigzZColumn ColumnTooBig has a maximum value \(.+\) outside the range supported by Stata \(.+\))r  r   r  zaColumn ColumnTooBig has a maximum value of infinity which is outside the range supported by Stata)r   r  )r   ri   finfodoubleepsmaxr  r  r  r^   r_   r`   infr   )rR   r   r  r!   r   r   r   test_out_of_range_double  s    $

z"TestStata.test_out_of_range_doublec             C   s  t dtjtjjtjtjjd gdtjtjjtjtjjgd}d|j_x |D ]}|| jtj||< qTW t	j
 >}|j| t|}|d jtj|d< t	j||jd W d Q R X tj|jd	< d}tjt|d$ t	j
 }|j| W d Q R X W d Q R X d S )
Ng        g      $@)r  r  r   r  r   zaColumn ColumnTooBig has a maximum value of infinity which is outside the range supported by Stata)r  )r   r  )r   ri   r  rl   r  r  r   r#   rk   r^   r_   r`   r   r   ra   r   r  r   r  r  r  )rR   r   r   r!   r  r  r   r   r   test_out_of_range_float  s(    



z!TestStata.test_out_of_range_floatc             C   s6   t j }d|j_dd }t j|j|}t j|| d S )Nr   c             S   s   t | jdS )Nr   )r   r   )r   r   r   r   r    s    z-TestStata.test_path_pathlib.<locals>.<lambda>)r^   makeDataFramer   r#   Zround_trip_pathlibr`   ra   )rR   r   r  r   r   r   r   test_path_pathlib  s
    zTestStata.test_path_pathlibc             C   s6   t j }d|j_dd }t j|j|}t j|| d S )Nr   c             S   s   t | jdS )Nr   )r   r   )r   r   r   r   r    s    z6TestStata.test_pickle_path_localpath.<locals>.<lambda>)r^   r  r   r#   Zround_trip_localpathr`   ra   )rR   r   r  r   r   r   r   test_pickle_path_localpath  s
    z$TestStata.test_pickle_path_localpathr]   c             C   s   ddddddgi}t j|d}|d jd|d< tj 6}|j||d t j|dd	}|j }W d Q R X W d Q R X |dddddd
ikstd S )NABEC)r   r   )r]   T)r  )r   r   r   r   )	r   r   rk   r^   r_   r`   r   r,  r   )rR   r]   r  r   r!   Zdta_iterr,  r   r   r   test_value_labels_iterator  s    
z$TestStata.test_value_labels_iteratorc             C   sL   t j }d|j_t j }|j| tj|dd}W d Q R X t j|| d S )Nr   )	index_col)	r^   r  r   r#   r_   r`   r   r   ra   )rR   r   r!   r  r   r   r   test_set_index  s    

zTestStata.test_set_indexcolumnmsdayweekmonthZqtrZhalfrv  c             C   s:   t | j}|jd|f }|jd|d f }||ks6td S )Nr   _fmt)r   rQ   r   r   )rR   r  r   Zunformattedr   r   r   r   (test_date_parsing_ignores_format_details  s    
z2TestStata.test_date_parsing_ignores_format_detailsc             C   sV  t dddddddtjdddd dd dgd	d
dddddtjdddd ddggddddddddddddgd}t|d td|d< t|d tjd|d< t|d tjd|d< |d jtj|d< t|d tj	d|d< d|j
_|j
jtj|_
|j }tj J}|j|ddidgdd | j|}tj|jd|dd tj|| W d Q R X d S ) Nr   r   r   g?z
2003-12-25r   i  i  zstring-1zobject-1z
2003-12-26r   r   r   r   r   rl   r   r   r   Zs2045ZsrtlZforced_strl)r   r\   )r   r   r   rX   )r   convert_strlrV   F)r   )r   ri   r   r   r   r   r   rk   r   rl   r   r#   r   r^   r_   r`   rU   ra   r   )rR   r   r   r!   r   r   r   r   test_writer_117   sr    


zTestStata.test_writer_117c             C   s   t dd ddgdd ddggd	d
 ddgd}d|j_tjtjjjZ tj	 F}|j
|ddgdd | j|}|jd}|j|_tj||dd W d Q R X W d Q R X d S )Nr   i  r  r  r   i  r  r  Zlong1ry   longr   )r\   r   rX   )r  rV   F)r   )r   r   r#   r^   r   r   r   r   r   r_   r`   rU   r   r\   ra   )rR   r   r!   r  r   r   r   test_convert_strl_name_swapg  s    


z%TestStata.test_convert_strl_name_swapc             C   s   t jdddddddt jdddddddt jdd	d
d	d
d	dg}tjdddgdddg|d}tj 6}d}tjt|d |j	|ddid W d Q R X W d Q R X d S )Ni  rw   rx   i.  i  r  iR  i  rv   r   i  g      ?g       @g      @r  r  r  )r  r  r  z0convert_dates key must be a column or an integer)r  Z
wrong_namer   )r   )
r  r   r   r   r^   r_   r  r  r  r`   )rR   r  r   r!   r  r   r   r   test_invalid_date_conversionv  s    

z&TestStata.test_invalid_date_conversionc             C   s   t j }tj }d|j_tj P}|j||d |jd t	|d}|j
|j  W d Q R X tj|dd}W d Q R X tj|| d S )Nr   )rV   r   wb)r  )r   BytesIOr^   r  r   r#   r_   r`   seekopenwriter  r   r   ra   )rR   rV   Zbior   r!   dtar  r   r   r   test_nonfile_writing  s    

zTestStata.test_nonfile_writingc             C   s   t j }d|j_t j R}tj|d}|j|dd W d Q R X tj|d}tj	|dd}W d Q R X W d Q R X t j
|| d S )Nr   r  rW   )rV   rb)r  )r^   r  r   r#   r_   gzipGzipFiler`   r   r   ra   )rR   r   r!   gzr  r   r   r   test_gzip_writing  s    
"zTestStata.test_gzip_writingc             C   s   | j | j}dddddg}dddddgd	d
dd	dgdddddgdddddgdddddgdddddgdddddgg}tj||d}tj|| d S )Nutf8latin1asciiZ	utf8_strlZ
ascii_strlu   ραηδαςu   PÄNDÄSpu   ƤĀńĐąŜ   Ör   u   ᴘᴀᴎᴅᴀS   Ünz      r   r   rP  )r\   )rU   rO   r   r   r^   ra   )rR   Z
unicode_dfr\   r  rn   r   r   r   test_unicode_dta_118  s    zTestStata.test_unicode_dta_118c             C   s   dd ddd ddg}t j|}|jjd|_tj x}|j|ddd	 t|}|jd
}tj	|| d |j
d d df< |j|ddgdd t|}|jd
}tj	|| W d Q R X d S )Nr   i  r   )mixednumberr   r   FrX   )r]   rV   r   r  )r]   r  rV   )r   r   r  rk   r^   r_   r`   r   fillnara   r   )rR   outputr!   r  rn   r   r   r   test_mixed_string_strl  s    



z TestStata.test_mixed_string_strlc             C   sp   dddd ddg}t j|}d |jd d df< tj .}tjtdd |j||d W d Q R X W d Q R X d S )Nnoner   )r  r  r   z Column `none` cannot be exported)r  )rV   )	r   r   r   r^   r_   r  r  r  r`   )rR   rV   r  r!   r   r   r   test_all_none_exception  s    

z!TestStata.test_all_none_exceptionc             C   sx   d}t |gdgd}tj R}d}d}tjt| d| d& tjt |j| W d Q R X W d Q R X W d Q R X d S )Nu9   Here is one __�__ Another one __·__ Another one __½__invalid)r\   z\'latin-1' codec can't encode character '\\ufffd' in position 14: ordinal not in range\(256\)zP'ascii' codec can't decode byte 0xef in position 14: ordinal not in range\(128\)|)r  )	r   r^   r_   r  r  UnicodeEncodeErrorr   ResourceWarningr`   )rR   rV   contentr   r!   Zmsg1Zmsg2r   r   r   test_invalid_file_not_written  s    
z'TestStata.test_invalid_file_not_writtenc       
      C   s   t dgd dgd gddgd}tj }|j|ddgd t|d	}|j }d}|jd
|ksft|jd|ksxt|jdd dd }xN|jddd  D ]8}|jdd }||j	dd  }	t
||	d kstqW W d Q R X W d Q R X d S )Npandasr   u   þâÑÐÅ§Zvar_strZvar_strl)r\   rX   )rV   r  r  zlatin-1zutf-8s   strlsr   s   GSO       r(  r(  )r   r^   r_   r`   r  r  encoder   splitfindr   )
rR   r  r!   r  r  rn   ZgsosZgsork  sizer   r   r   test_strl_latin1  s    
zTestStata.test_strl_latin1c             C   st   d}t jt8}t| j}t|dks*t|d jjd |ksBtW d Q R X t	j
dggd dgd}t j|| d S )Na  
One or more strings in the dta file could not be decoded using utf-8, and
so the fallback encoding of latin-1 is being used.  This can happen when a file
has been incorrectly encoded by Stata or some other software. You should verify
the string values returned are correct.   r   u   Düsseldorfr   )r\   )r^   r   UnicodeWarningr   r6   r   r   messager  r   r   ra   )rR   r  r   r   rn   r   r   r   test_encoding_latin1_118  s    
"z"TestStata.test_encoding_latin1_118c             C   s   t j| jd}t|}W d Q R X |jdks0t|jd dd ksFt|jd d	ksXt|jd dksjt|jd tjt	d
dddddkstd S )Nr  r     r   rz   r  i  rv   gQ	@i  rw   r  )r   r  )r   rz   )r   rv   rQ  )r   rQ  )r   r   )
r  r  rP   r   r  r   r  r   	Timestampr   )rR   r  r   r   r   r   test_stata_119  s    zTestStata.test_stata_119c             C   s   t jdddgdd}t jdddd	gd
dddgdddd ggddddgd}||d< dddddd}d}|d jtj|d< tj }t|||dg|d|d}|j	  t
|}|d jd|d< tj|| t|}	|	j|kst|	j |kst|j||dd t
|}
tj||
 W d Q R X d S )Nr   u   βu   ĉT)r   g      ?r   u   ᴬu   ᴀ relatively long ŝtringg       @r   u   ᴮr   g      @r   u   ᴰstrls)r\   u	   ᴐᴬᵀr  u	   ᵈᵉᵊu   ᴎტჄႲႳႴႶႺzLong Strings)r   u   βu   ĉr  u	   ᴐᴬᵀu   ᴅaᵀa-labelF)r   r  r   r]   rV   )rV   r]   )r   r  r   rk   ri   r   r^   r_   r   
write_filer   r  ra   r   r   r   r   r`   )rR   rV   r   r   r   r   r!   writerr   r  Zreread_to_statar   r   r   test_utf8_writer  sB    


zTestStata.test_utf8_writerc          !   C   s   t tjd
tjd}tj .}tjtdd t	||dd W d Q R X W d Q R X tj .}tjtdd t	||d	d W d Q R X W d Q R X d S )Nr     )r   z"version must be either 118 or 119.)r  rX   )rV   zYou must use version 119rY   )r   r  )
r   ri   Zzerosr   r^   r_   r  r  r  r   )rR   r   r!   r   r   r   test_writer_118_exceptions<  s    
"
z$TestStata.test_writer_118_exceptions)^__name__
__module____qualname__r  fixturerS   rU   r   markparametrizerb   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r&  r'  r+  r.  r3  r5  rB  rN  rZ  r\  rl  rr  r|  r  r  filterwarningsr  r  r  r  r  r  r  staticmethodr   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  Zslowr  r  r  r   r   r   r   r&   9   s   A 	<.( 
  %  	 


	!:!5 $ % '  "	G  	 ,r&   rV   i   l   o   q   rW   c             C   sX   |ddd}t jj|d}t jj|d|  d}tj|}tj|}tj||dd d S )	Nr   r   r   zstata-compat-118.dtazstata-compat-z.dtaF)r   )r    r!   r"   r   r   r^   ra   )rV   r   Z	data_baserefoldrn   Zold_dtar   r   r   test_backward_compatF  s    

r,  rX   rY   rZ   use_dictTFinferc             C   s  d}| r,| dkr| rdn| }|d| 7 }| }|r8d}|rDd| i}t tjjddtd	d
}d|j_tj|}|j	|||d | dkrt
j|d}	tj|	j }
W d Q R X n| dkrtj|d}	tj|	j|	jd }
W d Q R X nr| dkrtj|d}	tj|	j }
W d Q R X n@| dkrPtj|d}	tj|	j }
W d Q R X n| d kr^|}
t|
dd}tj|| W d Q R X d S )Nzdta_inferred_compression.dtar  r  r_  r.  methodry   r   r   )r\   r   )rV   compressionr  r@  rr   bz2xz)r  )r   ri   r   r   r   r   r#   r^   r_   r`   r  r  r   r  r  zipfileZipFilefilelistr2  lzmar   ra   )r0  rV   r-  r.  	file_namefile_extZcompression_argr   r!   compfpr  r   r   r   test_compressionP  s:    "


r<  r/  r@  r9  r  c       
      C   s   d| }d}t tjjddtdd}d|j_tj|}| |d}|j	||d	 | d
ksd|d
krt
j|d}t|jdkst|jd j|ksttj|j|jd }n|}t|dd}	tj|	| W d Q R X d S )Nztest.ztest.dtary   r   r   )r\   r   )r/  archive_name)r0  r@  r1  r   r   )r  )r   ri   r   r   r   r   r#   r^   r_   r`   r4  r5  r   r6  r   filenamer   r  r  r   ra   )
r/  r9  r8  r=  r   r!   r0  Zzpr;  r  r   r   r   test_compression_dictt  s    

r?  c             C   s   t dtdddddgddi}d|j_tj t}|j|| d t|d	d
d}xPt|D ]D\}}|j	d}d|kstt
tj|j|jjd	| d	|d    qVW W d Q R X d S )Ncatsr   r   r   r   )r   r   )rV   r   F)r  r  r   )r   r   r   r#   r^   r_   r`   r   	enumerater   r   r  r@  r  )rV   r   r!   r  rb  blockr   r   r   test_chunked_categorical  s    

rC  c       	      C   s
  t jj| d}dddddg}t|dd}tjtx xpt|D ]d\}}t|j	|d| d|d   ksnt
|dk rtjddg}ntjdg}tj|j	jj| q@W W d Q R X W d Q R X tjt& t|dd}|j }W d Q R X W d Q R X t|}tj|| d S )	Nzstata-dta-partially-labeled.dtar   r   g      @r   )r  r   r   )r    r!   r"   r   r^   r   r   rA  r   r@  r   r   r  ZFloat64Indexr  r   r   __next__r   ra   )	r   dta_filer  r  rb  rB  idxZlarge_chunkr  r   r   r    test_chunked_categorical_partial  s    &*rG  c             C   s   t jj| d}tjtdd t|dd W d Q R X tjtdd t|dd W d Q R X tjtdd t|dd W d Q R X d S )	Nzstata-dta-partially-labeled.dtazchunksize must be a positive)r  r   )r  r   r  rQ  )r    r!   r"   r  r  r  r   )r   rE  r   r   r   test_iterator_errors  s    rH  c                 s   ddgdgd   t  fddtdD } tj }| j|dd	 tj|d
d}tjdddgdd}x\t|D ]P\}}x$tdD ]}tj	|j
| j| qW tj|| j|d
 |d d
   qpW W d Q R X d S )NZc_labelZb_labelZa_labeli  c                s"   i | ]}t j d dd| qS )T)r   r   )r   r  )r   rL  )r  r   r   r1    s    z.test_iterator_value_labels.<locals>.<dictcomp>r   F)r]   r9  )r  r   )r   r   )r   re  r^   r_   r`   r   r   r  rA  r  dtypesr   ra   r  )r   r!   r  rn   rn  r  rb  r   )r  r   test_iterator_value_labels  s    
rJ  c              C   s   t tdd tdD tdd tdD ggddgd} tj }tjt | j|dd	 W d Q R X t|}t	t
jt
jgddgd
}tj|j| |jd | jd kst|jd t| jd kstW d Q R X d S )Nc             s   s   | ]}d | V  qdS )r   Nr   )r   rb  r   r   r   	<genexpr>  s    z&test_precision_loss.<locals>.<genexpr><   4   biglittle)r\   F)r]   )r   r   )r   rO  )r   rO  )r   rN  )r   rN  )r   sumre  r^   r_   r   r	   r`   r   r   ri   r   r  rI  r   r   float)r   r!   r  Zexpected_dtr   r   r   test_precision_loss  s    ,
rR  )0r2  r   r  r  r   r7  r    rg  r   r4  Znumpyri   r  Zpandas.core.dtypes.commonr   r	  r   Zpandas._testingZ_testingr^   Zpandas.core.framer   r   Zpandas.io.parsersr   Zpandas.io.statar   r   r	   r
   r   r   r   r!  r   r   r%   r&   r"  r#  r,  r<  r?  rC  rG  rH  rJ  rR  r   r   r   r   <module>   s\   
$               
" 
