-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add calamite engine to read_excel
#50581
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
Changes from 32 commits
30da9a4
a47d3fb
6c1dd87
fd06ad9
8b6200a
6a8d822
efcb2fc
e1105de
6b50e0c
0784733
5971199
655318b
cc049cf
038133e
52c2cbd
2dc5e02
2076e11
6b0a7ac
a614089
256f9f9
eee8b4e
9fc2209
cf1268a
bebfec5
9019904
08a5616
677a224
8c55e5d
d817999
12aaf19
255e8fb
500fa9f
5d94728
15874c3
89ae49e
33e5b7e
85d31ec
a0d4193
a6b6fb2
0a431c5
745cd09
942a16a
8803ca9
2f5ffba
b8b1a9a
f5ab40d
02c2e7f
74a3e70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,6 @@ dependencies: | |
- xlrd | ||
- xlsxwriter | ||
- zstandard | ||
|
||
- pip: | ||
- python-calamine |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,3 +69,6 @@ dependencies: | |
- pandas-gbq | ||
- pyyaml | ||
- py | ||
|
||
- pip: | ||
- python-calamine |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,4 @@ dependencies: | |
|
||
- pip: | ||
- pyqt5==5.15.1 | ||
- python-calamine==0.0.7 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,3 +54,6 @@ dependencies: | |
- xlrd | ||
- xlsxwriter | ||
- zstandard | ||
|
||
- pip: | ||
- python-calamine |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,6 @@ dependencies: | |
- xlrd | ||
- xlsxwriter | ||
- zstandard | ||
|
||
- pip: | ||
- python-calamine |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,6 @@ dependencies: | |
- xlrd | ||
- xlsxwriter | ||
- zstandard | ||
|
||
- pip: | ||
- python-calamine |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,3 +117,4 @@ dependencies: | |
|
||
- pip: | ||
- sphinx-toggleprompt | ||
- python-calamine |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import ( | ||
date, | ||
datetime, | ||
time, | ||
) | ||
from tempfile import NamedTemporaryFile | ||
from typing import Union | ||
|
||
from pandas._typing import ( | ||
FilePath, | ||
ReadBuffer, | ||
Scalar, | ||
StorageOptions, | ||
) | ||
from pandas.compat._optional import import_optional_dependency | ||
|
||
import pandas as pd | ||
|
||
from pandas.io.common import stringify_path | ||
from pandas.io.excel._base import ( | ||
BaseExcelReader, | ||
inspect_excel_format, | ||
) | ||
|
||
ValueT = Union[int, float, str, bool, time, date, datetime] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we stick with the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type is for internal using in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this into the function it is actually used in? Sounds like it has a pretty localized use, so no need to be in the global namespace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I see in https://peps.python.org/pep-0613/#scope-restrictions, typealiases can't defined within function scope. But, I can make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah fair point. At the very least I think a more descriptive name than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed in _CellValueT. |
||
|
||
|
||
class __calamine__: | ||
pass | ||
|
||
|
||
class CalamineExcelReader(BaseExcelReader): | ||
book: str | ||
_sheet_names: list[str] | None = None | ||
|
||
def __init__( | ||
self, | ||
filepath_or_buffer: FilePath | ReadBuffer[bytes], | ||
storage_options: StorageOptions = None, | ||
) -> None: | ||
import_optional_dependency("python_calamine") | ||
super().__init__(filepath_or_buffer, storage_options=storage_options) | ||
|
||
@property | ||
def _workbook_class(self) -> type[__calamine__]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love the idea of typing but I don't think this is a correct type, especially since it will be inconsistent with other reader classes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
return __calamine__ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the idea here that this doesn't need a real value to fulfill the requirements of the abstract class? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
def load_workbook(self, filepath_or_buffer) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just seems to return a file name whereas the other readers return a physical workbook object. Does calamine not have a similar concept? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I can add a wrapper for result of get_sheet_data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea I think we should follow the requirements of the ABC here - otherwise things get pretty wonky across the various implementations. How hard would it be to implement that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made simple implementation of these classes (kostyafarber#5), but I need some time to test it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WillAyd I rewrote this code to use classes. |
||
if hasattr(filepath_or_buffer, "read") and hasattr(filepath_or_buffer, "seek"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can get rid of all of this if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
ext = inspect_excel_format(filepath_or_buffer) | ||
with NamedTemporaryFile(suffix=f".{ext}", delete=False) as tmp_file: | ||
filepath_or_buffer.seek(0) | ||
tmp_file.write(filepath_or_buffer.read()) | ||
filepath_or_buffer = tmp_file.name | ||
else: | ||
filepath_or_buffer = stringify_path(filepath_or_buffer) | ||
|
||
assert isinstance(filepath_or_buffer, str) | ||
|
||
from python_calamine import get_sheet_names | ||
|
||
self._sheet_names = get_sheet_names(filepath_or_buffer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should be set here - seems duplicate of the code in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put this code in load_workbook to check file by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment as above - we should trust the ABC to do the work and just implement the requirements it already has set forth There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
return filepath_or_buffer | ||
|
||
@property | ||
def sheet_names(self) -> list[str]: | ||
from python_calamine import get_sheet_names | ||
|
||
if self._sheet_names is None: | ||
self._sheet_names = get_sheet_names(self.book) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to accomplish this without assigning to a new attribute? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
return self._sheet_names | ||
|
||
def get_sheet_by_name(self, name: str) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is supposed to return a sheet not an |
||
self.raise_if_bad_sheet_by_name(name) | ||
return self.sheet_names.index(name) | ||
|
||
def get_sheet_by_index(self, index: int) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
self.raise_if_bad_sheet_by_index(index) | ||
return index | ||
|
||
def get_sheet_data( | ||
self, sheet: int, file_rows_needed: int | None = None | ||
) -> list[list[Scalar]]: | ||
def _convert_cell(value: ValueT) -> Scalar: | ||
if isinstance(value, float): | ||
val = int(value) | ||
if val == value: | ||
return val | ||
else: | ||
return value | ||
elif isinstance(value, date): | ||
return pd.Timestamp(value) | ||
elif isinstance(value, time): | ||
return value.isoformat() | ||
|
||
return value | ||
|
||
from python_calamine import get_sheet_data | ||
|
||
rows = get_sheet_data(self.book, sheet, 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 | ||
|
||
return data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are in the processing of cutting 2.0.0 now; at this point should target 2.1.0 for this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kostyafarber, hi! Can you merge main in issue-50395?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dimastbk merged!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is still in the v2.0.0 whatsnew
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I asked for merging main in pr branch. Now fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should revert any changes to this file - the v2.0.0.rst file shouldn't be touched as part of this PR