Skip to content

Commit 93c86aa

Browse files
miker985jreback
authored andcommitted
BUGFIX - AttributeError raised in StataReader.value_labels() (#19510)
1 parent d5eead6 commit 93c86aa

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ I/O
627627
- Bug in :func:`DataFrame.to_parquet` where an exception was raised if the write destination is S3 (:issue:`19134`)
628628
- :class:`Interval` now supported in :func:`DataFrame.to_excel` for all Excel file types (:issue:`19242`)
629629
- :class:`Timedelta` now supported in :func:`DataFrame.to_excel` for xls file type (:issue:`19242`, :issue:`9155`)
630+
- Bug in :meth:`pandas.io.stata.StataReader.value_labels` raising an ``AttributeError`` when called on very old files. Now returns an empty dict (:issue:`19417`)
630631

631632
Plotting
632633
^^^^^^^^

pandas/io/stata.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1341,12 +1341,14 @@ def _null_terminate(self, s):
13411341
return s
13421342

13431343
def _read_value_labels(self):
1344-
if self.format_version <= 108:
1345-
# Value labels are not supported in version 108 and earlier.
1346-
return
13471344
if self._value_labels_read:
13481345
# Don't read twice
13491346
return
1347+
if self.format_version <= 108:
1348+
# Value labels are not supported in version 108 and earlier.
1349+
self._value_labels_read = True
1350+
self.value_label_dict = dict()
1351+
return
13501352

13511353
if self.format_version >= 117:
13521354
self.path_or_buf.seek(self.seek_value_labels)

pandas/tests/io/test_stata.py

+10
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,16 @@ def test_105(self):
589589
df0['psch_dis'] = df0["psch_dis"].astype(np.float32)
590590
tm.assert_frame_equal(df.head(3), df0)
591591

592+
def test_value_labels_old_format(self):
593+
# GH 19417
594+
#
595+
# Test that value_labels() returns an empty dict if the file format
596+
# predates supporting value labels.
597+
dpath = os.path.join(self.dirpath, 'S4_EDUC1.dta')
598+
reader = StataReader(dpath)
599+
assert reader.value_labels() == {}
600+
reader.close()
601+
592602
def test_date_export_formats(self):
593603
columns = ['tc', 'td', 'tw', 'tm', 'tq', 'th', 'ty']
594604
conversions = {c: c for c in columns}

0 commit comments

Comments
 (0)