pandas 1.4.2

ParametersRaisesReturnsBackRef

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.

versionadded

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 :None:None:`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} :
versionadded
versionchanged
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

Raises

NotImplementedError
  • If datetimes contain timezone information

ValueError
  • Columns listed in convert_dates are neither datetime64[ns] or datetime.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

Returns

StataWriterUTF8

The instance has a write_file method, which will write the file to the given :None:None:`fname`.

Stata binary dta file writing in Stata 15 (118) and 16 (119) formats

Examples

Using Unicode data and column names

This example is valid syntax, but we were not able to check execution
>>> 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

This example is valid syntax, but we were not able to check execution
>>> data = pd.DataFrame([['ᴀ relatively long ŝtring'], [''], ['']],
...  columns=['strls'])
... writer = StataWriterUTF8('./data_file_with_long_strings.dta', data,
...  convert_strl=['strls'])
... writer.write_file()
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.io.stata.StataWriterUTF8

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/io/stata.py#3474
type: <class 'type'>
Commit: