Skip to content

Commit 0d54b01

Browse files
authored
Add calamine Excel reader (#898)
* Extract ReadEngine and WriteEngine types for Excel * Add calamine Excel reader engine pandas-dev/pandas#54998 * Add test for read_excel engine
1 parent c74a250 commit 0d54b01

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

pandas-stubs/_typing.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -780,4 +780,7 @@ TimeGrouperOrigin: TypeAlias = (
780780
Timestamp | Literal["epoch", "start", "start_day", "end", "end_day"]
781781
)
782782

783+
ExcelReadEngine: TypeAlias = Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"]
784+
ExcelWriteEngine: TypeAlias = Literal["openpyxl", "odf", "xlsxwriter"]
785+
783786
__all__ = ["npt", "type_t"]

pandas-stubs/io/excel/_base.pyi

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ from pandas._libs.lib import NoDefault
2323
from pandas._typing import (
2424
Dtype,
2525
DtypeBackend,
26+
ExcelReadEngine,
27+
ExcelWriteEngine,
2628
FilePath,
2729
IntStrT,
2830
ListLikeHashable,
@@ -50,7 +52,7 @@ def read_excel(
5052
index_col: int | Sequence[int] | None = ...,
5153
usecols: str | UsecolsArgType = ...,
5254
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
53-
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
55+
engine: ExcelReadEngine | None = ...,
5456
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
5557
true_values: Iterable[Hashable] | None = ...,
5658
false_values: Iterable[Hashable] | None = ...,
@@ -92,7 +94,7 @@ def read_excel(
9294
index_col: int | Sequence[int] | None = ...,
9395
usecols: str | UsecolsArgType = ...,
9496
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
95-
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
97+
engine: ExcelReadEngine | None = ...,
9698
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
9799
true_values: Iterable[Hashable] | None = ...,
98100
false_values: Iterable[Hashable] | None = ...,
@@ -135,7 +137,7 @@ def read_excel( # type: ignore[misc]
135137
index_col: int | Sequence[int] | None = ...,
136138
usecols: str | UsecolsArgType = ...,
137139
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
138-
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
140+
engine: ExcelReadEngine | None = ...,
139141
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
140142
true_values: Iterable[Hashable] | None = ...,
141143
false_values: Iterable[Hashable] | None = ...,
@@ -177,7 +179,7 @@ def read_excel(
177179
index_col: int | Sequence[int] | None = ...,
178180
usecols: str | UsecolsArgType = ...,
179181
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
180-
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
182+
engine: ExcelReadEngine | None = ...,
181183
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
182184
true_values: Iterable[Hashable] | None = ...,
183185
false_values: Iterable[Hashable] | None = ...,
@@ -206,7 +208,7 @@ class ExcelWriter:
206208
def __init__(
207209
self,
208210
path: FilePath | WriteExcelBuffer | ExcelWriter,
209-
engine: Literal["auto", "openpyxl", "odf", "xlsxwriter"] | None = ...,
211+
engine: ExcelWriteEngine | Literal["auto"] | None = ...,
210212
date_format: str | None = ...,
211213
datetime_format: str | None = ...,
212214
mode: Literal["w", "a"] = ...,
@@ -217,7 +219,7 @@ class ExcelWriter:
217219
@property
218220
def supported_extensions(self) -> tuple[str, ...]: ...
219221
@property
220-
def engine(self) -> Literal["openpyxl", "odf", "xlsxwriter"]: ...
222+
def engine(self) -> ExcelWriteEngine: ...
221223
@property
222224
def sheets(self) -> dict[str, Any]: ...
223225
@property

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ SQLAlchemy = ">=2.0.12"
6464
types-python-dateutil = ">=2.8.19"
6565
beautifulsoup4 = ">=4.12.2"
6666
html5lib = ">=1.1"
67+
python-calamine = "0.2.0"
6768

6869
[build-system]
6970
requires = ["poetry-core>=1.0.0"]

tests/test_io.py

+10
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,16 @@ def test_read_excel() -> None:
961961
),
962962
pd.DataFrame,
963963
)
964+
check(
965+
assert_type(
966+
pd.read_excel(
967+
path,
968+
engine="calamine",
969+
),
970+
pd.DataFrame,
971+
),
972+
pd.DataFrame,
973+
)
964974
if TYPE_CHECKING_INVALID_USAGE:
965975
pd.read_excel(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportArgumentType]
966976

0 commit comments

Comments
 (0)