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

fix(python): address issue with inadvertently shared options dict in read_excel #11908

Merged
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions py-polars/polars/io/spreadsheet/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def read_excel(
engine: Literal["xlsx2csv", "openpyxl", "pyxlsb"] | None = ...,
xlsx2csv_options: dict[str, Any] | None = ...,
read_csv_options: dict[str, Any] | None = ...,
schema_overrides: SchemaDict | None = None,
schema_overrides: SchemaDict | None = ...,
raise_if_empty: bool = ...,
) -> pl.DataFrame:
...
Expand All @@ -43,7 +43,7 @@ def read_excel(
engine: Literal["xlsx2csv", "openpyxl", "pyxlsb"] | None = ...,
xlsx2csv_options: dict[str, Any] | None = ...,
read_csv_options: dict[str, Any] | None = ...,
schema_overrides: SchemaDict | None = None,
schema_overrides: SchemaDict | None = ...,
raise_if_empty: bool = ...,
) -> pl.DataFrame:
...
Expand All @@ -58,7 +58,7 @@ def read_excel(
engine: Literal["xlsx2csv", "openpyxl", "pyxlsb"] | None = ...,
xlsx2csv_options: dict[str, Any] | None = ...,
read_csv_options: dict[str, Any] | None = ...,
schema_overrides: SchemaDict | None = None,
schema_overrides: SchemaDict | None = ...,
raise_if_empty: bool = ...,
) -> NoReturn:
...
Expand All @@ -75,7 +75,7 @@ def read_excel(
engine: Literal["xlsx2csv", "openpyxl", "pyxlsb"] | None = ...,
xlsx2csv_options: dict[str, Any] | None = ...,
read_csv_options: dict[str, Any] | None = ...,
schema_overrides: SchemaDict | None = None,
schema_overrides: SchemaDict | None = ...,
raise_if_empty: bool = ...,
) -> dict[str, pl.DataFrame]:
...
Expand All @@ -90,7 +90,7 @@ def read_excel(
engine: Literal["xlsx2csv", "openpyxl", "pyxlsb"] | None = ...,
xlsx2csv_options: dict[str, Any] | None = ...,
read_csv_options: dict[str, Any] | None = ...,
schema_overrides: SchemaDict | None = None,
schema_overrides: SchemaDict | None = ...,
raise_if_empty: bool = ...,
) -> pl.DataFrame:
...
Expand All @@ -105,7 +105,7 @@ def read_excel(
engine: Literal["xlsx2csv", "openpyxl", "pyxlsb"] | None = ...,
xlsx2csv_options: dict[str, Any] | None = ...,
read_csv_options: dict[str, Any] | None = ...,
schema_overrides: SchemaDict | None = None,
schema_overrides: SchemaDict | None = ...,
raise_if_empty: bool = ...,
) -> dict[str, pl.DataFrame]:
...
Expand Down Expand Up @@ -548,6 +548,7 @@ def _csv_buffer_to_frame(
raise ParameterCollisionError(
"cannot specify columns in both `schema_overrides` and `read_csv_options['dtypes']`"
)
read_csv_options = read_csv_options.copy()
read_csv_options["dtypes"] = {**csv_dtypes, **schema_overrides}

# otherwise rewind the buffer and parse as csv
Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/unit/io/test_spreadsheet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import warnings
from collections import OrderedDict
from datetime import date, datetime
from io import BytesIO
from typing import TYPE_CHECKING, Any, Callable, Literal
Expand Down Expand Up @@ -275,6 +276,22 @@ def test_schema_overrides(path_xlsx: Path, path_xlsb: Path, path_ods: Path) -> N
read_csv_options={"dtypes": {"cardinality": pl.Int32}},
)

# read multiple sheets in conjunction with 'schema_overrides'
# (note: reading the same sheet twice simulates the issue in #11850)
overrides = OrderedDict(
[
("cardinality", pl.UInt32),
("rows_by_key", pl.Float32),
("iter_groups", pl.Float64),
]
)
df = pl.read_excel( # type: ignore[call-overload]
path_xlsx,
sheet_name=["test4", "test4"],
schema_overrides=overrides,
)
assert df["test4"].schema == overrides


def test_unsupported_engine() -> None:
with pytest.raises(NotImplementedError):
Expand Down