Skip to content

Commit

Permalink
BUG: fix usecols case when passing names and usecols relative to pass…
Browse files Browse the repository at this point in the history
…ed names. close #2465
  • Loading branch information
wesm committed Dec 9, 2012
1 parent bf0c904 commit 7679eee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,11 +1783,14 @@ def test_usecols(self):
expected = expected[['b', 'c']]
tm.assert_frame_equal(result, expected)

result2 = self.read_csv(StringIO(data), names=['a', 'b', 'c'],
header=None, usecols=['b', 'c'])
tm.assert_frame_equal(result2, result)

# length conflict, passed names and usecols disagree
self.assertRaises(ValueError, self.read_csv, StringIO(data),
names=['a', 'b'], usecols=[1], header=None)


def test_pure_python_failover(self):
data = "a,b,c\n1,2,3#ignore this!\n4,5,6#ignorethistoo"

Expand Down Expand Up @@ -1951,14 +1954,17 @@ def test_convert_sql_column_decimals(self):
expected = np.array([1.5, np.nan, 3, 4.2], dtype='f8')
assert_same_values_and_dtype(result, expected)


def assert_same_values_and_dtype(res, exp):
assert(res.dtype == exp.dtype)
assert_almost_equal(res, exp)


def curpath():
pth, _ = os.path.split(os.path.abspath(__file__))
return pth


if __name__ == '__main__':
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
exit=False)
5 changes: 4 additions & 1 deletion pandas/src/parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,10 @@ cdef class TextReader:

cdef _get_column_name(self, Py_ssize_t i, Py_ssize_t nused):
if self.has_usecols and self.names is not None:
return self.names[nused]
if len(self.names) == len(self.usecols):
return self.names[nused]
else:
return self.names[i]
else:
if self.header is not None:
return self.header[i - self.leading_cols]
Expand Down

0 comments on commit 7679eee

Please sign in to comment.