Skip to content

add fix for opening zero observation dta files #7369

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

Merged
merged 1 commit into from
Jun 13, 2014
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
1 change: 1 addition & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,4 @@ Bug Fixes
- Bug where a string column name assignment to a ``DataFrame`` with a
``Float64Index`` raised a ``TypeError`` during a call to ``np.isnan``
(:issue:`7366`).
- Bug in ``StataReader.data`` where reading a 0-observation dta failed (:issue:`7369`)
5 changes: 4 additions & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,10 @@ def data(self, convert_dates=True, convert_categoricals=True, index=None):
if convert_categoricals:
self._read_value_labels()

data = DataFrame(data, columns=self.varlist, index=index)
if len(data)==0:
data = DataFrame(columns=self.varlist, index=index)
else:
data = DataFrame(data, columns=self.varlist, index=index)

cols_ = np.where(self.dtyplist)[0]
for i in cols_:
Expand Down
8 changes: 8 additions & 0 deletions pandas/io/tests/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ def read_dta(self, file):
def read_csv(self, file):
return read_csv(file, parse_dates=True)

def test_read_empty_dta(self):
empty_ds = DataFrame(columns=['unit'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the comment GH 7369 here

# GH 7369, make sure can read a 0-obs dta file
with tm.ensure_clean() as path:
empty_ds.to_stata(path,write_index=False)
empty_ds2 = read_stata(path)
tm.assert_frame_equal(empty_ds, empty_ds2)

def test_read_dta1(self):
reader_114 = StataReader(self.dta1_114)
parsed_114 = reader_114.data()
Expand Down