Skip to content

Bug in read_csv and read_excel not applying dtype to second col with dup cols #41411

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 3 commits into from
May 12, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ I/O
- Bug in :func:`read_excel` raising ``AttributeError`` with ``MultiIndex`` header followed by two empty rows and no index, and bug affecting :func:`read_excel`, :func:`read_csv`, :func:`read_table`, :func:`read_fwf`, and :func:`read_clipboard` where one blank row after a ``MultiIndex`` header with no index would be dropped (:issue:`40442`)
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`)
- Bug in :func:`read_orc` always raising ``AttributeError`` (:issue:`40918`)
- Bug in :func:`read_csv` and :func:`read_excel` not respecting dtype for duplicated column name when ``mangle_dupe_cols`` is set to ``True`` (:issue:`35211`)
- Bug in :func:`read_csv` and :func:`read_table` misinterpreting arguments when ``sys.setprofile`` had been previously called (:issue:`41069`)
- Bug in the conversion from pyarrow to pandas (e.g. for reading Parquet) with nullable dtypes and a pyarrow array whose data buffer size is not a multiple of dtype size (:issue:`40896`)

Expand Down
15 changes: 11 additions & 4 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,17 @@ cdef class TextReader:
count = counts.get(name, 0)

if not self.has_mi_columns and self.mangle_dupe_cols:
while count > 0:
counts[name] = count + 1
name = f'{name}.{count}'
count = counts.get(name, 0)
if count > 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

would be nice to unify this code between here and the python parser (followon)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes definitely, but will have to refactor the PythonParser quite a bit and split into 2 classes to be able to inherit from TextReader respectively a generic cython class where TextReader and something like PythonTextReader can inherit from.

I am planning to do this in the (probably medium-term) future

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds great!

feels free to open an issue for tracking

Copy link
Member Author

Choose a reason for hiding this comment

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

Though about using #39345 for this

while count > 0:
counts[name] = count + 1
name = f'{name}.{count}'
count = counts.get(name, 0)
if (
self.dtype is not None
and self.dtype.get(old_name) is not None
and self.dtype.get(name) is None
):
self.dtype.update({name: self.dtype.get(old_name)})

if old_name == '':
unnamed_cols.add(name)
Expand Down
16 changes: 12 additions & 4 deletions pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,20 @@ def _infer_columns(self):
counts: DefaultDict = defaultdict(int)

for i, col in enumerate(this_columns):
old_col = col
cur_count = counts[col]

while cur_count > 0:
counts[col] = cur_count + 1
col = f"{col}.{cur_count}"
cur_count = counts[col]
if cur_count > 0:
while cur_count > 0:
counts[col] = cur_count + 1
col = f"{col}.{cur_count}"
cur_count = counts[col]
if (
self.dtype is not None
and self.dtype.get(old_col) is not None
and self.dtype.get(col) is None
):
self.dtype.update({col: self.dtype.get(old_col)})

this_columns[i] = col
counts[col] = cur_count + 1
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ def test_reader_dtype_str(self, read_ext, dtype, expected):
actual = pd.read_excel(basename + read_ext, dtype=dtype)
tm.assert_frame_equal(actual, expected)

@pytest.mark.parametrize("dtypes, exp_value", [({}, "1"), ({"a.1": "int64"}, 1)])
def test_dtype_mangle_dup_cols(self, read_ext, dtypes, exp_value):
# GH#35211
basename = "df_mangle_dup_col_dtypes"
result = pd.read_excel(basename + read_ext, dtype={"a": str, **dtypes})
expected = DataFrame({"a": ["1"], "a.1": [exp_value]})
tm.assert_frame_equal(result, expected)

def test_reader_spaces(self, read_ext):
# see gh-32207
basename = "test_spaces"
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/parser/dtypes/test_dtypes_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,13 @@ def test_true_values_cast_to_bool(all_parsers):
)
expected["a"] = expected["a"].astype("boolean")
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("dtypes, exp_value", [({}, "1"), ({"a.1": "int64"}, 1)])
def test_dtype_mangle_dup_cols(all_parsers, dtypes, exp_value):
# GH#35211
parser = all_parsers
data = """a,a\n1,1"""
result = parser.read_csv(StringIO(data), dtype={"a": str, **dtypes})
expected = DataFrame({"a": ["1"], "a.1": [exp_value]})
tm.assert_frame_equal(result, expected)