Skip to content

read_excel() modifies provided types dict when accessing file with duplicate column #42508

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 17 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 4 additions & 1 deletion pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def __init__(self, f: FilePathOrBuffer | list, **kwds):
self.verbose = kwds["verbose"]
self.converters = kwds["converters"]

self.dtype = kwds["dtype"]
if isinstance(kwds["dtype"], dict):
Copy link
Member

Choose a reason for hiding this comment

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

Can you please a small comment why this is necessary (even just pointing back to the issue)

Copy link
Member Author

Choose a reason for hiding this comment

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

@mzeitlin11 added comment pointing back to issue

self.dtype = kwds["dtype"].copy()
else:
self.dtype = kwds["dtype"]
self.thousands = kwds["thousands"]
self.decimal = kwds["decimal"]

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,24 @@ def test_ignore_chartsheets_by_int(self, request, read_ext):
):
pd.read_excel("chartsheet" + read_ext, sheet_name=1)

def test_dtype_dict_unchanged_with_duplicate_columns(self, read_ext):
# GH 42462

filename = "test_common_headers" + read_ext
Copy link
Contributor

Choose a reason for hiding this comment

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

can you not use existing data? or simply do a round trip; i don't want to add even more files like this

dtype_dict = {"a": str, "b": str, "c": str}
dtype_dict_copy = dtype_dict.copy()
result = pd.read_excel(filename, dtype=dtype_dict)
expected = DataFrame(
{
"a": ["1", "2", "3"],
"a.1": ["1", "2", "3"],
"b": ["b1", "b2", "b3"],
"c": ["c1", "c2", "c3"],
}
)
assert dtype_dict == dtype_dict_copy, "dtype dict changed"
Copy link
Member

Choose a reason for hiding this comment

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

Can we also check that the resulting frame is as expected? (I know this is focusing on the dtypes dict, but may as well also test the reading portion here since unlikely we have great coverage for dtypes dict with duplicate cols)

tm.assert_frame_equal(result, expected)


class TestExcelFileRead:
@pytest.fixture(autouse=True)
Expand Down