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

DEPR: **kwargs in ExcelWriter #40430

Merged
merged 6 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ Deprecations
- Deprecated casting ``datetime.date`` objects to ``datetime64`` when used as ``fill_value`` in :meth:`DataFrame.unstack`, :meth:`DataFrame.shift`, :meth:`Series.shift`, and :meth:`DataFrame.reindex`, pass ``pd.Timestamp(dateobj)`` instead (:issue:`39767`)
- Deprecated :meth:`.Styler.set_na_rep` and :meth:`.Styler.set_precision` in favour of :meth:`.Styler.format` with ``na_rep`` and ``precision`` as existing and new input arguments respectively (:issue:`40134`)
- Deprecated allowing partial failure in :meth:`Series.transform` and :meth:`DataFrame.transform` when ``func`` is list-like or dict-like; will raise if any function fails on a column in a future version (:issue:`40211`)
- Deprecated the use of ``**kwargs`` in :class:`.ExcelWriter`; use the keyword argument ``engine_kwargs`` instead (:issue:`40430`)

.. ---------------------------------------------------------------------------

Expand Down
32 changes: 30 additions & 2 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,14 @@ class ExcelWriter(metaclass=abc.ABCMeta):
be parsed by ``fsspec``, e.g., starting "s3://", "gcs://".

.. versionadded:: 1.2.0
engine_kwargs : dict, optional
Keyword arguments to be passed into the engine.
jreback marked this conversation as resolved.
Show resolved Hide resolved
**kwargs : dict, optional
Keyword arguments to be passed into the engine.

.. deprecated:: 1.3.0

Use engine_kwargs instead.

Attributes
----------
Expand Down Expand Up @@ -745,7 +753,26 @@ class ExcelWriter(metaclass=abc.ABCMeta):
# You also need to register the class with ``register_writer()``.
# Technically, ExcelWriter implementations don't need to subclass
# ExcelWriter.
def __new__(cls, path, engine=None, **kwargs):
def __new__(
cls,
path: Union[FilePathOrBuffer, ExcelWriter],
engine=None,
date_format=None,
datetime_format=None,
mode: str = "w",
storage_options: StorageOptions = None,
engine_kwargs: Optional[Dict] = None,
**kwargs,
):
if kwargs:
if engine_kwargs is not None:
raise ValueError("Cannot use both engine_kwargs and **kwargs")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test for this

warnings.warn(
"Use of **kwargs is deprecated, use engine_kwargs instead.",
FutureWarning,
stacklevel=2,
)
jreback marked this conversation as resolved.
Show resolved Hide resolved

# only switch class if generic(ExcelWriter)

if cls is ExcelWriter:
Expand Down Expand Up @@ -835,7 +862,8 @@ def __init__(
datetime_format=None,
mode: str = "w",
storage_options: StorageOptions = None,
**engine_kwargs,
engine_kwargs: Optional[Dict] = None,
**kwargs,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and not do this

):
# validate that this engine can handle the extension
if isinstance(path, str):
Expand Down
11 changes: 7 additions & 4 deletions pandas/io/excel/_odswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ def __init__(
self,
path: str,
engine: Optional[str] = None,
date_format=None,
datetime_format=None,
mode: str = "w",
storage_options: StorageOptions = None,
**engine_kwargs,
engine_kwargs: Optional[Dict[str, Any]] = None,
):
from odf.opendocument import OpenDocumentSpreadsheet

engine_kwargs["engine"] = engine

if mode == "a":
raise ValueError("Append mode is not supported with odf!")

super().__init__(
path, mode=mode, storage_options=storage_options, **engine_kwargs
path,
mode=mode,
storage_options=storage_options,
engine_kwargs=engine_kwargs,
)

self.book = OpenDocumentSpreadsheet()
Expand Down
10 changes: 8 additions & 2 deletions pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mmap
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Optional,
Expand Down Expand Up @@ -35,15 +36,20 @@ def __init__(
self,
path,
engine=None,
date_format=None,
datetime_format=None,
mode: str = "w",
storage_options: StorageOptions = None,
**engine_kwargs,
engine_kwargs: Optional[Dict[str, Any]] = None,
):
# Use the openpyxl module as the Excel writer.
from openpyxl.workbook import Workbook

super().__init__(
path, mode=mode, storage_options=storage_options, **engine_kwargs
path,
mode=mode,
storage_options=storage_options,
engine_kwargs=engine_kwargs,
)

# ExcelWriter replaced "a" by "r+" to allow us to first read the excel file from
Expand Down
8 changes: 6 additions & 2 deletions pandas/io/excel/_xlsxwriter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import (
Any,
Dict,
List,
Optional,
Tuple,
)

Expand Down Expand Up @@ -175,11 +177,13 @@ def __init__(
datetime_format=None,
mode: str = "w",
storage_options: StorageOptions = None,
**engine_kwargs,
engine_kwargs: Optional[Dict[str, Any]] = None,
):
# Use the xlsxwriter module as the Excel writer.
from xlsxwriter import Workbook

engine_kwargs = engine_kwargs or {}

if mode == "a":
raise ValueError("Append mode is not supported with xlsxwriter!")

Expand All @@ -190,7 +194,7 @@ def __init__(
datetime_format=datetime_format,
mode=mode,
storage_options=storage_options,
**engine_kwargs,
engine_kwargs=engine_kwargs,
)

self.book = Workbook(self.handles.handle, **engine_kwargs)
Expand Down
13 changes: 9 additions & 4 deletions pandas/io/excel/_xlwt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import (
TYPE_CHECKING,
Any,
Dict,
Optional,
)

import pandas._libs.json as json
Expand All @@ -21,21 +23,24 @@ def __init__(
self,
path,
engine=None,
date_format=None,
datetime_format=None,
encoding=None,
mode: str = "w",
storage_options: StorageOptions = None,
**engine_kwargs,
engine_kwargs: Optional[Dict[str, Any]] = None,
):
# Use the xlwt module as the Excel writer.
import xlwt

engine_kwargs["engine"] = engine

if mode == "a":
raise ValueError("Append mode is not supported with xlwt!")

super().__init__(
path, mode=mode, storage_options=storage_options, **engine_kwargs
path,
mode=mode,
storage_options=storage_options,
engine_kwargs=engine_kwargs,
)

if encoding is None:
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from functools import partial
from io import BytesIO
import os
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -1382,6 +1383,25 @@ def check_called(func):
with tm.ensure_clean("something.xls") as filepath:
check_called(lambda: df.to_excel(filepath, engine="dummy"))

@pytest.mark.parametrize(
"ext",
[
pytest.param(".xlsx", marks=td.skip_if_no("xlsxwriter")),
pytest.param(".xlsx", marks=td.skip_if_no("openpyxl")),
pytest.param(".ods", marks=td.skip_if_no("odf")),
],
)
def test_kwargs_deprecated(self, ext):
# GH 40430
msg = re.escape("Use of **kwargs is deprecated")
with tm.assert_produces_warning(FutureWarning, match=msg):
with tm.ensure_clean(ext) as path:
try:
with ExcelWriter(path, kwarg=1):
pass
except TypeError:
pass


@td.skip_if_no("xlrd")
@td.skip_if_no("openpyxl")
Expand Down