Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Fix for #1512, added StataReader and StataWriter to pandas.io.parsers #3270

Merged
merged 6 commits into from
May 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pandas 0.11.1
- pd.read_html() can now parse HTML string, files or urls and return dataframes
courtesy of @cpcloud. (GH3477_)
- Support for reading Amazon S3 files. (GH3504_)
- Added module for reading and writing Stata files: pandas.io.stata (GH1512_)

**Improvements to existing features**

Expand Down Expand Up @@ -166,6 +167,7 @@ pandas 0.11.1
.. _GH3610: https://github.com/pydata/pandas/issues/3610
.. _GH3596: https://github.com/pydata/pandas/issues/3596
.. _GH3435: https://github.com/pydata/pandas/issues/3435
.. _GH1512: https://github.com/pydata/pandas/issues/1512


pandas 0.11.0
Expand Down
41 changes: 41 additions & 0 deletions doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1829,3 +1829,44 @@ There are a few other available functions:

For now, writing your DataFrame into a database works only with
**SQLite**. Moreover, the **index** will currently be **dropped**.


Reading from STATA format
~~~~~~~~~~~~~~~~~~~~~~

.. _io.StataReader:

.. versionadded:: 0.11.1

The class StataReader will read the header of the given dta file at
initialization. Its function :func:'~pandas.io.StataReader.data' will
read the observations, converting them to a DataFrame which is returned:

.. ipython:: python
reader = StataReader(dta_filepath)
dataframe = reader.data()

The parameter convert_categoricals indicates wheter value labels should be
read and used to create a Categorical variable from them. Value labels can
also be retrieved by the function variable_labels, which requires data to be
called before.
The StataReader supports .dta Formats 104, 105, 108, 113-115.

Alternatively, the function :func:'~pandas.io.read_stata' can be used:

.. ipython:: python
dataframe = read_stata(dta_filepath)


Writing to STATA format
~~~~~~~~~~~~~~~~~~~~~~

.. _io.StataWriter:

The function :func:'~pandas.io.StataWriter.write_file' will write a DataFrame
into a .dta file. The format version of this file is always the latest one,
115.

.. ipython:: python
writer = StataWriter(filename, dataframe)
writer.write_file()
2 changes: 2 additions & 0 deletions doc/source/v0.11.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Enhancements
- support datelike columns with a timezone as data_columns (GH2852_)
- ``fillna`` methods now raise a ``TypeError`` if the ``value`` parameter is
a list or tuple.
- Added module for reading and writing Stata files: pandas.io.stata (GH1512_)

See the `full release notes
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
Expand All @@ -68,3 +69,4 @@ on GitHub for a complete list.
.. _GH3596: https://github.com/pydata/pandas/issues/3596
.. _GH3590: https://github.com/pydata/pandas/issues/3590
.. _GH3435: https://github.com/pydata/pandas/issues/3435
.. _GH1512: https://github.com/pydata/pandas/issues/1512
29 changes: 29 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,35 @@ def from_csv(cls, path, header=0, sep=',', index_col=0,
parse_dates=parse_dates, index_col=index_col,
encoding=encoding)

@classmethod
def from_dta(dta, path, parse_dates=True, convert_categoricals=True, encoding=None, index_col=None):
"""
Read Stata file into DataFrame

Parameters
----------
path : string file path or file handle / StringIO
parse_dates : boolean, default True
Convert date variables to DataFrame time values
convert_categoricals : boolean, default True
Read value labels and convert columns to Categorical/Factor variables
encoding : string, None or encoding, default None
Encoding used to parse the files. Note that Stata doesn't
support unicode. None defaults to cp1252.
index_col : int or sequence, default None
Column to use for index. If a sequence is given, a MultiIndex
is used. Different default from read_table

Notes
-----

Returns
-------
y : DataFrame
"""
from pandas.io.stata import read_stata
return read_stata(path, parse_dates=parse_dates, convert_categoricals=convert_categoricals, encoding=encoding, index=index_col)

def to_sparse(self, fill_value=None, kind='block'):
"""
Convert to SparseDataFrame
Expand Down
Loading