Skip to content

CLN: remove redundant openpyxl type conversions #39782

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 2 commits into from
Feb 15, 2021
Merged
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
16 changes: 3 additions & 13 deletions pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,24 +509,14 @@ def get_sheet_by_index(self, index: int):

def _convert_cell(self, cell, convert_float: bool) -> Scalar:

from openpyxl.cell.cell import TYPE_BOOL, TYPE_ERROR, TYPE_NUMERIC
from openpyxl.cell.cell import TYPE_ERROR, TYPE_NUMERIC

if cell.value is None:
return "" # compat with xlrd
elif cell.is_date:
return cell.value
elif cell.data_type == TYPE_ERROR:
return np.nan
elif cell.data_type == TYPE_BOOL:
return bool(cell.value)
Copy link
Member

Choose a reason for hiding this comment

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

Are there tests for bool/int/etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes there are. If I break the function to return

  • all numbers as float, 198 tests fail
  • all numbers as int, 211 tests fail
  • all bools as int, 20 tests fail
  • all bools as str, zero tests fail because TextParser does its own conversions

Thanks for taking them time to look at my first pull request. Let me know if there's anything that needs to be improved.

Copy link
Member

Choose a reason for hiding this comment

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

Great - thanks for checking.

elif cell.data_type == TYPE_NUMERIC:
# GH5394
if convert_float:
val = int(cell.value)
if val == cell.value:
return val
else:
return float(cell.value)
elif not convert_float and cell.data_type == TYPE_NUMERIC:
return float(cell.value)

return cell.value

Expand Down