pandas.io.stata.StataWriter#
- class pandas.io.stata.StataWriter(
- fname,
- data,
- convert_dates=None,
- write_index=True,
- byteorder=None,
- time_stamp=None,
- data_label=None,
- variable_labels=None,
- compression='infer',
- storage_options=None,
- *,
- value_labels=None,
A class for writing Stata binary dta files.
Writes dta format 114.
to_stata()is the usual entry point and also supports newer formats.- Parameters:
- fnamestr, path object or file-like object
String, pathlib.Path or object implementing a binary
write()function. If using a buffer then the buffer will not be automatically closed after the file is written.- dataDataFrame
Input to save.
- convert_datesdict
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_indexbool
Write the index to Stata dataset.
- byteorderstr
Can be “>”, “<”, “little”, or “big”. default is sys.byteorder.
- time_stampdatetime
A datetime to use as file creation date. Default is the current time.
- data_labelstr
A label for the data set. Must be 80 characters or smaller.
- variable_labelsdict
Dictionary containing columns as keys and variable labels as values. Each label must be 80 characters or smaller.
- compressionstr or dict, default ‘infer’
For on-the-fly compression of the output data. If ‘infer’ and ‘fname’ is path-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 to
Nonefor no compression. Can also be a dict with key'method'set to one of {'zip','gzip','bz2','zstd','xz','tar'} and other key-value pairs are forwarded tozipfile.ZipFile,gzip.GzipFile,bz2.BZ2File,zstandard.ZstdCompressor,lzma.LZMAFileortarfile.TarFile, respectively. As an example, the following could be passed for faster compression and to create a reproducible gzip archive:compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}.- 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 pairs are forwarded to
urllib.request.Requestas header options. For other URLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs are forwarded tofsspec.open. Please seefsspecandurllibfor more details, and for more examples on storage options refer here.- value_labelsdict 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.
- Returns:
- writerStataWriter 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
See also
DataFrame.to_stataExport DataFrame object to Stata dta format.
read_stataRead Stata file into DataFrame.
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()
Methods
Export DataFrame object to Stata dta format.