Skip to content

BUG: Fix an issue with the csv cparser when usecols is used #4406

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
Jul 30, 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 doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pandas 0.13
- Fixed an issue where ``PeriodIndex`` joining with self was returning a new
instance rather than the same instance (:issue:`4379`); also adds a test
for this for the other index types
- Fixed a bug with all the dtypes being converted to object when using the CSV cparser
with the usecols parameter (:issue: `3192`)

pandas 0.12
===========
Expand Down
22 changes: 22 additions & 0 deletions pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,28 @@ def test_usecols(self):
self.assertRaises(ValueError, self.read_csv, StringIO(data),
names=['a', 'b'], usecols=[1], header=None)

def test_usecols_dtypes(self):
data = """\
1,2,3
4,5,6
7,8,9
10,11,12"""
result = self.read_csv(StringIO(data), usecols=(0, 1, 2),
names=('a', 'b', 'c'),
header=None,
converters={'a': str},
dtype={'b': int, 'c': float},
)
result2 = self.read_csv(StringIO(data), usecols=(0, 2),
names=('a', 'b', 'c'),
header=None,
converters={'a': str},
dtype={'b': int, 'c': float},
)
self.assertTrue((result.dtypes == [object, np.int, np.float]).all())
self.assertTrue((result2.dtypes == [object, np.float]).all())


def test_usecols_implicit_index_col(self):
# #2654
data = 'a,b,c\n4,apple,bat,5.7\n8,orange,cow,10'
Expand Down
5 changes: 1 addition & 4 deletions pandas/parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ cdef class TextReader:
if self.has_usecols and not (i in self.usecols or
name in self.usecols):
continue
nused += 1

conv = self._get_converter(i, name)

Expand Down Expand Up @@ -907,10 +908,6 @@ cdef class TextReader:

results[i] = col_res

# number of used column names
if i > self.leading_cols:
nused += 1

self.parser_start += end - start

return results
Expand Down