gL iUdZddlmZddlmZddlmZmZddlmZddl Z ddl Z ddl Z ddl m Z mZmZmZmZmZddlZddlZddlmZdd lmZdd lmZdd lmZmZmZm Z dd l!m"Z"m#Z#dd l$m%Z%ddl&m'Z'ddl(m)Z)m*Z*m+Z+ddl,m-Z-ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5ddl6m7Z7ddl8m9Z9ddl:m;Z;ddlm?Z?ddl@mAZAer$ddlBmCZCmDZDddlEmFZFddl mGZGddlHmIZImJZJmKZKmLZLmMZMmNZNdZOdZPdZQdZRd ZSd!ZTd"ePd#eQd#eRd#eSd#e?d$d%zd#e?d&d'eTd(ZUd)ePd#eQd*ZVd+ePd#eQd#eRd#e?d$d#e?d&d,eTd# ZWgd-ZXed.d/d/ZYd0eZd1<dad2Z[dad3Z\d4Z]d0eZd5<d6Z^d0eZd7<d8Z_d0eZd9<d:Z`d0eZd;<dZbGd?d@ZcGdAdBecZdGdCdDZeGdEdFZfGdGdHefejZhe"eUdIdIddJdIddIddJdKddL dcdMZidddNZjdedOZkdfdPZldgdQZmdhdRZn di djdSZoe#e?d&e?dTdUzVGdWdXefZpdkdYZqdldZZrGd[d\ZsGd]d^epZtGd_d`etZuy)ma Module contains tools for processing Stata files into DataFrames The StataReader below was originally written by Joe Presbrey as part of PyDTA. It has been extended and improved by Skipper Seabold from the Statsmodels project who also developed the StataWriter and was finally added to pandas in a once again improved version. You can find more information on http://presbrey.mit.edu/PyDTA and https://www.statsmodels.org/devel/ ) annotations)abc)datetime timedelta)BytesION)IO TYPE_CHECKINGAnyStrCallableFinalcast)lib) infer_dtype)max_len_string_array)CategoricalConversionWarningInvalidColumnNamePossiblePrecisionLossValueLabelTypeMismatch)Appenderdoc)find_stack_level)ExtensionDtype) ensure_objectis_numeric_dtypeis_string_dtype)CategoricalDtype) Categorical DatetimeIndexNaT Timestampisna to_datetime to_timedelta) DataFrame)Index) RangeIndex)Series) _shared_docs) get_handle)HashableSequence) TracebackType)Literal)CompressionOptionsFilePath ReadBufferSelfStorageOptions WriteBufferzVersion of given Stata file is {version}. pandas supports importing versions 105, 108, 111 (Stata 7SE), 113 (Stata 8/9), 114 (Stata 10/11), 115 (Stata 12), 117 (Stata 13), 118 (Stata 14/15/16),and 119 (Stata 15/16, over 32,767 variables).zconvert_dates : bool, default True Convert date variables to DataFrame time values. convert_categoricals : bool, default True Read value labels and convert columns to Categorical/Factor variables.aindex_col : str, optional Column to set as index. convert_missing : bool, default False Flag indicating whether to convert missing values to their Stata representations. If False, missing values are replaced with nan. If True, columns containing missing values are returned with object data types and missing values are represented by StataMissingValue objects. preserve_dtypes : bool, default True Preserve Stata datatypes. If False, numeric data are upcast to pandas default types for foreign data (float64 or int64). columns : list or None Columns to retain. Columns will be returned in the given order. None returns all columns. order_categoricals : bool, default True Flag indicating whether converted categorical data are ordered.zzchunksize : int, default None Return StataReader object for iterations, returns chunks with given number of lines.z=iterator : bool, default False Return StataReader object.zNotes ----- Categorical variables read through an iterator may not have the same categories and dtype. This occurs when a variable stored in a DTA file is associated to an incomplete set of value labels that only label a strict subset of the values.a> Read Stata file into DataFrame. Parameters ---------- filepath_or_buffer : str, path object or file-like object Any valid string path is acceptable. The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. A local file could be: ``file://localhost/path/to/table.dta``. If you want to pass in a path object, pandas accepts any ``os.PathLike``. By file-like object, we refer to objects with a ``read()`` method, such as a file handle (e.g. via builtin ``open`` function) or ``StringIO``.  decompression_optionsfilepath_or_bufferstorage_optionsz Returns ------- DataFrame or pandas.api.typing.StataReader See Also -------- io.stata.StataReader : Low-level reader for Stata data files. DataFrame.to_stata: Export Stata data files. a Examples -------- Creating a dummy stata for this example >>> df = pd.DataFrame({'animal': ['falcon', 'parrot', 'falcon', 'parrot'], ... 'speed': [350, 18, 361, 15]}) # doctest: +SKIP >>> df.to_stata('animals.dta') # doctest: +SKIP Read a Stata dta file: >>> df = pd.read_stata('animals.dta') # doctest: +SKIP Read a Stata dta file in 10,000 line chunks: >>> values = np.random.randint(0, 10, size=(20_000, 1), dtype="uint8") # doctest: +SKIP >>> df = pd.DataFrame(values, columns=["i"]) # doctest: +SKIP >>> df.to_stata('filename.dta') # doctest: +SKIP >>> with pd.read_stata('filename.dta', chunksize=10000) as itr: # doctest: +SKIP >>> for chunk in itr: ... # Operate on a single chunk, e.g., chunk.mean() ... pass # doctest: +SKIP zReads observations from Stata file, converting them into a dataframe Parameters ---------- nrows : int Number of lines to read from data file, if None read whole file. z Returns ------- DataFrame zClass for reading Stata dta files. Parameters ---------- path_or_buf : path (string), buffer or path object string, path object (pathlib.Path or py._path.local.LocalPath) or object implementing a binary read() functions. z ) %tc%tC%td%d%tw%tm%tq%th%tyr stata_epochctjjtjjctjt dddz j tjt dddz j dzdzdzdzdzdzd"fd }d"fd }d"fd }t j|}d }|jrd }d |j|<|jt j}|jd rt}|}|||d } n|jdr=tjdt!t#|t$} |r t&| |<| S|jdrt}|} ||| d} n+|jdr(tj|dzz} |dzdz} || | } n|jdr(tj|dzz} |dzdz} || | } n|jdr+tj|dzz} |dzdzdz} || | } n}|jdr+tj|dzz} |dzdzdz} || | } nA|jdr!|} t j(|}|| |} nt+d |d!|r t&| |<| S)#a Convert from SIF to datetime. https://www.stata.com/help.cgi?datetime Parameters ---------- dates : Series The Stata Internal Format date to convert to datetime according to fmt fmt : str The format to convert to. Can be, tc, td, tw, tm, tq, th, ty Returns Returns ------- converted : Series The converted dates Examples -------- >>> dates = pd.Series([52]) >>> _stata_elapsed_date_to_datetime_vec(dates , "%tw") 0 1961-01-01 dtype: datetime64[ns] Notes ----- datetime/c - tc milliseconds since 01jan1960 00:00:00.000, assuming 86,400 s/day datetime/C - tC - NOT IMPLEMENTED milliseconds since 01jan1960 00:00:00.000, adjusted for leap seconds date - td days since 01jan1960 (01jan1960 = 0) weekly date - tw weeks since 1960w1 This assumes 52 weeks in a year, then adds 7 * remainder of the weeks. The datetime value is the start of the week in terms of days in the year, not ISO calendar weeks. monthly date - tm months since 1960m1 quarterly date - tq quarters since 1960q1 half-yearly date - th half-years since 1960h1 yearly date - ty years since 0000 rArBic |jkr&|jkDrtd|z|zdSt|dd}t t ||Dcgc]\}}t ||dc}}|Scc}}w)z Convert year and month to datetimes, using pandas vectorized versions when the date range falls within the range supported by pandas. Otherwise it falls back to a slower but more robust method using datetime. dz%Y%mformatindexNrBrK)maxminr"getattrr'zipr)yearmonthrKymMAX_YEARMIN_YEARs U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/pandas/io/stata.pyconvert_year_month_safezD_stata_elapsed_date_to_datetime_vec..convert_year_month_safe!st 88: TXXZ(%:sTzE1&A AD'40ET59IJA8Aq!,JRWX XJsA= c J|jdz kr.|jkDrt|dt|dzSt |dd}t ||Dcgc](\}}t |ddtt|z*}}}t|| Scc}}w) z{ Converts year (e.g. 1999) and days since the start of the year to a datetime or datetime64 Series rB%YrIdunitrKNdaysrL) rMrNr"r#rOrPrrintr')rQr_rKrSr[valuerUrVs rWconvert_year_days_safezC_stata_elapsed_date_to_datetime_vec..convert_year_days_safe.s 88:A &488:+@tD1LC4PP PD'40EGJ4QU?Cq!Aq!I3q6$::E%u- -s"-Bc t|dd}|dk(rX|jkDs|jkr|Dcgc]}|tt |z}}t ||S|dk(r[|jkDs|j kr@|Dcgc]}|tt |dzz}}t ||St d t|}t|| }||zScc}wcc}w) z Convert base dates and deltas to datetimes, using pandas vectorized versions if the deltas satisfy restrictions required to be expressed as dates in pandas. rKNr[r^rLmsrF) microsecondszformat not understoodr\) rOrMrNrr`r' ValueErrorr"r#) basedeltasr]rKr[values MAX_DAY_DELTA MAX_MS_DELTA MIN_DAY_DELTA MIN_MS_DELTAs rWconvert_delta_safez?_stata_elapsed_date_to_datetime_vec..convert_delta_safe<s . 3;zz|m+vzz|m/KAGHA$A!77HHfE22 T\zz|l*fjjl\.ILRGHD93q6D=BBfE2245 54 f40f}Is C4"C9FTg?r8tcrdr9tCz9Encountered %tC format. Leaving in Stata Internal Format. stackleveldtype)r:tdr;r[r[r<tw4r=tm r>tqr?thr@tyz Date fmt  not understood)returnr')r rNrQrMrr_npisnanany_valuesastypeint64 startswithrCwarningswarnrr'objectr ones_likerf)datesfmtrXrbrnbad_locshas_bad_valuesrgrd conv_datesr_rQrR quarter_month first_monthrjrkrUrlrmrVs @@@@@@rW#_stata_elapsed_date_to_datetime_vecrs\#++Y]]-?-?Hh]]XdAq%99??M]]XdAq%99??M 2%,t3L 2%,t3L Y .2xxHN||~"% h LL "E ~~m$ 'b$7  & G') E0 #&Jx  0 1'dC8   &%2+- a+D$7  &%2+-q ,T59  &%1*,a!+ ,T=A  &%1*,a!#,T59  &ll5) ,T;? 9SE9::" 8 c |j ddz  d# d$ fd }t|}|j |jrPtj|j drt t|j|<nt|j|<|dvr||d}|jdz }n|d vr#tjd t |}n\|d vr||d}|jz}n=|d vr<||dd}d|jtjz z|jdzz}n|dvr;||d}d|jtjz z|jzdz }n|dvr>||d}d|jtjz z|jdz dzz}n||dvrN||d}d|jtjz z|jdkDj!t"z}n*|dvr||d}|j}nt%d|dt'|t(j*d}t-j.dd d!}|||<t'| d"S)%aO Convert from datetime to SIF. https://www.stata.com/help.cgi?datetime Parameters ---------- dates : Series Series or array containing datetime or datetime64[ns] to convert to the Stata Internal Format given by fmt fmt : str The format to convert to. Can be, tc, td, tw, tm, tq, th, ty l"R:rFFci}tj|jdr|rP|ttj dz }|j jtjdz|d<|s|r=t|}|jj|d<|jj|d<|r%|j jtjt|ddj jtjz }| z|d <nt|d d k(r|r9|j tz }d fd }tj |} | ||d<|r<|j#d} | j dz|d<| j |ddzz |d<|r0dd} tj | } | ||d <n t%dt'|S)NMnsrFdeltarQrRrZrIr_Fskipnarc\|jzd|jzz|jzS)Ni@B)r_secondsre)x US_PER_DAYs rWfzC_datetime_to_stata_elapsed_vec..parse_dates_safe..fs)%.1991DDq~~UUrc:d|jz|jzS)NrH)rQrRrs rWzJ_datetime_to_stata_elapsed_vec..parse_dates_safe..s3.parse_dates_safe..gs A 66<<.parse_dates_safes  ??5;; ,"Y{%;%C%CD%II '//44RXX>$F' t*51 &,,11& '--33' "]]//9KfId='$$rxx.) '*4& u - ; 3VLLOuX' "[[)IJ &..#5& '//!F)c/A' =LLOeH& 7  %((rrroT)rrqz'Stata Internal Format tC not supported.rs)r:rwrx)rQr_rzr{r|)rQr~rBrrrrrrrFormat z! is not a known Stata date format)rvcopy||jArtCd|d||j3} | tj<k(r+| |kDr&||j'tj||<n-| tjk(r| |kDrtCd|d| d|d|s|jAstDjF||jjH} | |j.||f<|r$tKjL|tNtQ|S) a- Checks the dtypes of the columns of a pandas DataFrame for compatibility with the data types and ranges supported by Stata, and converts if necessary. Parameters ---------- data : DataFrame The DataFrame to check and convert Notes ----- Numeric columns in Stata must be one of int8, int16, int32, float32 or float64, with some additional value restrictions. int8 and int16 columns are checked for violations of the value restrictions and upcast if needed. int64 data is not usable in Stata, and so it is downcast to int32 whenever the value are in the int32 range, and sidecast to float64 when larger than this range. If the int64 values are outside of the range of those perfectly representable as float64 values, a warning is raised. bool columns are cast to int8. uint columns are converted to int of the same size if there is no loss in precision, otherwise are upcast to a larger type. uint64 is currently not supported since it is concerted to object in a DataFrame.  I,,RXX6S bhh xCy}}&$s)--/F*B I,,RXX6S bhh S :-$s)--/[2P I,,RXX6S  I,,RZZ8S 9==?e+tCy}}(/J+227IFB rzz2::. .xxS "&&( cU#EEIMMOE "u{': I,,RZZ8S "**$;&$!#& !,@@cAUAUV.6s*+QH7R   !') Krc6eZdZdZ d ddZddZd dZy) StataValueLabelz Parse a categorical column and prepare formatted output Parameters ---------- catarray : Series Categorical Series to encode encoding : {"latin-1", "utf-8"} Encoding to use for value labels. c|dvr td|j|_||_|jj }t ||_|jy)Nlatin-1utf-8%Only latin-1 and utf-8 are supported.) rfrlabname _encodingcat categories enumerate value_labels_prepare_value_labels)selfcatarrayencodingrs rW__init__zStataValueLabel.__init__sS / /DE E}} !\\,, %j1 ""$rcLd|_g|_d|_tjgtj |_tjgtj |_d|_g}g}|jD]}|d}t|tsLt|}tjtj|j t"t%|j'|j(}|j+|j|xjt|dzz c_|j+|d|jj+||xjdz c_|jdkDr t-dtj|tj |_tj|tj |_dd|jzzd|jzz|jz|_y ) zEncode value labels.rrurBrsi}zaStata value labels for a single variable must have a combined length less than 32,000 characters.rN)text_lentxtnrarrayroffvallenr rstrrrrrJrrrencoderappendrf)r offsetsrivlcategorys rWr z%StataValueLabel._prepare_value_labelss "88Bbhh/88Bbhh/  ## B$&qEHh,x= ,33DLLA*/1  t~~6H NN4== ) MMS]Q. .M MM"Q% HHOOH % FFaKF  ==5 F  88G288488F"((31tvv:%DFF 2T]]Brc|j}t}d}|jtj|dz|j t |jddj|}|dvrdnd}t||dz}|j|tdD]'}|jtjd |)|jtj|dz|j|jtj|dz|j|jD]*}|jtj|dz|,|jD]*} |jtj|dz| ,|jD]} |j| |z|j!S) a! Generate the binary representation of the value labels. Parameters ---------- byteorder : str Byte order of the output Returns ------- value_label : bytes Bytes containing the formatted value label iN )rutf8rBrc)rrwriterpackrrrr _pad_bytesrangerrrrrgetvalue) r  byteorderr bio null_byterlab_lenr offsetratexts rWgenerate_value_labelz$StataValueLabel.generate_value_labels>>i  &++i#otxx89dll#CR(//9 (99"sWgk2 'q 3A IIfkk#y1 2 3 &++i#otvv67 &++i#ot}}=>hh .6s !r)key)rfrrsorteditemsr r )r rr r s rWrzStataNonCatValueLabel.__init__*sP / /DE E !"    n  ""$rNr1)rrr dict[float, str]r r2rr3)r6r7r8r9rr:rrWr<r<s7 "1: %%'%. %  %rr<ceZdZUdZiZded<dZded<eD])Zdee<edd D]Z de d e zzee ez<+d Z d ed <e jdddZded<ed D]uZ e jde dZdee<e dkDreexxe d e zz cc<e jde j dedezZe j deZ wdZd ed<e jdddZed D]uZ e jdedZdee<e dkDreexxe d e zz cc<e jde j dedezZe j deZwddde jde de jdeddZded<d&dZed'dZed(d Zd'd!Zd'd"Zd)d#Zed*d$Zy%)+ra An observation's missing value. Parameters ---------- value : {int, float} The Stata missing value code Notes ----- More information: Integer missing values make the code '.', '.a', ..., '.z' to the ranges 101 ... 127 (for int8), 32741 ... 32767 (for int16) and 2147483621 ... 2147483647 (for int32). Missing values for floating point data types are more complex but the pattern is simple to discern from the following table. np.float32 missing values (float in Stata) 0000007f . 0008007f .a 0010007f .b ... 00c0007f .x 00c8007f .y 00d0007f .z np.float64 missing values (double in Stata) 000000000000e07f . 000000000001e07f .a 000000000002e07f .b ... 000000000018e07f .x 000000000019e07f .y 00000000001ae07f .z rBMISSING_VALUES)er bases.rB`r5 float32_basez?B\Q "v{{43 4>L%= 6==&IJ1ML 2Y3fmmD,/2!s q5 3 3rAv; . !FMM#{v{{4'=>qALP "v{{3 2 3 6==|4Q7 6==|4Q7 "/     '   rrceZdZddZy) StataParserc ttddDcgc]}|tjd|fc}dtjtjfdtjtj fdtjtj fdtjtjfdtjtjfgz|_ tjtjtjtjtjtjtjtj tjtj tjtjd |_ tttdtd z|_d d d dddd |_d}d}d}d}dddtjt!j"d|dtjt!j"d|dftjt!j"d|dtjt!j"d|dfd|_dddddd|_dddtjt!j"dd dtjt!j"dd!dd|_d"d#d$d%d&d'd(|_hd)|_ycc}w)*NrBS)bhlfdQr[rlhrisrsr)rrH)rr)rrrrr)rirrrr[)bilfrHrErFrGrLri1i2i4f4f8u8)rirrrr[r><str#_N_b_ndoifin_pi_rc_seendforr`NULL_allbytecaseelseenumgotolongquadstrLwith_coef_cons_pred_skiprbreakcatchclassconstrlocalshortusingdeletedoubleexportfriendglobalinlinepragmabooleancomplexdefaulttypedefvirtualcontinuedelegateexplicitexternalfunctiontypename aggregate colvector eltypedef protected rowvector)dictr(rrvrrrrr DTYPE_MAPr DTYPE_MAP_XMLlisttupleTYPE_MAP TYPE_MAP_XMLrr VALID_RANGEOLD_TYPE_MAPPINGrDNUMPY_TYPE_MAPRESERVED_WORDS)r r  float32_minr float64_minrs rWrzStataParser.__init__s -21c] ;aAaS'" # ;bhhrww'(bhhrxx()bhhrxx()bhhrzz*+bhhrzz*+    88BHH%88BJJ'88BJJ'88BHH%88BHH%88BGG$ 3 U5:.w?@  * ) 9 9  * 6=={;A>? 6=={;A>?  6=={;A>? 6=={;A>?   ! FMM$0CDQGH d$GHK    = [ d?d%Z(e)e* d@ dAd&Z+dBd'Z,dCd(Z-dDd)Z. dEd*Z/e0d9d+Z1e0d9d,Z2dFd-Z3dGd.Z4xZ5S)H StataReaderz IO[bytes] _path_or_bufc t |||_||_||_||_||_||_||_||_ | |_ | |_ d|_ | |_ d|_d|_|jd|_ n t!| t"r| dkr t%dd|_d|_d|_d|_d|_d|_d|_d|_t7t8j:|_y)NrFrBrz.chunksize must be a positive integer when set.)superr_convert_dates_convert_categoricals _index_col_convert_missing_preserve_dtypes_columns_order_categoricals_original_path_or_buf _compression_storage_optionsr _chunksize_using_iterator_enteredrr`rf _close_file_missing_values_can_read_value_labels_column_selector_set_value_labels_read _data_read_dtype _lines_read_set_endiannesssysr*_native_byteorder) r  path_or_buf convert_datesconvert_categoricals index_colconvert_missingpreserve_dtypescolumnsorder_categoricals chunksize compressionr7 __class__s rWrzStataReader.__init__es ,%9"# / / #5 %0"' /#$ ?? "DOIs+yA~MN N7;$&+#$)!"''+ !0!?rc>t|ds|jyy)zK Ensure the file has been opened and its header data read. rN)hasattr _open_filerVs rW _ensure_openzStataReader._ensure_openst^, OO -rcZ|js$tjdtt t |j d|jd|j}t|jdr=|jjr#|j|_ |j|_nN|5t|jj!|_ ddd|jj|_|j#|j%y#1swYExYw)z^ Open the file (with compression options, etc.), and read header information. zStataReader is being used without using a context manager. Using StataReader as a context manager is the only supported method.rsrbF)r7is_textrseekableN)rrrResourceWarningrr)rrrrhandlerrcloserrread _read_header _setup_dtype)r handless rWrzStataReader._open_files}} MMW+-     & &  11))   7>>: .7>>3J3J3L 'D &}}D  C$+GNN,?,?,A$B! C#0066D     C Cs 4)D!!D*cd|_|S)zenter context managerT)rrVs rW __enter__zStataReader.__enter__s  rc>|jr|jyyrZ)r)r exc_type exc_value tracebacks rW__exit__zStataReader.__exit__s        rctjdtt|jr|j yy)zClose the handle if its open. .. deprecated: 2.0.0 The close method is not part of the public API. The only supported way to use StataReader is to use it as a context manager. zThe StataReader.close() method is not part of the public API and will be removed in a future version without notice. Using StataReader as a context manager is the only supported method.rsN)rr FutureWarningrrrVs rWrzStataReader.closes=   S ')          rc@|jdkrd|_yd|_y)zC Set string encoding which depends on file version vrrN)_format_versionrrVs rW _set_encodingzStataReader._set_encodings    # %&DN$DNrcftjd|jjddS)NrirBrrrrrrVs rW _read_int8zStataReader._read_int8)}}S$"3"3"8"8";<rr{ r rrB)+rrr`rrf_version_errorrJrr!r"r&_nvar _get_nobs_nobs_get_data_label _data_label_get_time_stamp _time_stampr._seek_vartypes_seek_varnames_seek_sortlist _seek_formats_seek_value_label_names_get_seek_variable_labels_seek_variable_labels_data_location _seek_strls_seek_value_labels _get_dtypes_typlist _dtyplistseek _get_varlist_varlistr3_srtlist _get_fmtlist_fmtlist _get_lbllist_lbllist_get_variable_labels_variable_labelsrVs rWr6zStataReader._read_new_header se r""4#4#4#9#9!#<=    6^224;O;O2PQ Q  r"!%!2!2!7!7!:f!D## r"#'#7#73#>D   DDUDUDW  q!^^%  r"//1 r"//1 r" q! q!"..025"..025"..025!--/!3'+'7'7'9B'>$&*%C%C%E" q!"..014++-1"&"2"2"4r"9(,(8(89L9L(M% t~ t223))+  t223..tzzA~>sC  t112))+  t;;<))+  t99: $ 9 9 ;rc|jj|g}g}t|jD]}|j }|dkr,|j ||j t |D |j |j||j |j|||fS#t$r}td|d|d}~wwxYw)Ncannot convert stata types []) rr`r(rLr"rrrrKeyErrorrf)r  seek_vartypestyplistdtyplist_typerrs rWr]zStataReader._get_dtypesEs }-tzz" UA##%Cd{s#C)UNN4#4#4S#9:OOD$6$6s$;< U   U$'CC5%JKQTTUs:? T..33C89G ! !C 'BG BS=> T..33B78GCH BS=> T..33B78Gs1D1D1D c`|jdk\r|jS|jS)Nr)rr(r&rVs rWrMzStataReader._get_nobss.   3 &$$& &$$& &rc|jdk\r:|j}|j|jj |S|jdk(r:|j }|j|jj |S|jdkDr*|j|jj dS|j|jj dS)Nrr;rrr!)rr"ryrrrr strlens rWrOzStataReader._get_data_labels   3 &&&(F<< 1 1 6 6v >? ?  ! !S (__&F<< 1 1 6 6v >? ?  ! !C '<< 1 1 6 6r :; ;<< 1 1 6 6r :; ;rc|jdk\r:|j}|jj|j dS|jdk(r:|j}|j |jj|S|jdkDr*|j |jjdSt )Nrrr;r)rrrrdecoderyrfrs rWrQzStataReader._get_time_stamps   3 &__&F$$))&188A A  ! !S (__&F<< 1 1 6 6v >? ?  ! !C '<< 1 1 6 6r :; ;, rc|jdk(r=|jjd|jd|jzzdzdzS|jdk\r|j dzSt )Nr;rrvr)rrrrWrLr.rfrVs rWrXz%StataReader._get_seek_variable_labelsss   3 &    " "1 %//2 ?CbH2M M  ! !S (##%* *, rc t|d|_|jdvr)ttj |j|j |j dk(rdnd|_|j |_|jjd|j|_ |j|_|j|_|j#|_|jdkDr<|jj|jDcgc] }t|}}n|jj|j}t'j(|t&j*}g}|D]C}||j,vr|j/|j,|0|j/|d z E |Dcgc]}|j0|c}|_ |Dcgc]}|j8|c}|_|jdkDrQt=|jD cgc],} |j?|jjd.c} |_ nPt=|jD cgc],} |j?|jjd.c} |_ |jC|jdzdd|_"|jG|_$|jK|_&|jO|_(|jdkDrc |j } |jdkDr|jS}n|jU}| dk(rn|jj|b|jjW|_,ycc}wcc}w#t$rC}d j5|D cgc] } t7| ncc} wc} } td | d |d}~wwxYwcc}w#t$rC}d j5|D cgc] } t7| ncc} wc} } td | d |d}~wwxYwcc} wcc} w)Nr)rrror}rsr=rBr@rArru,rlrmzcannot convert stata dtypes [rvrHrJr)-r`rrfrKrJrrr! _filetyperrr"rLrMrNrOrPrQrRr frombufferrrrrr^joinrrr_r(ryrbr3rcrdrerfrgrhrir,r*tellrZ)r r8r$rpbuftyplistbtprsrtr invalid_typesinvalid_dtypesrr data_typedata_lens rWr7zStataReader._read_old_headers":a=1   'J J^224;O;O2PQ Q !%!2c!9#s* q!&&( ^^% //1//1   # %'+'8'8'='=djj'IJ!s1vJGJ##((4C}}S9HG -...NN4#8#8#<=NN28,  -  W;BCCT]]3/CDM Y=DEcdnnS1EDN   # %BG BS=> T..33B78DM BGtzzAR<= T..33A67DM..tzzA~>sC ))+ ))+ $ 9 9 ;   # % OO- ''#-#//1H#//1H>!!&&x0#//446oKD WHHg%>c!f%>%>?M;M?!LMSV V WF Y XXw&?!s1v&?&?@N<^P%P P%1Q41Q9 O PP)O< ;PP P%% Q1.Q,=Q Q,,Q1c|j |jSg}t|jD]n\}}||jvrBt t |}|j d||j|j|fV|j d|d|fptj||_|jS)z"Map between numpy and state dtypessrs) rrr^rr rrr!rrv)r dtypesr rss rWrzStataReader._setup_dtypes ;; ";;  . 4FAsd)))3n 1#w4??*;D} | |d z kr|| d zn|} |j| || | |j||| <@|jdk\r|jjd >)NrTr;s#';'',,Q/G6#'5##s*,,t'8'8'='=b'AB,,t'8'8'='=c'BC    " "1 %!!#A&&(F--!!&&q1u-7H5KSTC--!!&&q1u-7H5KSTCCBb'Cb'C##((0C.0D " "7 +1X $%AIc!a%j6:>,,A%;&&w/A7  ##s*!!&&q)Crc|jj|jddi|_ |jj ddk7ry|j dk(r|j }ns|jj d}|j dk(rdnd}|jd k(r|d ||d d|z z}n|d ||d |zdz}tjd |d }|j}|j}|jj |}|d k(r|d dj|j}n t|}||jt|<6)N0rrsGSOr;r~rrrArrrrJ)rr`r[GSOrrr(r!rrrr&rrr)r v_orv_sizerslengthva decoded_vas rW _read_strlszStataReader._read_strlsWsX t//09  %%a(F2##s*'')'',,R0"22c9q??c)a-#a2;*@@Ca-#q6zn*==CmmC-a0""$C&&(F""''/Bcz"X__T^^< !W !+DHHSX 3rcHd|_|j|jS)NTnrows)rrrrVs rW__next__zStataReader.__next__vs#yyty//rcB| |j}|j|S)a Reads lines from Stata file and returns as dataframe Parameters ---------- size : int, defaults to None Number of lines to read. If None, reads whole file. Returns ------- DataFrame r)rr)r sizes rW get_chunkzStataReader.get_chunkzs# <??Dyyty$$rc  |j| |j}| |j}| |j}| |j}| |j }| |j }| |j}| |j}|jdk(r|dk(rd|_ d|_ t|j} t| jD]V\} } |j| } t!| t"j$s0| j&dk7s@| | j)| | | <X||j+| |} | S|j,dk\r#|j.sd|_ |j1|j2J|j2} |j|j4z | j6z}|| j6z}t9||}|dkr|r|j;t<|j4| j6z}|j>jA|jB|zt9||j|j4z }t#jD|j>jG|| |}|xj4|z c_|j4|jk(rd|_ d|_ |jH|jJk7r7|jMjO|j$jQ}|r|j;tS|dk(rt|j} n/tjT|} tW|j| _|(tY|j4|z |j4| _-||j+| |} t]| |j^D]7\} }t!|t`s| | jc|jd| | <9|jg| } t|jD cgc] \} }| |  }} }t#j$th}|D]e}| jjdd|fj$} | ||j|fvs4| jm|| jjdd|fj)| g|jo| |} |rct|jpD]K\} tsfdttDs| jm| tw| jjdd| fM|r7|j,dkDr(|jy| |jz|j||} |s^g}d }| D]4} | | j$} | t#j$t"j~t#j$t"jfvr&t#j$t"j} d}n| t#j$t"jt#j$t"jt#j$t"jfvr%t#j$t"j} d}|j| | | j)| f7|rtjt|} | | j| j|} | Scc}} w) NrT)rrsr;rc3@K|]}j|ywrZ)r).0date_fmtrs rW z#StataReader.read..sNHs~~h/NsrF)KrrrrrrrrrNrrr$rbrrr_rrrvcharr_do_select_columnsrrrrrrrNr StopIterationrr`rZrrr!rbyteswapr newbyteorderr from_recordsr%r&rKrPr^r`rry _insert_strlsrilocisetitem_do_convert_missingrer _date_formatsr_do_convert_categoricalsrrgfloat16rrrrrrr from_dictr set_indexpop)r rrrrrrrrrr rdtrv max_read_lenread_lenr. read_linesraw_datarsdtyp valid_dtypes object_typeidx retyped_dataconvertrs @rWrzStataReader.reads    //M  '#'#=#=  ""33O  ""33O ?mmG  %!%!9!9   I =JJE JJ!O!*.D '"DOT]]3D#DLL1 93^^A&b"((+ww#~$(I$4$4R$8S  9 "..tW=K  C '$2I2I*.D '    {{&&&  T%5%55G 5>>)x. q=$'') !!ENN2 t22V;< T-=-= => ==    " "8 ,E  J&   tzz )*.D '"DO ??d44 4((*//0K0K0MNH   # # % x=A T]]3D))(3D /DL  #  :-t/?/?DJ  **49DD$--0 :HC#s# IOODLL9S  :!!$'*34>>)BWgadFVW Whhv&  DCIIaf%++E[$..*=>> c499QV#4#;#;E#BC D ''o> #DMM2 3N NNMM>tyyAPST  D$8$83$>00d,,dmm=ODLG DS RXXbjj1288BJJ3GHHHHRZZ0E"GHHRWW%HHRXX&HHRXX& HHRXX.E"G##S$s)*:*:5*A$BC D **4 +=>  >>$((9"56D WXs & [01[0ci}tt|jD]}|j|}||jvr"t t |}|j|\}}|jdd|f}|j} | |k| |kDz} | js|rtjtj| d} tj|| d\} } t|t}t!| D]'\}}t#|}| | |k(}||j|<)n|j$}|tj&tj(fvrtj(}t||}|jj*ds|j-}tj.|j| <|||<|r*|j1D]\}}|j3|||S)NrT)return_inverseru WRITEABLE)r(rrr^rr rrrrrnonzeroasarrayuniquer'rrrrvrrflagsrnanrAr)r rr replacementsr rnminnmaxseriessvalsmissing missing_locumissing umissing_loc replacementjumrrrvrras rWrzStataReader._do_convert_missing#s s4<<()% *A--"C$***sC.C))#.JD$YYq!t_FNNEt| 5G;;= jjG)<=a@ )+6'?SW)X&,$V6: &x0:EAr$5b$9M%la&78C,9K$$S) :  RZZ 88JJE$V59 "**00=#."2"2"4K02vv ##G,)LOK% *L *002 * U c5) * rc 0t|drt|jdk(r|St|jD]R\}}|dk7r |j ||j dd|fDcgc]}|jt|c}T|Scc}w)Nrrr)rrrrr^rrr)r rr rsks rWrzStataReader._insert_strlsQstU#s488}'9K . JFAscz MM! !Q$H1dhhs1v.H I  J  Is)B c|js7t|}t|t|k7r td|j |j }|r(dj t|}td|g}g}g}g} |D]} |j j| } |j|j| |j|j| |j|j| | j|j| ||_ ||_ ||_ | |_ d|_||S)Nz"columns contains duplicate entriesz, zg}| jD],}||vr|j|||j|.nt|j!} | j#|} t'| |j*d }|j||fN|j|||fft/t1|d}|S#t$$rd}t'|dj)}t|j*|dkD}ddj-|z}d |d |d}t%||d}~wwxYw) zC Converts categorical columns to Categorical type. rsN)rorderedF)rrBzQ-------------------------------------------------------------------------------- r4z Value labels for column a 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: r)rPrrrkeysisinrallrrrrrrrrrirename_categoriesrfr' value_countsrKrr$r)r rvalue_label_dictrrcat_converted_datarlabelrrcolumn key_matchesinitial_categoriescat_datarrrtvc repeated_catsrepeatsr cat_seriess rWrz$StataReader._do_convert_categoricals{s KdG,= &--bl;&--h7 8"&biik!2J3 (99*EH&$HDJJUK "))3 *;<"))3S *:;{= <|01> /"3 7DDFB$("q&)9$:M- -0HHG   C%S/s2!3s#F G?AG::G?c:|j|jS)a Return data label of Stata file. Examples -------- >>> df = pd.DataFrame([(1,)], columns=["variable"]) >>> time_stamp = pd.Timestamp(2000, 2, 29, 14, 21) >>> data_label = "This is a data file." >>> path = "/My_path/filename.dta" >>> df.to_stata(path, time_stamp=time_stamp, # doctest: +SKIP ... data_label=data_label, # doctest: +SKIP ... version=None) # doctest: +SKIP >>> with pd.io.stata.StataReader(path) as reader: # doctest: +SKIP ... print(reader.data_label) # doctest: +SKIP This is a data file. )rrPrVs rW data_labelzStataReader.data_labels$ rc:|j|jS)z2 Return time stamp of Stata file. )rrRrVs rW time_stampzStataReader.time_stamps rct|jtt|j|jS)a Return a dict associating each variable name with corresponding label. Returns ------- dict Examples -------- >>> df = pd.DataFrame([[1, 2], [3, 4]], columns=["col_1", "col_2"]) >>> time_stamp = pd.Timestamp(2000, 2, 29, 14, 21) >>> path = "/My_path/filename.dta" >>> variable_labels = {"col_1": "This is an example"} >>> df.to_stata(path, time_stamp=time_stamp, # doctest: +SKIP ... variable_labels=variable_labels, version=None) # doctest: +SKIP >>> with pd.io.stata.StataReader(path) as reader: # doctest: +SKIP ... print(reader.variable_labels()) # doctest: +SKIP {'index': '', 'col_1': 'This is an example', 'col_2': ''} >>> pd.read_stata(path) # doctest: +SKIP index col_1 col_2 0 0 1 2 1 1 3 4 )rrrPrbrirVs rWvariable_labelszStataReader.variable_labelss,0 C t'<'<=>>rcR|js|j|jS)aX Return a nested dict associating each variable name to its value and label. Returns ------- dict Examples -------- >>> df = pd.DataFrame([[1, 2], [3, 4]], columns=["col_1", "col_2"]) >>> time_stamp = pd.Timestamp(2000, 2, 29, 14, 21) >>> path = "/My_path/filename.dta" >>> value_labels = {"col_1": {3: "x"}} >>> df.to_stata(path, time_stamp=time_stamp, # doctest: +SKIP ... value_labels=value_labels, version=None) # doctest: +SKIP >>> with pd.io.stata.StataReader(path) as reader: # doctest: +SKIP ... print(reader.value_labels()) # doctest: +SKIP {'col_1': {3: 'x'}} >>> pd.read_stata(path) # doctest: +SKIP index col_1 col_2 0 0 1 2 1 1 x 4 )rrrrVs rWr zStataReader.value_labelss%0&&  # # %%%%r) TTNFTNTNinferN)rFilePath | ReadBuffer[bytes]rrrrr str | NonerrrrrSequence[str] | Nonerrr int | Nonerr.r7StorageOptions | Nonerr3r4)rr1)r ztype[BaseException] | NonerzBaseException | NonerzTracebackType | Nonerr3)rr`)rr5)r2r`rztuple[int, ...])ror`rz,tuple[list[int | str], list[str | np.dtype]])rz list[str]rf)r8r5rr3)rrg)rr5rr)rr$rZ)rrrr$)NNNNNNNN)rrr bool | Nonerr rrrr rr rrrr rr$)rr$rrrr$rr$rr$)rr$r Sequence[str]rr$) rr$rdict[str, dict[float, str]]rr"rrrr$)rzdict[str, str])rr#)6r6r7r8_stata_reader_docr9rhrrrr rrrrrr"r&r(r*r,r.r0r3rr6r]rardrfrhrMrOrQrXr7rryrrrrr_read_method_docrrrrrrmrrrr  __classcell__rs@rWrr`sG #%) $ % $(,#' $*115/@1/@/@# /@  /@  /@/@&/@!/@/@(/@//@ /@b> ,((   $%@@RRRRRR@ .50%"!%),0 $'+'+(,*.UU#U* U  U % U%U&U(U U Un,\@LL6L L ! L  L\  (  ?6&rrTFr) rrrrrrrriteratorrr7c t|||||||||| |  } | s|r| S| 5| jcdddS#1swYyxYw)N) rrrrrrrrr7r)rr) r6rrrrrrrrr(rr7readers rW read_statar+s\ #1''-' F9 {{}s 9Acl|jdvry|jdvrytd|d)N)rAlittlerA)r@bigr@z Endianness r)lowerrf) endiannesss rWrrDs>_,    | +;zl/BCCrcrt|tr|d|t|z zzS|d|t|z zzS)zQ Take a char string and pads it with null bytes until it's length chars. r)rr5rrrs rWr'r'Ms@$g#d)!3444 &FSY./ //rcn|dvr#tjtjStd|d)zK Convert from one of the stata date formats to a type in TYPE_MAP. )rpr8rwr:ryr<r}r=rr>rr?rr@rz not implemented)rrvrNotImplementedError)rs rW_convert_datetime_to_stata_typer6Vs;  xx ##!GC50@"ABBrc i}|D]|}||jds d||z||<||vr&|j|j|||iLt|ts t d|j|||i~|S)N%z0convert_dates key must be a column or an integer)rupdaterKrr`rf)rvarlistnew_dictr?s rW_maybe_convert_to_int_keysr<osH7S!,,S1!$}S'9!9M#  '> OOW]]3/s1CD Ec3' !STT OOS-"45 67 Orc|jtjur*tt |j }t |dS|jtjury|jtjury|jtjury|jtjury|jtjurytd|d) a Convert dtype types to stata types. Returns the byte of the given ordinal. See TYPE_MAP and comments for an explanation. This is also explained in the dta spec. 1 - 244 are strings of this length Pandas Stata 251 - for int8 byte 252 - for int16 int 253 - for int32 long 254 - for float32 float 255 - for double double If there are dates to convert, then dtype will already have the correct type inserted. rBrxrwrvrurt Data type  not supported. r^robject_rrrrMrrrrrr5)rvr rs rW_dtype_to_stata_typerB}s" zzRZZ( fnn(EF8Q rzz ! rzz ! rxx  rxx  rww !Jug_"EFFrc|dkrd}nd}|ry|jtjurltt |j }||kDr.|dk\ryt tj|jdtt|dzdzS|tjk(ry|tjk(ry |tjk(ry |tjtj fvry t#d |d )a Map numpy dtype to stata's default format for this type. Not terribly important since users can change this in Stata. Semantics are object -> "%DDs" where DD is the length of the string. If not a string, raise ValueError float64 -> "%10.0g" float32 -> "%9.0g" int64 -> "%9.0g" int32 -> "%12.0g" int16 -> "%8.0g" int8 -> "%8.0g" strl -> "%9s" r;rkz%9sr8rBrz%10.0gz%9.0gz%12.0gz%8.0gr>r?)r^rrArrrrfrrJrrrMrrrrrr5)rvr  dta_version force_strl max_str_lenrs rW_dtype_to_default_stata_fmtrHs&S   zzRZZ' fnn(EF k !c! !>!E!Efkk!RSSSXq)**S00 "**  "**  "((  277BHH% %!Jug_"EFFrcompression_optionsfname)r7rIceZdZUdZdZdZded< d(dd d)fdZd*d Zd+d Z d,d Z d-d Z d-d Z d.dZ d/dZd-dZd0dZd1dZd.dZd.dZd.dZd.dZd.dZd.dZd.dZd.dZd.dZ d2 d3dZd.dZd.dZd.dZd.d Zd.d!Z d.d"Z!d-d#Z"d4d$Z#d5d%Z$e%d6d&Z&d7d'Z'xZ(S)8 StataWriterar A class for writing Stata binary dta files Parameters ---------- fname : path (string), buffer or path object string, path object (pathlib.Path or py._path.local.LocalPath) or object implementing a binary write() functions. If using a buffer then the buffer will not be automatically closed after the file is written. data : DataFrame Input to save convert_dates : dict Dictionary mapping columns containing datetime types to stata internal format to use when writing the dates. Options are 'tc', 'td', 'tm', 'tw', 'th', 'tq', 'ty'. Column can be either an integer or a name. Datetime columns that do not have a conversion type specified will be converted to 'tc'. Raises NotImplementedError if a datetime column has timezone information write_index : bool Write the index to Stata dataset. byteorder : str Can be ">", "<", "little", or "big". default is `sys.byteorder` time_stamp : datetime A datetime to use as file creation date. Default is the current time data_label : str A label for the data set. Must be 80 characters or smaller. variable_labels : dict Dictionary containing columns as keys and variable labels as values. Each label must be 80 characters or smaller. {compression_options} .. versionchanged:: 1.4.0 Zstandard support. {storage_options} value_labels : dict of dicts Dictionary containing columns as keys and dictionaries of column value to labels as values. The combined length of all labels for a single variable must be 32,000 characters or smaller. .. versionadded:: 1.4.0 Returns ------- writer : StataWriter instance The StataWriter instance has a write_file method, which will write the file to the given `fname`. Raises ------ NotImplementedError * If datetimes contain timezone information ValueError * Columns listed in convert_dates are neither datetime64[ns] or datetime * Column dtype is not representable in Stata * Column listed in convert_dates is not in DataFrame * Categorical label contains more than 32,000 characters Examples -------- >>> data = pd.DataFrame([[1.0, 1]], columns=['a', 'b']) >>> writer = StataWriter('./data_file.dta', data) >>> writer.write_file() Directly write a zip file >>> compression = {{"method": "zip", "archive_name": "data_file.dta"}} >>> writer = StataWriter('./data_file.zip', data, compression=compression) >>> writer.write_file() Save a DataFrame with dates >>> from datetime import datetime >>> data = pd.DataFrame([[datetime(2000,1,1)]], columns=['date']) >>> writer = StataWriter('./date_data_file.dta', data, {{'date' : 'tw'}}) >>> writer.write_file() rDrr2rNr c t |||_|in||_||_||_||_||_| |_g|_ tjgt|_ | |_d|_i|_|j#|| |_|t&j(}t+||_||_tj0tj2tj4d|_y)Nru)rvrurt)rrrr _write_indexrRrPri_non_cat_value_labels _value_labelsrrr_has_value_labelsr _output_file_converted_names_prepare_pandasr7rr*rr!_fnamerrrtype_converters) r rJrr write_indexr*rrrrr7r rs rWrzStataWriter.__init__$ s  $1$9b}'%% /%1"46!#"D!9'.257 T".   I))4 %'XXBHH277Krc|jjj|j|jy)zS Helper to call encode before writing to file for Python 3 compat. N)r rr%rr)r to_writes rW_writezStataWriter._writeJ s) !!(//$.."ABrcN|jjj|y)z? Helper to assert file is open before writing. N)r rr%rTs rW _write_byteszStataWriter._write_bytesP s !!%(rcg}|j|S|jjD]\}}||jvr|j|}n)||jvr t |}nt d|dt ||jstd|dt|||j}|j||S)zc Check for value labels provided for non-categorical columns. Value labels zCan't create value labels for z!, it wasn't found in the dataset.z6, value labels can only be applied to numeric columns.) rPrArTrrrnrrvrfr<rr)r rnon_cat_value_labelsrlabelscolnamesvls rW_prepare_non_cat_value_labelsz)StataWriter._prepare_non_cat_value_labelsV s=?  % % -' '#99??A -OGV$/////8DLL(g,4WI>,, $DM$7$78!4WI>>>(HC ' ' ,' -($#rc|jDcgc]}t|t}}t|s|S|xjt j |zc_tj}g}t||D]\}}|rt|||j}|jj|||jjj }|t j"k(r t%d||jjj&j)} | j+||k\r|t j,k(r$t j t j.}nZ|t j.k(r$t j t j0}n#t j t j2}t j | |} ||| | dk(<|j|| f|j|||ft5j6t9|Scc}w)z Check for categorical columns, retain categorical information for Stata file and convert categorical data to int )r zCIt is not possible to export int64-based categorical data to Stata.rurJ)rrrrrRrrrrerPrrrQrrcodesrvrrfrrrMrrrrr$rr) r rrvis_catredata_formattedr col_is_catrbris rW_prepare_categoricalsz!StataWriter._prepare_categoricalsw s DH;;O%*U$45OO6{K "((6"22!2!I!I"40 8OC%d3i$..I""))#.S ++11BHH$$Ac,,4499;::<#9%#@@' " 2"((* " 2 " 4XXfE:F(>e'Dv|$%%sFm4%%sDI&675 86""4#788GPsIc |D]}}||j}|tjtjfvs5|tjk(r|jd}n|jd}||j |||<|S)z Checks floating point data columns for nans, and replaces these with the generic Stata for missing value (.) rr[)rvrrrrDr)r rr$rvrs rW _replace_nanszStataWriter._replace_nans s~  6AGMMERZZ00BJJ&"&"5"5c":K"&"5"5c":Kq'..5Q 6 rcy)zNo-op, forward compatibilityNr:rVs rW_update_strl_nameszStataWriter._update_strl_names rc|D];}|dks|dkDs|dks|dkDs|dks|dkDs$|dk7s*|j|d}=|S)a Validate variable names for Stata export. Parameters ---------- name : str Variable name Returns ------- str The validated name with invalid characters replaced with underscores. Notes ----- Stata 114 and 117 support ascii characters in a-z, A-Z, 0-9 and _. AZazr9rr)replacer rr$s rW_validate_variable_namez#StataWriter._validate_variable_name sX( ,ASAGWCWCH||As+ , rci}t|j}|dd}d}t|D]\}}|}t|ts t |}|j |}||j vrd|z}d|dcxkrdkrnnd|z}|dtt|d}||k(s\|j|dkDrCdt |z|z}|dtt|d}|dz }|j|dkDrC|||<|||<t||_|jrCt||D]4\} } | | k7s |j| |j| <|j| =6|rzg} |jD]\}}|d|} | j| tj!d j#| } t%j&| t(t+ ||_|j/|S) a Checks column names to ensure that they are valid Stata column names. This includes checks for: * Non-string names * Stata keywords * Variables that start with numbers * Variables with names that are too long When an illegal variable name is detected, it is converted, and if dates are exported, the variable name is propagated to the date conversion dictionary Nrrrrrtr!rBz -> z rs)rrrrrrwrrNrr2r%rrPrArrrJrrrrrrTrm)r rconverted_namesroriginal_columnsduplicate_var_idrr orig_namer$oconversion_warningrrs rW_check_column_nameszStataWriter._check_column_names s$02t||$"1: ) GAtIdC(4y//5Dt***Tzd1g$$Tz,#c$i,-D9$mmD)A-%5!66=D 4#c$i"45D$)$ mmD)A- .2 *GAJ5 8W~    G%56 /16-1-@-@-CD''*++A. / !# #2#8#8#: / 4" 8D62"))#. /"((7I)JKB MM!+-  !0 ! rcg|_g|_|jD]i\}}|jjt ||j ||jjt ||j |kyrZ)rrprArrHrrB)r rrrvs rW_set_formats_and_typesz"StataWriter._set_formats_and_types so"$ "$  ,,. MJC LL   ;E499S> R S LL   4UDIIcN K L Mrc |j}|jr"|j}t|tr|}|j |}t |}|j|}tjd|jd|_ |j|}|Dcgc]}|j}}|jj|}|xj|zc_ |j j#||j%|}|j\|_|_||_|jj-|_|j0}|D]D}||j2vrt5j6||j8ds6d|j2|<Ft;|j2|j.|_|j2D]<} t=|j2| } tj8| |j>| <>|jA|jC||j2?|j2D]/} t| tDs|j2| |jF| <1yycc}w)NFrBrrp)$rrO reset_indexrr$rrrkrrepeatrrRrcrrrrQextendrinobsnvarrtolistr:rrrrrvr<r6r_encode_stringsrr`r) r rtempr_rbnon_cat_columnshas_non_cat_val_labelsrrr?new_types rWrUzStataWriter._prepare_pandas# sIyy{   ##%D$ *''-$D)!!$'"$5$**Q-!@ $AA$G2FG33;;GG!%!2!2?!C "88 !!"67))$/#zz 49 ||**,  0Cd)))tCy4+/##C(  0 9    && 2C6t7J7J37OPH!xx1FKK  2  ##F+    *** Ac3'(,(;(;C(@DLL% A +EHs)J cB|j}t|dg}t|jD]\}}||vs||vr|j|}|j}|j t jusGt|d}|dk(s)t|dk(s|j}td|d|j|jj|j}tt!|j"|j$ks||j|<y) z Encode strings in dta-specific encoding Do not encode columns marked for date conversion or for strL conversion. The strL converter independently handles conversion and also accepts empty string arrays. _convert_strlTrrWrzColumn `a` cannot be exported. Only string-like object arrays containing all strings or a mix of strings and None can be exported. Object arrays containing only null values are prohibited. Other object types cannot be exported and must first be converted to one of the supported types.N)rrOrrrvr^rrArrrrfrrrrrr_max_string_length) r r convert_strlr rr rvinferred_dtypeencodeds rWrzStataWriter._encode_stringse s++ t_b9  * -FAsM!SL%8YYs^FLLEzzRZZ'!,VD!A'83F q8H ++C$ ))C.,,33DNNC)w)GH../&-DIIcN3 -rc t|jd|jd|j5|_|jj dn|jj tc|_|j_|jjj|jj  |j|j|j|j|j|j!|j#|j%|j'|j)|j+|j-|j/}|j1||j3|j5|j7|j|j9 dddy#t:$r}|jj=t?|jt@tBjDfrtBjFjI|jrd tCjJ|j|#tL$r6tOjPd|jdtRtU Y|wxYw|d}~wwxYw#1swYyxYw) a Export DataFrame object to Stata dta format. Examples -------- >>> df = pd.DataFrame({"fully_labelled": [1, 2, 3, 3, 1], ... "partially_labelled": [1.0, 2.0, np.nan, 9.0, np.nan], ... "Y": [7, 7, 9, 8, 10], ... "Z": pd.Categorical(["j", "k", "l", "k", "j"]), ... }) >>> path = "/My_path/filename.dta" >>> labels = {"fully_labelled": {1: "one", 2: "two", 3: "three"}, ... "partially_labelled": {1.0: "one", 2.0: "two"}, ... } >>> writer = pd.io.stata.StataWriter(path, ... df, ... value_labels=labels) # doctest: +SKIP >>> writer.write_file() # doctest: +SKIP >>> df = pd.read_stata(path) # doctest: +SKIP >>> df # doctest: +SKIP index fully_labelled partially_labeled Y Z 0 0 one one 7 j 1 1 two two 7 k 2 2 three NaN 9 l 3 3 three 9.0 8 k 4 4 one NaN 10 j wbF)rrr7methodN)rrz!This save was not successful but z. could not be deleted. This file is not valid.rs)+r)rVrr7r rrrrScreated_handlesr _write_headerrPrR _write_map_write_variable_types_write_varnames_write_sortlist_write_formats_write_value_label_names_write_variable_labels_write_expansion_fields_write_characteristics _prepare_data _write_data _write_strls_write_value_labels_write_file_close_tag_close ExceptionrrrosPathLikepathisfileunlinkOSErrorrrrr)r recordsexcs rW write_filezStataWriter.write_file sJ8 KK )) 00  /  \||''1=:>9L9Lgi6!4<<#6 ,,33DLL4G4GH" ""#//D277>>KKD $++. # ? }MBB+'7'9     C/ / sPB K=D(G// K8A-K &JK ;KK KK  KKKc4|jt|jjtsJ|jj|jc}|j_|jjj |j yy)z Close the file if it was created by the writer. If a buffer or file-like object was passed in, for example a GzipFile, then leave this file open for the caller to close. N)rSrr rrr%r))r r+s rWrzStataWriter._close sp    (dll117; ;;'+||':':D.` s<A S 4<<?!DI  ""4(??ocmm.LLo $FAs!*Cd---,,..++ 9!. c))"-B .HHZsfH=S C5 #s  I,,U3S S '!..t?E#s % $(U&AA#..s 1FF cB|j|jyrZ)r]tobytesr rs rWrzStataWriter._write_data s '//+,rc|dz }|S)Nr2r:)rs rWrzStataWriter._null_terminate_str s V rcV|j|j|jSrZ)rrr)r rs rWrz!StataWriter._null_terminate_bytes s"''*11$..AAr)NTNNNNrN)rJFilePath | WriteBuffer[bytes]rr$rdict[Hashable, str] | NonerXrr*rrdatetime | Nonerrrrrr.r7rr 'dict[Hashable, dict[float, str]] | Nonerr3)rZrrr3)rar5rr3)rr$rzlist[StataNonCatValueLabel]r!r4rrrrrr'rr3)rr$rr3NNrrrrrr3)rnp.rec.recarray)rrrr3)rrrr)rrrr5))r6r7r8r9rrrhrr[r]rcrirkrmrwrrrUrrrrrrrrrrrrrrrrrrr staticmethodrrr&r's@rWrLrLs L\-6I*6 59 $&*!%6:*115$LAE$L,$L$L2 $L  $L  $L$$L$L4$L($L/$L>$L $LLC ) $$ $$B(9T"+r?r@)rvr rFrs rW_dtype_to_stata_type_117r s$ zzRZZ( fnn(EFx# t O rzz ! rzz ! rxx  rxx  rww !Jug_"EFFrcbt|tr t|d}|d|t|z zzS)zU Takes a bytes instance and pads it with null bytes until it's length chars. rr)rrr5rr3s rW_pad_bytes_newr s3$T7# 'Vc$i/0 00rcHeZdZdZ d ddZd dZd dZd dZy) StataStrLWritera Converter for Stata StrLs Stata StrLs map 8 byte values to strings which are stored using a dictionary-like format where strings are keyed to two values. Parameters ---------- df : DataFrame DataFrame to convert columns : Sequence[str] List of columns names to convert to StrL version : int, optional dta version. Currently supports 117, 118 and 119 byteorder : str, optional Can be ">", "<", "little", or "big". default is `sys.byteorder` Notes ----- Supports creation of the StrL block of a dta file for dta versions 117, 118 and 119. These differ in how the GSO is stored. 118 and 119 store the GSO lookup value as a uint32 and a uint64, while 117 uses two uint32s. 118 and 119 also encode all strings as unicode which is required by the format. 117 uses 'latin-1' a fixed width encoding that extends the 7-bit ascii table with an additional 128 characters. Nc*|dvr td||_||_||_ddi|_|t j }t||_d}d}d|_ |dk(r d }d}d |_ n |d k(rd }nd }ddd|z zz|_ ||_ ||_ y)Nr:z,Only dta versions 117, 118 and 119 supportedrrrr%rrr;rrrrrrr) rf_dta_verdfr _gso_tablerr*rr!r_o_offet _gso_o_type _gso_v_type)r rrr>r* gso_v_type gso_o_typeo_sizes rWrzStataStrLWriter.__init__ s / )KL L  v,   I))4    c>FJ&DN ^FFa1v:./ %%rc0|\}}||j|zzSrZ)r )r r?rr}s rW _convert_keyzStataStrLWriter._convert_key s14==1$$$rc|j}|j}t|j}||j}|jDcgc]}||j |f}}t j |jt j}t|jD]b\}\} } t|D]L\} \}} | |} | dn| } |j| d}|| dz|dzf}||| <|j|||| f<Ndt|jD]\}}|dd|f||<||fScc}w)a Generates the GSO lookup table for the DataFrame Returns ------- gso_table : dict Ordered dictionary using the string found as keys and their lookup position (v,o) as values gso_df : DataFrame DataFrame where strl columns have been converted to (v,o) values Notes ----- Modifies the DataFrame in-place. The DataFrame returned encodes the (v,o) values as uint64s. The encoding depends on the dta version, and can be expressed as enc = v + o * 2 ** (o_size * 8) so that v is stored in the lower bits and o is in the upper bits. o_size is * 117: 4 * 118: 6 * 119: 5 ruNrrB) rrrrrKremptyrrriterrowsgetr)r  gso_tablegso_dfrselectedr col_indexrr}rrowrrrr?r s rWgenerate_tablezStataStrLWriter.generate_table sM:OO v~~&$,,':>,,G3c7==-.G Gxxbii8&x'8'8':; 4MAzS(3 4 8C#hKbSmmC.;q5!a%.C%(IcN!..s3QT  4 4  - %FAsq!t*F3K %&  !Hs Ec 8t}tdd}tj|jdzd}tj|jdzd}|j|j z}|j|j z}|jdz}|jD]\} } | dk(r | \} } |j||jtj|| |jtj|| |j|t| d} |jtj|t| d z|j| |j||jS) a Generates the binary blob of GSOs that is written to the dta file. Parameters ---------- gso_table : dict Ordered dictionary (str, vo) Returns ------- gso : bytes Binary content of dta file to be placed between strl tags Notes ----- Output format depends on dta version. 117 uses two uint32s to express v and o while 118+ uses a uint32 for v and a uint64 for o. rasciirrrr%rrrB) rr5rr&r!r r rAr%rr))r rr+gsogso_typenullv_typeo_typelen_typestrlvorr} utf8_strings rW generate_blobzStataStrLWriter.generate_blobI sO:iE7#;;t4c:{{4??S0!44#3#334#3#33??S(!) HD"V|DAq IIcN IIfkk&!, - IIfkk&!, - IIh  g.K IIfkk(C ,r`r*rrr3)r?ztuple[int, int]rr`)rz,tuple[dict[str, tuple[int, int]], DataFrame])rzdict[str, tuple[int, int]]rr5)r6r7r8r9rrrr&r:rrWrr sV@ $ & && &  &  &B%1!f=rrc*eZdZdZdZdZ ddd dfdZeddZddZ d dd Z d d Z d d Z d d Z d d Zd dZd dZd dZd dZd dZd dZd dZd dZd dZd dZd!dZd"dZxZS)#StataWriter117a A class for writing Stata binary dta files in Stata 13 format (117) Parameters ---------- fname : path (string), buffer or path object string, path object (pathlib.Path or py._path.local.LocalPath) or object implementing a binary write() functions. If using a buffer then the buffer will not be automatically closed after the file is written. data : DataFrame Input to save convert_dates : dict Dictionary mapping columns containing datetime types to stata internal format to use when writing the dates. Options are 'tc', 'td', 'tm', 'tw', 'th', 'tq', 'ty'. Column can be either an integer or a name. Datetime columns that do not have a conversion type specified will be converted to 'tc'. Raises NotImplementedError if a datetime column has timezone information write_index : bool Write the index to Stata dataset. byteorder : str Can be ">", "<", "little", or "big". default is `sys.byteorder` time_stamp : datetime A datetime to use as file creation date. Default is the current time data_label : str A label for the data set. Must be 80 characters or smaller. variable_labels : dict Dictionary containing columns as keys and variable labels as values. Each label must be 80 characters or smaller. convert_strl : list List of columns names to convert to Stata StrL format. Columns with more than 2045 characters are automatically written as StrL. Smaller columns can be converted by including the column name. Using StrLs can reduce output file size when strings are longer than 8 characters, and either frequently repeated or sparse. {compression_options} .. versionchanged:: 1.4.0 Zstandard support. value_labels : dict of dicts Dictionary containing columns as keys and dictionaries of column value to labels as values. The combined length of all labels for a single variable must be 32,000 characters or smaller. .. versionadded:: 1.4.0 Returns ------- writer : StataWriter117 instance The StataWriter117 instance has a write_file method, which will write the file to the given `fname`. Raises ------ NotImplementedError * If datetimes contain timezone information ValueError * Columns listed in convert_dates are neither datetime64[ns] or datetime * Column dtype is not representable in Stata * Column listed in convert_dates is not in DataFrame * Categorical label contains more than 32,000 characters Examples -------- >>> data = pd.DataFrame([[1.0, 1, 'a']], columns=['a', 'b', 'c']) >>> writer = pd.io.stata.StataWriter117('./data_file.dta', data) >>> writer.write_file() Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} >>> writer = pd.io.stata.StataWriter117( ... './data_file.zip', data, compression=compression ... ) >>> writer.write_file() Or with long strings stored in strl format >>> data = pd.DataFrame([['A relatively long string'], [''], ['']], ... columns=['strls']) >>> writer = pd.io.stata.StataWriter117( ... './data_file_with_long_strings.dta', data, convert_strl=['strls']) >>> writer.write_file() rkr;NrMc  g|_| |jj| t | ||||||||| | |  i|_d|_y)N)r*rrrr rr7r)rrrr_map _strl_blob)r rJrrrXr*rrrrrr7r rs rWrzStataWriter117.__init__ sj".0  #    % %l 3     !!+%#+  %' rct|tr t|d}td|zdzd|ztd|zdzdzS)zSurround val with rrAr@zrreleaser@MSFLSFr*rrr%Kr;rNNrrrr rrrBrr timestampheader)r!r]r5rr%r.r _dta_versionrr&rrrrrrrrrfrrrRr))r rrr*r+ nvar_type nobs_sizer  encoded_label label_size label_lenrr rRrrstata_tss rWrzStataWriter117._write_header sG OO  % w78i $))E#d&7&7"8'BINO $))I,6?%MN,,3C  $))FKK I(=tyyI3OP,,3C  $))FKK I(=tyyI3OP#-#9 3Br T^^4  --4S# KK J 6M8JK !M1  $))M734  !JJ1AB B  6?v5FGEAu G G    &:++, -!!+. / U2w// $))Hk23 $))CLLNH=>Hs J/c|js8d|jjjddddddddddddd|_|jjj |jdt }|jj D]4}|jtj|jdz|6|j|j|jdy)z Called twice during file write. The first populates the values in the map with 0s. The second call writes the final map locations when all blocks have been written. r) stata_datamapvariable_typesvarnamessortlistformatsvalue_label_namesrcharacteristicsrstrlsr stata_data_close end-of-filerCrN)r*r rrr`rrir%rr&r!r]r.r))r r+rs rWrzStataWriter117._write_mapM s yy||**//1"#%&#$#$ !$% DI"   5!12i99##% ?C IIfkk$//C"7= > ? $))CLLNE:;rc|jdt}|jD]4}|jt j |j dz|6|j|j|jdy)NrDr) r0rrpr%rr&r!r]r.r))r r+rss rWrz$StataWriter117._write_variable_typesk sk )*i<< ?C IIfkk$//C"7= > ? $))CLLN4DEFrcz|jdt}|jdk(rdnd}|jD]O}|j |}t |ddj |j|dz}|j|Q|j|j|jdy)NrEr;r!r#rB) r0rr:r:rrrrr%r]r.r))r r+vn_lenrs rWrzStataWriter117._write_varnamesr s $i((C/SLL D++D1D!$s)"2"24>>"BFQJOD IIdO  $))CLLNJ?@rc|jd|jdkrdnd}|j|jd|z|jdzzdy)NrFr<rrrrB)r0r:r]r.r)r  sort_sizes rWrzStataWriter117._write_sortlist} sO $**S0Aa  $))Gi$7499q=$I:VWrcH|jdt}|jdk(rdnd}|jD]6}|j t |j |j|8|j|j|jdy)NrGr;r~r|) r0rr:rr%rrrr]r.r))r r+fmt_lenrs rWrzStataWriter117._write_formats s #i))S0"b<< KC IInSZZ%?I J K $))CLLNI>?rc|jdt}|jdk(rdnd}t|jD]o}d}|j |r|j |}|j|}t|ddj|j|dz}|j|q|j|j|jdy)NrHr;r!r#rrB)r0rr:r(rrRr:rrrrr%r]r.r))r r+vl_lenr r encoded_names rWrz'StataWriter117._write_value_label_names s ,-i((C/Styy! $AD%%a(||A++D1D)$s)*:*:4>>*JFUVJWL IIl # $ $))CLLN4GHIrc$|jdt}|jdk(rdnd}td|dz}|j[t |j D]}|j||j|j|jdy|jD]}||jvrc|j|}t|dkDr td |j|j}|jt||dzt|j||j|j|jdy#t $r}td|j|d}~wwxYw) Nrr;ri@rrBrzDVariable labels must contain only characters that can be encoded in )r0rr:rrir(rr%r]r.r)rrrfrrUnicodeEncodeError) r r+rUrrrrr rrts rWrz%StataWriter117._write_variable_labels sm *+i((C/Sr6A:.  (499% ! %  !   dii 8IJ K 99 !Cd+++--c2u:?$%UVV#ll4>>:G .&1*=> %  ! $))CLLN4EFG*$--1^^,<>s+E(( F1F  Fch|jd|j|jddy)NrIr)r0r]r.rVs rWrz%StataWriter117._write_characteristics s+ *+ $))C):;s)r0r]rrs rWrzStataWriter117._write_data sA   )$ '//+, *%rc||jd|j|j|jdy)NrJ)r0r]r.r+rVs rWrzStataWriter117._write_strls s- ! $))DOOW=>rcy)zNo-op in dta 117+Nr:rVs rWrz&StataWriter117._write_expansion_fields rnrc6|jdt}|jD]@}|j|j}|j |d}|j |B|j|j |jdy)Nr lbl) r0rrQr0r!r.r%r]r))r r+rlabs rWrz"StataWriter117._write_value_labels s| (i$$ B))$//:C))C'C IIcN  $))CLLNNCDrc~|jd|jtdd|jdy)NrKz rrL)r0r]r5rVs rWrz$StataWriter117._write_file_close_tag s4 +, %89 'rc|jjD]>\}}||jvs|jj|}||j|<@y)z Update column names for conversion to strl if they might have been changed to comply with Stata naming rules N)rTrArrK)r orignewrs rWrmz!StataWriter117._update_strl_names sZ ..446 .ID#t)))((..t4*-""3' .rct|Dcgc]'\}}|j|dk(s||jvr|)}}}|rCt|||j}|j \}}|}|j ||_|Scc}}w)zg Convert columns to StrLs if either very large or in the convert_strl variable ryr=)rrprrr:rr&r+)r rr r convert_colssswtabnew_datas rWrzStataWriter117._convert_strls s$D/ 3||A%'3$2D2D+D   !$ d>O>OPC..0MCD!//4DO  s,BcTg|_g|_|jD]\}}||jv}t ||j ||j |}|jj||jjt||j ||y)N)rErF) rprrArrHrr:rr)r rrrvrFrs rWrz%StataWriter117._set_formats_and_types s   ,,. JC 2 22J- # --% C LL   $ LL  ( # K  r) NTNNNNNrN)rJrrr$rrrXrr*rrrrrrrrSequence[Hashable] | Nonerr.r7rr rrr3)r str | bytesr-rrr5)r-rrr3rrr4r!r)r6r7r8r9rr:rrr.r0rrrrrrrrrrrrrrrmrrr&r's@rWr(r( scSjL 59 $&*!%6:26*115#AE#,##2 #  #  #$##4#0#(#/#># #JXX 4"&&*8?8?$8?  8?t<<G AX @ JH@=& ? E( .$rr(ceZdZUdZdZded< d dd d fdZd dZxZS) StataWriterUTF8u Stata binary dta file writing in Stata 15 (118) and 16 (119) formats DTA 118 and 119 format files support unicode string data (both fixed and strL) format. Unicode is also supported in value labels, variable labels and the dataset label. Format 119 is automatically used if the file contains more than 32,767 variables. Parameters ---------- fname : path (string), buffer or path object string, path object (pathlib.Path or py._path.local.LocalPath) or object implementing a binary write() functions. If using a buffer then the buffer will not be automatically closed after the file is written. data : DataFrame Input to save convert_dates : dict, default None Dictionary mapping columns containing datetime types to stata internal format to use when writing the dates. Options are 'tc', 'td', 'tm', 'tw', 'th', 'tq', 'ty'. Column can be either an integer or a name. Datetime columns that do not have a conversion type specified will be converted to 'tc'. Raises NotImplementedError if a datetime column has timezone information write_index : bool, default True Write the index to Stata dataset. byteorder : str, default None Can be ">", "<", "little", or "big". default is `sys.byteorder` time_stamp : datetime, default None A datetime to use as file creation date. Default is the current time data_label : str, default None A label for the data set. Must be 80 characters or smaller. variable_labels : dict, default None Dictionary containing columns as keys and variable labels as values. Each label must be 80 characters or smaller. convert_strl : list, default None List of columns names to convert to Stata StrL format. Columns with more than 2045 characters are automatically written as StrL. Smaller columns can be converted by including the column name. Using StrLs can reduce output file size when strings are longer than 8 characters, and either frequently repeated or sparse. version : int, default None The dta version to use. By default, uses the size of data to determine the version. 118 is used if data.shape[1] <= 32767, and 119 is used for storing larger DataFrames. {compression_options} .. versionchanged:: 1.4.0 Zstandard support. value_labels : dict of dicts Dictionary containing columns as keys and dictionaries of column value to labels as values. The combined length of all labels for a single variable must be 32,000 characters or smaller. .. versionadded:: 1.4.0 Returns ------- StataWriterUTF8 The instance has a write_file method, which will write the file to the given `fname`. Raises ------ NotImplementedError * If datetimes contain timezone information ValueError * Columns listed in convert_dates are neither datetime64[ns] or datetime * Column dtype is not representable in Stata * Column listed in convert_dates is not in DataFrame * Categorical label contains more than 32,000 characters Examples -------- Using Unicode data and column names >>> from pandas.io.stata import StataWriterUTF8 >>> data = pd.DataFrame([[1.0, 1, 'ᴬ']], columns=['a', 'β', 'ĉ']) >>> writer = StataWriterUTF8('./data_file.dta', data) >>> writer.write_file() Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} >>> writer = StataWriterUTF8('./data_file.zip', data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format >>> data = pd.DataFrame([['ᴀ relatively long ŝtring'], [''], ['']], ... columns=['strls']) >>> writer = StataWriterUTF8('./data_file_with_long_strings.dta', data, ... convert_strl=['strls']) >>> writer.write_file() rzLiteral['utf-8']rNrMc | |jddkrdnd} n1| dvr td| dk(r|jddkDr tdt| ||||||||| | | |  | |_y) NrBirr<)rr<z"version must be either 118 or 119.zKYou must use version 119 for data sets containing more than32,767 variables) rrXr*rrrr rrr7)rrfrrr:)r rJrrrXr*rrrrr>rr7r rs rWrzStataWriterUTF8.__init__is" ?!ZZ]e3cG J &AB B ^ 1  5#    '#!!+%%#+  $rc|D]`}t|dkr#|dks|dkDr|dks|dkDr|dks|dkDr|dk7sdt|cxkrd ksn|d vsO|j|d}b|S) a Validate variable names for Stata export. Parameters ---------- name : str Variable name Returns ------- str The validated name with invalid characters replaced with underscores. Notes ----- Stata 118+ support most unicode characters. The only limitation is in the ascii range where the characters supported are a-z, A-Z, 0-9 and _. r#rprqrrrsrrtrr>×÷)rrurvs rWrwz'StataWriterUTF8._validate_variable_names~* ,AFSLSAGSAGSAGS#a&&3& $||As+ , r) NTNNNNNNrN)rJrrr$rrrXrr*rrrrrrrrrjr>rrr.r7rr rrr3r) r6r7r8r9rrhrrwr&r's@rWrmrms^@#*I) 59 $&*!%6:26"*115*$AE*$,*$*$2 *$  *$  *$$*$*$4*$0*$*$(*$/*$>*$ !*$X#rrm)rr'rrrr'r!)r6rrrrrrrrrrrrrrrrrr(rrr.r7rrzDataFrame | StataReader)r0rrr)rr rr`rr )rrrrg)rrr:zlist[Hashable]rr)rvrgr r'rr`)rF)r r'rEr`rFrrr)rvrgr r'rFrrr`)rrkrr`rr5)vr9 __future__r collectionsrrriorrrrtypingrr r r r r rnumpyr pandas._libsrpandas._libs.librpandas._libs.writersr pandas.errorsrrrrpandas.util._decoratorsrrpandas.util._exceptionsrpandas.core.dtypes.baserpandas.core.dtypes.commonrrrpandas.core.dtypes.dtypesrpandasrrrr r!r"r#pandas.core.framer$pandas.core.indexes.baser%pandas.core.indexes.ranger&pandas.core.seriesr'pandas.core.shared_docsr(pandas.io.commonr)collections.abcr*r+typesr,r-pandas._typingr.r/r0r1r2r3rK_statafile_processing_params1_statafile_processing_params2_chunksize_params_iterator_params _reader_notes_read_stata_docr%r$rrCrhrrrrrrrrrr<rrpIteratorrr+rr'r6r<rBrHrLrrrr(rmr:rrWrs  # (5 52 7(*0%0'$4!N !G$ "(    %&)==>?  ! "?8t      %&'(  !"$O dAq) U)]@h7V(uE #% % )}@vvr%O%>CCL\ \ ~|&+s|||&~ /!% ! $(# &--1!4!! !  !  !!"!!!!$!+!!!HD0C2 !GJGL*G*G(+*G?C*G*GZ !23$%:;gEL B+L B L B^'GT1rrjz[zz rnrr