8000 BUG: read_excel with openpyxl and missing dimension by rhshadrach · Pull Request #39486 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: read_excel with openpyxl and missing dimension #39486

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 12 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
BUG: read_excel with openpyxl and missing dimension
  • Loading branch information
rhshadrach committed Jan 30, 2021
commit 2bcf35b80ba765e8aee002bb0a19acd2799a5cc5
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~

-
- Fixed bug in :func:`pandas.read_excel` producing incorrect results when the engine ``openpyxl`` is used and the excel file is missing dimension information (:issue:`38956`)
-

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,10 @@ def get_sheet_data(self, sheet, convert_float: bool) -> List[List[Scalar]]:
for row in sheet.rows:
data.append([self._convert_cell(cell, convert_float) for cell in row])

# openpyxl may not have the correct padding if the dimension tag is
# not specified or is incorrect
max_width = max(len(row) for row in data)
if min(len(row) for row in data) < max_width:
data = [row + (max_width - len(row)) * [""] for row in data]

return data
Binary file added pandas/tests/io/data/excel/no_dimension.xlsx
Binary file not shown.
21 changes: 21 additions & 0 deletions pandas/tests/io/excel/test_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,24 @@ def test_to_excel_with_openpyxl_engine(ext):
).highlight_max()

styled.to_excel(filename, engine="openpyxl")


@pytest.mark.parametrize(
"header, expected_data",
[
(
0,
{
"Title": [np.nan, "A", 1, 2, 3],
"Unnamed: 1": [np.nan, "B", 4, 5, 6],
"Unnamed: 2": [np.nan, "C", 7, 8, 9],
},
),
(2, {"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}),
],
)
def test_read_with_missing_dimension(datapath, ext, header, expected_data):
path = datapath("io", "data", "excel", f"no_dimension{ext}")
result = pd.read_excel(path, header=header)
expected = DataFrame(expected_data)
tm.assert_frame_equal(result, expected)
0