pandas.api.typing.StataReader#

class pandas.api.typing.StataReader(
path_or_buf,
convert_dates=True,
convert_categoricals=True,
index_col=None,
convert_missing=False,
preserve_dtypes=True,
columns=None,
order_categoricals=True,
chunksize=None,
compression='infer',
storage_options=None,
)[source]#

Class for reading Stata dta files.

Returned by read_stata() when iterator=True or chunksize is given. Must be used as a context manager.

Parameters:
path_or_bufstr, path object or file-like object

String, pathlib.Path or object implementing a binary read() function.

convert_datesbool, default True

Convert date variables to DataFrame time values.

convert_categoricalsbool, default True

Read value labels and convert columns to Categorical/Factor variables.

index_colstr, optional

Column to set as index.

convert_missingbool, 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_dtypesbool, default True

Preserve Stata datatypes. If False, numeric data are upcast to pandas default types for foreign data (float64 or int64).

columnslist or None

Columns to retain. Columns will be returned in the given order. None returns all columns.

order_categoricalsbool, default True

Flag indicating whether converted categorical data are ordered.

chunksizeint, default None

Return StataReader object for iterations, returns chunks with given number of lines.

compressionstr or dict, default ‘infer’

For on-the-fly decompression of on-disk data. If ‘infer’ and ‘%s’ 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). If using ‘zip’ or ‘tar’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. 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 to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, zstandard.ZstdDecompressor, lzma.LZMAFile or tarfile.TarFile, respectively. As an example, the following could be passed for Zstandard decompression using a custom compression dictionary: compression={'method': 'zstd', 'dict_data': my_compression_dict}.

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.Request as header options. For other URLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs are forwarded to fsspec.open. Please see fsspec and urllib for more details, and for more examples on storage options refer here.

See also

read_stata

Read Stata file into DataFrame.

DataFrame.to_stata

Export DataFrame object to Stata dta format.

Notes

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.

Examples

>>> from io import BytesIO
>>> buf = BytesIO()
>>> pd.DataFrame({"a": [1, 2, 3, 4]}).to_stata(buf, write_index=False)
>>> _ = buf.seek(0)
>>> with pd.read_stata(buf, chunksize=2) as reader:
...     for chunk in reader:
...         print(chunk.shape)
(2, 1)
(2, 1)

Attributes

data_label

Return data label of Stata file.

time_stamp

Return time stamp of Stata file.

Methods

get_chunk([size])

Read the next chunk of lines from the Stata file into a DataFrame.

read([nrows, convert_dates, ...])

Read observations from the Stata file into a DataFrame.

value_labels()

Return a nested dict mapping each value label name to its values and labels.

variable_labels()

Return a dict associating each variable name with corresponding label.