From 9a649bb0bfb1056ac3c944bb450ca90d738573c9 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Mon, 9 Jan 2023 16:27:17 -0500 Subject: [PATCH] Fix read_excel dtypes and converters to use Mapping not dict --- pandas-stubs/io/excel/_base.pyi | 9 +++++---- tests/test_io.py | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/io/excel/_base.pyi b/pandas-stubs/io/excel/_base.pyi index 186fc178c..545783343 100644 --- a/pandas-stubs/io/excel/_base.pyi +++ b/pandas-stubs/io/excel/_base.pyi @@ -2,6 +2,7 @@ from collections.abc import ( Callable, Hashable, Iterable, + Mapping, Sequence, ) from types import TracebackType @@ -41,9 +42,9 @@ def read_excel( names: list[str] | None = ..., index_col: int | Sequence[int] | None = ..., usecols: Sequence[int] | Sequence[str] | Callable[[str], bool] | None = ..., - dtype: str | Dtype | dict[str, str | Dtype] | None = ..., + dtype: str | Dtype | Mapping[str, str | Dtype] | None = ..., engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ..., - converters: dict[int | str, Callable[[object], object]] | None = ..., + converters: Mapping[int | str, Callable[[object], object]] | None = ..., true_values: Iterable[Hashable] | None = ..., false_values: Iterable[Hashable] | None = ..., skiprows: int | Sequence[int] | Callable[[object], bool] | None = ..., @@ -79,9 +80,9 @@ def read_excel( names: list[str] | None = ..., index_col: int | Sequence[int] | None = ..., usecols: Sequence[int] | Sequence[str] | Callable[[str], bool] | None = ..., - dtype: str | Dtype | dict[str, str | Dtype] | None = ..., + dtype: str | Dtype | Mapping[str, str | Dtype] | None = ..., engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ..., - converters: dict[int | str, Callable[[object], object]] | None = ..., + converters: Mapping[int | str, Callable[[object], object]] | None = ..., true_values: Iterable[Hashable] | None = ..., false_values: Iterable[Hashable] | None = ..., skiprows: int | Sequence[int] | Callable[[object], bool] | None = ..., diff --git a/tests/test_io.py b/tests/test_io.py index 2817b170d..3c8b17c05 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -705,6 +705,15 @@ def test_read_excel_list(): ) +def test_read_excel_dtypes(): + # GH 440 + df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"], "c": [10.0, 20.0, 30.3]}) + with ensure_clean(".xlsx") as path: + check(assert_type(df.to_excel(path), None), type(None)) + dtypes = {"a": np.int64, "b": str, "c": np.float64} + check(assert_type(read_excel(path, dtype=dtypes), pd.DataFrame), pd.DataFrame) + + def test_excel_writer(): with ensure_clean(".xlsx") as path: with pd.ExcelWriter(path) as ew: