Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.to_stata#

DataFrame.to_stata(path,*,convert_dates=None,write_index=True,byteorder=None,time_stamp=None,data_label=None,variable_labels=None,version=114,convert_strl=None,compression='infer',storage_options=None,value_labels=None)[source]#

Export DataFrame object to Stata dta format.

Writes the DataFrame to a Stata dataset file.“dta” files contain a Stata dataset.

Parameters:
pathstr, path object, or buffer

String, path object (implementingos.PathLike[str]), or file-likeobject implementing a binarywrite() function.

convert_datesdict

Dictionary mapping columns containing datetime types to statainternal format to use when writing the dates. Options are ‘tc’,‘td’, ‘tm’, ‘tw’, ‘th’, ‘tq’, ‘ty’. Column can be either an integeror a name. Datetime columns that do not have a conversion typespecified will be converted to ‘tc’. Raises NotImplementedError ifa datetime column has timezone information.

write_indexbool

Write the index to Stata dataset.

byteorderstr

Can be “>”, “<”, “little”, or “big”. default issys.byteorder.

time_stampdatetime

A datetime to use as file creation date. Default is the currenttime.

data_labelstr, optional

A label for the data set. Must be 80 characters or smaller.

variable_labelsdict

Dictionary containing columns as keys and variable labels asvalues. Each label must be 80 characters or smaller.

version{114, 117, 118, 119, None}, default 114

Version to use in the output dta file. Set to None to let pandasdecide between 118 or 119 formats depending on the number ofcolumns in the frame. Version 114 can be read by Stata 10 andlater. Version 117 can be read by Stata 13 or later. Version 118is supported in Stata 14 and later. Version 119 is supported inStata 15 and later. Version 114 limits string variables to 244characters or fewer while versions 117 and later allow stringswith lengths up to 2,000,000 characters. Versions 118 and 119support Unicode characters, and version 119 supports more than32,767 variables.

Version 119 should usually only be used when the number ofvariables exceeds the capacity of dta format 118. Exportingsmaller datasets in format 119 may have unintended consequences,and, as of November 2020, Stata SE cannot read version 119 files.

convert_strllist, optional

List of column names to convert to string columns to Stata StrLformat. Only available if version is 117. Storing strings in theStrL format can produce smaller dta files if strings have more than8 characters and values are repeated.

compressionstr or dict, default ‘infer’

For on-the-fly compression of the output data. If ‘infer’ and ‘path’ ispath-like, then detect compression from the following extensions: ‘.gz’,‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, ‘.tar’, ‘.tar.gz’, ‘.tar.xz’ or ‘.tar.bz2’(otherwise no compression).Set toNone for no compression.Can also be a dict with key'method' setto one of {'zip','gzip','bz2','zstd','xz','tar'} andother key-value pairs are forwarded tozipfile.ZipFile,gzip.GzipFile,bz2.BZ2File,zstandard.ZstdCompressor,lzma.LZMAFile ortarfile.TarFile, respectively.As an example, the following could be passed for faster compression and to createa reproducible gzip archive:compression={'method':'gzip','compresslevel':1,'mtime':1}.

Added in version 1.5.0:Added support for.tar files.

Changed in version 1.4.0:Zstandard support.

storage_optionsdict, optional

Extra options that make sense for a particular storage connection, e.g.host, port, username, password, etc. For HTTP(S) URLs the key-value pairsare forwarded tourllib.request.Request as header options. For otherURLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs areforwarded tofsspec.open. Please seefsspec andurllib for moredetails, and for more examples on storage options referhere.

value_labelsdict of dicts

Dictionary containing columns as keys and dictionaries of column valueto labels as values. Labels for a single variable must be 32,000characters or smaller.

Added in version 1.4.0.

Raises:
NotImplementedError
  • If datetimes contain timezone information

  • Column dtype is not representable in Stata

ValueError
  • Columns listed in convert_dates are neither datetime64[ns]or datetime.datetime

  • Column listed in convert_dates is not in DataFrame

  • Categorical label contains more than 32,000 characters

See also

read_stata

Import Stata data files.

io.stata.StataWriter

Low-level writer for Stata data files.

io.stata.StataWriter117

Low-level writer for version 117 files.

Examples

>>>df=pd.DataFrame({'animal':['falcon','parrot','falcon',...'parrot'],...'speed':[350,18,361,15]})>>>df.to_stata('animals.dta')

[8]ページ先頭

©2009-2025 Movatter.jp