Skip to content

Commit 49455ce

Browse files
authored
Fix read_excel dtypes and converters to use Mapping not dict (#503)
1 parent f09268e commit 49455ce

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

pandas-stubs/io/excel/_base.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from collections.abc import (
22
Callable,
33
Hashable,
44
Iterable,
5+
Mapping,
56
Sequence,
67
)
78
from types import TracebackType
@@ -41,9 +42,9 @@ def read_excel(
4142
names: list[str] | None = ...,
4243
index_col: int | Sequence[int] | None = ...,
4344
usecols: Sequence[int] | Sequence[str] | Callable[[str], bool] | None = ...,
44-
dtype: str | Dtype | dict[str, str | Dtype] | None = ...,
45+
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
4546
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
46-
converters: dict[int | str, Callable[[object], object]] | None = ...,
47+
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
4748
true_values: Iterable[Hashable] | None = ...,
4849
false_values: Iterable[Hashable] | None = ...,
4950
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,
@@ -79,9 +80,9 @@ def read_excel(
7980
names: list[str] | None = ...,
8081
index_col: int | Sequence[int] | None = ...,
8182
usecols: Sequence[int] | Sequence[str] | Callable[[str], bool] | None = ...,
82-
dtype: str | Dtype | dict[str, str | Dtype] | None = ...,
83+
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
8384
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
84-
converters: dict[int | str, Callable[[object], object]] | None = ...,
85+
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
8586
true_values: Iterable[Hashable] | None = ...,
8687
false_values: Iterable[Hashable] | None = ...,
8788
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,

tests/test_io.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,15 @@ def test_read_excel_list():
705705
)
706706

707707

708+
def test_read_excel_dtypes():
709+
# GH 440
710+
df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"], "c": [10.0, 20.0, 30.3]})
711+
with ensure_clean(".xlsx") as path:
712+
check(assert_type(df.to_excel(path), None), type(None))
713+
dtypes = {"a": np.int64, "b": str, "c": np.float64}
714+
check(assert_type(read_excel(path, dtype=dtypes), pd.DataFrame), pd.DataFrame)
715+
716+
708717
def test_excel_writer():
709718
with ensure_clean(".xlsx") as path:
710719
with pd.ExcelWriter(path) as ew:

0 commit comments

Comments
 (0)