Skip to content

BUG: dataframe loading with duplicated columns and usecols #11823 #11882

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

Closed
wants to merge 1 commit into from
Closed
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/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ Bug Fixes


- Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`)

- Bug in ``read_csv`` with duplicated columns and ``usecols`` (:issue:`11823`)
10 changes: 3 additions & 7 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from __future__ import print_function
from pandas.compat import range, lrange, StringIO, lzip, zip, string_types, map
from pandas import compat
from pandas import compat, unique
from collections import defaultdict
import re
import csv
Expand Down Expand Up @@ -1788,12 +1788,8 @@ def _handle_usecols(self, columns, usecols_key):
if len(columns) > 1:
raise ValueError("If using multiple headers, usecols must "
"be integers.")
col_indices = []
for u in self.usecols:
if isinstance(u, string_types):
col_indices.append(usecols_key.index(u))
else:
col_indices.append(u)
col_indices = Index(usecols_key).get_indexer_for(
unique(self.usecols))
else:
col_indices = self.usecols

Expand Down
14 changes: 14 additions & 0 deletions pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,6 +2342,20 @@ def test_usecols(self):
expected = expected[['a', 'b']]
tm.assert_frame_equal(result, expected)

# 11823: usecols vs no usecols
result = self.read_csv(StringIO(data), names=['a', 'a', 'b'],
header=None, usecols=['a', 'a', 'b'])
expected = self.read_csv(StringIO(data), names=['a', 'a', 'b'],
header=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to use a fixed result, here IOW, actually construct the expected DataFrame, rather than reading it in with a different set of options.

tm.assert_frame_equal(result, expected)

# 11823: c vs python engine
result_c = pd.read_csv(StringIO(data), engine='c', header=None,
names=['a', 'a', 'b'], usecols=['a','a','b'])
result_py = pd.read_csv(StringIO(data), engine='python', header=None,
names=['a', 'a', 'b'], usecols=['a','a','b'])
tm.assert_frame_equal(result_c, result_py)

# length conflict, passed names and usecols disagree
self.assertRaises(ValueError, self.read_csv, StringIO(data),
names=['a', 'b'], usecols=[1], header=None)
Expand Down
6 changes: 4 additions & 2 deletions pandas/parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ cdef class TextReader:
object compression
object mangle_dupe_cols
object tupleize_cols
set noconvert, usecols
set noconvert
list usecols

def __cinit__(self, source,
delimiter=b',',
Expand Down Expand Up @@ -409,7 +410,8 @@ cdef class TextReader:
# suboptimal
if usecols is not None:
self.has_usecols = 1
self.usecols = set(usecols)
self.usecols = list(usecols)
#self.usecols = set(usecols)

# XXX
if skip_footer > 0:
Expand Down