diff --git a/doc/source/release.rst b/doc/source/release.rst index e9af4ccf50dc4..c5856666ca02a 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -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 =========== diff --git a/pandas/io/tests/test_parsers.py b/pandas/io/tests/test_parsers.py index 730450e373341..d83fbd97b6044 100644 --- a/pandas/io/tests/test_parsers.py +++ b/pandas/io/tests/test_parsers.py @@ -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' diff --git a/pandas/parser.pyx b/pandas/parser.pyx index 9bf693f3cb703..36055e681a706 100644 --- a/pandas/parser.pyx +++ b/pandas/parser.pyx @@ -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) @@ -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