-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: parsing multi-column headers in read_csv (GH6051) #6170
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7f86033
TST: add test cases for parsing duplicated multiple-column header
waitingkuo 08ed426
BUG: fix the bug when parsing multiple-column header
waitingkuo b7079a6
BUG: use lzip instead of zip to fix py3 compatible issue
waitingkuo 6c35663
EHN: give warning if duplicated columns have been found
waitingkuo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import numpy as np | |
cimport util | ||
|
||
import pandas.lib as lib | ||
from pandas.compat import lzip | ||
|
||
import time | ||
import os | ||
|
@@ -460,7 +461,8 @@ cdef class TextReader: | |
self.parser_start = 0 | ||
self.header = [] | ||
else: | ||
if isinstance(header, list) and len(header): | ||
if isinstance(header, list) and len(header) >= 2: | ||
# FIXME | ||
# need to artifically skip the final line | ||
# which is still a header line | ||
header = list(header) | ||
|
@@ -473,6 +475,11 @@ cdef class TextReader: | |
self.has_mi_columns = 1 | ||
self.header = header | ||
else: | ||
# if the header is a list with length 1 | ||
# set the header as the only element in the list | ||
if isinstance(header, list) and len(header) == 1: | ||
header = header[0] | ||
|
||
self.parser.header_start = header | ||
self.parser.header_end = header | ||
self.parser.header = header | ||
|
@@ -586,6 +593,7 @@ cdef class TextReader: | |
char *errors = "strict" | ||
|
||
header = [] | ||
is_duplicated = False | ||
|
||
if self.parser.header_start >= 0: | ||
|
||
|
@@ -633,6 +641,9 @@ cdef class TextReader: | |
count = counts.get(name, 0) | ||
if count > 0 and self.mangle_dupe_cols and not self.has_mi_columns: | ||
this_header.append('%s.%d' % (name, count)) | ||
|
||
# for warning later | ||
is_duplicated = True | ||
else: | ||
this_header.append(name) | ||
counts[name] = count + 1 | ||
|
@@ -653,6 +664,43 @@ cdef class TextReader: | |
data_line = hr + 1 | ||
header.append(this_header) | ||
|
||
# | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put a GH ???? (the ref number of the issue), instead of these FIXME's |
||
# Append a seq number for the duplicated columns pairs | ||
# | ||
# i.e. [['a', 'a', 'a', 'b'], | ||
# ['A', 'A', 'B', 'C']] | ||
# ==> | ||
# [['a', 'a', 'b', 'b'], | ||
# ['A', 'A.1', 'B', 'C']] | ||
# | ||
if self.has_mi_columns: | ||
|
||
# zip the header, so that we can easily find the duplicated pair | ||
header = lzip(*header) | ||
|
||
counts = {} | ||
for i, column in enumerate(header): | ||
|
||
# Check whether the column is duplicated | ||
count = counts.get(column, 0) | ||
if count > 0: | ||
# | ||
# FIXME | ||
# Since we've added an extra header line (search FIXME in this page) | ||
# Append an incremental seq number to the second-last element | ||
# | ||
tmp_column = list(column) | ||
tmp_column[-2] = '%s.%d' % (tmp_column[-2], count) | ||
header[i] = tuple(tmp_column) | ||
|
||
# for warning later | ||
is_duplicated = True | ||
|
||
counts[column] = count + 1 | ||
|
||
# unzip the header | ||
header = lzip(*header) | ||
|
||
if self.names is not None: | ||
header = [ self.names ] | ||
|
||
|
@@ -710,6 +758,9 @@ cdef class TextReader: | |
elif self.allow_leading_cols and passed_count < field_count: | ||
self.leading_cols = field_count - passed_count | ||
|
||
if self.mangle_dupe_cols and is_duplicated: | ||
warnings.warn('Duplicated columns have been mangled', DtypeWarning) | ||
|
||
return header, field_count | ||
|
||
cdef _implicit_index_count(self): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the
assert_produces_warning
here to assert that you are actually showing the warning (around the read_csv)