Skip to content

Commit 70b1889

Browse files
committed
Add failed test
Fix column order of dtypes Use .get_loc, get rid of matched() Add line to What's New
1 parent 304a5f4 commit 70b1889

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,4 @@ Bug Fixes
606606
- Bug in vectorised setting of timestamp columns with python ``datetime.date`` and numpy ``datetime64`` (:issue:`10408`, :issue:`10412`)
607607

608608
- Bug in ``pd.DataFrame`` when constructing an empty DataFrame with a string dtype (:issue:`9428`)
609+
- Bug in ``read_stata`` when reading a file with a different order set in ``columns`` (:issue:`10739`)

pandas/io/stata.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1614,14 +1614,12 @@ def _do_select_columns(self, data, columns):
16141614
typlist = []
16151615
fmtlist = []
16161616
lbllist = []
1617-
matched = set()
1618-
for i, col in enumerate(data.columns):
1619-
if col in column_set:
1620-
matched.update([col])
1621-
dtyplist.append(self.dtyplist[i])
1622-
typlist.append(self.typlist[i])
1623-
fmtlist.append(self.fmtlist[i])
1624-
lbllist.append(self.lbllist[i])
1617+
for col in columns:
1618+
i = data.columns.get_loc(col)
1619+
dtyplist.append(self.dtyplist[i])
1620+
typlist.append(self.typlist[i])
1621+
fmtlist.append(self.fmtlist[i])
1622+
lbllist.append(self.lbllist[i])
16251623

16261624
self.dtyplist = dtyplist
16271625
self.typlist = typlist

pandas/io/tests/test_stata.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,14 @@ def test_drop_column(self):
780780
expected = expected[columns]
781781
dropped = read_stata(self.dta15_117, convert_dates=True,
782782
columns=columns)
783-
784783
tm.assert_frame_equal(expected, dropped)
784+
785+
columns = ['int_', 'long_', 'byte_']
786+
expected = expected[columns]
787+
reordered = read_stata(self.dta15_117, convert_dates=True,
788+
columns=columns)
789+
tm.assert_frame_equal(expected, reordered)
790+
785791
with tm.assertRaises(ValueError):
786792
columns = ['byte_', 'byte_']
787793
read_stata(self.dta15_117, convert_dates=True, columns=columns)

0 commit comments

Comments
 (0)