Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Classe « DataFrame »

Méthode pandas.DataFrame.to_stata

Signature de la méthode to_stata

def to_stata(self, path: 'FilePathOrBuffer', convert_dates: 'Optional[Dict[Label, str]]' = None, write_index: 'bool' = True, byteorder: 'Optional[str]' = None, time_stamp: 'Optional[datetime.datetime]' = None, data_label: 'Optional[str]' = None, variable_labels: 'Optional[Dict[Label, str]]' = None, version: 'Optional[int]' = 114, convert_strl: 'Optional[Sequence[Label]]' = None, compression: 'CompressionOptions' = 'infer', storage_options: 'StorageOptions' = None) -> 'None' 

Description

to_stata.__doc__

Export DataFrame object to Stata dta format.

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

Parameters
----------
path : str, buffer or path object
    String, path object (pathlib.Path or py._path.local.LocalPath) or
    object implementing a binary write() function. If using a buffer
    then the buffer will not be automatically closed after the file
    data has been written.

    .. versionchanged:: 1.0.0

    Previously this was "fname"

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, optional
    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.
version : {114, 117, 118, 119, None}, default 114
    Version to use in the output dta file. Set to None to let pandas
    decide between 118 or 119 formats depending on the number of
    columns in the frame. Version 114 can be read by Stata 10 and
    later. Version 117 can be read by Stata 13 or later. Version 118
    is supported in Stata 14 and later. Version 119 is supported in
    Stata 15 and later. Version 114 limits string variables to 244
    characters or fewer while versions 117 and later allow strings
    with lengths up to 2,000,000 characters. Versions 118 and 119
    support Unicode characters, and version 119 supports more than
    32,767 variables.

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

    .. versionchanged:: 1.0.0

        Added support for formats 118 and 119.

convert_strl : list, optional
    List of column names to convert to string columns to Stata StrL
    format. Only available if version is 117.  Storing strings in the
    StrL format can produce smaller dta files if strings have more than
    8 characters and values are repeated.
compression : str or dict, default 'infer'
    For on-the-fly compression of the output dta. If string, specifies
    compression mode. If dict, value at key 'method' specifies
    compression mode. Compression mode must be one of {'infer', 'gzip',
    'bz2', 'zip', 'xz', None}. If compression mode is 'infer' and
    `fname` is path-like, then detect compression from the following
    extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise no
    compression). If dict and compression mode is one of {'zip',
    'gzip', 'bz2'}, or inferred as one of the above, other entries
    passed as additional compression options.

    .. versionadded:: 1.1.0

storage_options : dict, optional
    Extra options that make sense for a particular storage connection, e.g.
    host, port, username, password, etc., if using a URL that will
    be parsed by ``fsspec``, e.g., starting "s3://", "gcs://". An error
    will be raised if providing this argument with a non-fsspec URL.
    See the fsspec and backend storage implementation docs for the set of
    allowed keys and values.

    .. versionadded:: 1.2.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')  # doctest: +SKIP