Skip to content
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

Difference between calamine and openpyxl readers fixed #59188

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 18 additions & 14 deletions pandas/io/excel/_calamine.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
TYPE_CHECKING,
Any,
Union,
cast,
)

from pandas._typing import Scalar
from pandas.compat._optional import import_optional_dependency
from pandas.util._decorators import doc

Expand All @@ -28,13 +30,11 @@

from pandas._typing import (
FilePath,
NaTType,
ReadBuffer,
Scalar,
StorageOptions,
)

_CellValue = Union[int, float, str, bool, time, date, datetime, timedelta]
_CellValueT = Union[int, float, str, bool, time, date, datetime, timedelta]
Copy link
Member

Choose a reason for hiding this comment

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

Why is this changing? I think we only use T suffix for TypeVar, not type aliases.



class CalamineReader(BaseExcelReader["CalamineWorkbook"]):
Expand Down Expand Up @@ -75,8 +75,7 @@ def load_workbook(
from python_calamine import load_workbook

return load_workbook(
filepath_or_buffer,
**engine_kwargs,
filepath_or_buffer, **engine_kwargs # type: ignore[arg-type]
)

@property
Expand All @@ -99,26 +98,31 @@ def get_sheet_by_index(self, index: int) -> CalamineSheet:

def get_sheet_data(
self, sheet: CalamineSheet, file_rows_needed: int | None = None
) -> list[list[Scalar | NaTType | time]]:
def _convert_cell(value: _CellValue) -> Scalar | NaTType | time:
) -> list[list[Scalar]]:
def _convert_cell(value: _CellValueT) -> Scalar:
# Avoid explicit conversion to pd.Timestamp and pd.Timedelta
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this comment is necessary/helpful.

if isinstance(value, float):
val = int(value)
if val == value:
return val
else:
return value
elif isinstance(value, date):
return pd.Timestamp(value)
return value
elif isinstance(value, timedelta):
return pd.Timedelta(value)
elif isinstance(value, time):
return value
elif isinstance(value, time):
Comment on lines 110 to +114
Copy link
Member

Choose a reason for hiding this comment

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

I think these can just be removed. We only need elif instance(value, ...) if we are going to do something besides just returning value.

# cast needed here because Scalar doesn't include datetime.time
return cast(Scalar, value)

return value

rows: list[list[_CellValue]] = sheet.to_python(
skip_empty_area=False, nrows=file_rows_needed
)
data = [[_convert_cell(cell) for cell in row] for row in rows]
rows: list[list[_CellValueT]] = sheet.to_python(skip_empty_area=False)
data: list[list[Scalar]] = []

for row in rows:
data.append([_convert_cell(cell) for cell in row])
if file_rows_needed is not None and len(data) >= file_rows_needed:
break
Comment on lines +120 to +126
Copy link
Member

Choose a reason for hiding this comment

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

Why is this changing?


return data
Loading