Skip to content

Commit 7e0f5ca

Browse files
authored
DEPR: **kwargs in ExcelWriter (#40430)
1 parent 8588651 commit 7e0f5ca

File tree

7 files changed

+98
-14
lines changed

7 files changed

+98
-14
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ Deprecations
405405
- 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`, :issue:`40425`)
406406
- 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`)
407407
- Deprecated support for ``np.ma.mrecords.MaskedRecords`` in the :class:`DataFrame` constructor, pass ``{name: data[name] for name in data.dtype.names}`` instead (:issue:`40363`)
408+
- Deprecated the use of ``**kwargs`` in :class:`.ExcelWriter`; use the keyword argument ``engine_kwargs`` instead (:issue:`40430`)
408409

409410
.. ---------------------------------------------------------------------------
410411

pandas/io/excel/_base.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,16 @@ class ExcelWriter(metaclass=abc.ABCMeta):
667667
be parsed by ``fsspec``, e.g., starting "s3://", "gcs://".
668668
669669
.. versionadded:: 1.2.0
670+
engine_kwargs : dict, optional
671+
Keyword arguments to be passed into the engine.
672+
673+
.. versionadded:: 1.3.0
674+
**kwargs : dict, optional
675+
Keyword arguments to be passed into the engine.
676+
677+
.. deprecated:: 1.3.0
678+
679+
Use engine_kwargs instead.
670680
671681
Attributes
672682
----------
@@ -745,7 +755,26 @@ class ExcelWriter(metaclass=abc.ABCMeta):
745755
# You also need to register the class with ``register_writer()``.
746756
# Technically, ExcelWriter implementations don't need to subclass
747757
# ExcelWriter.
748-
def __new__(cls, path, engine=None, **kwargs):
758+
def __new__(
759+
cls,
760+
path: Union[FilePathOrBuffer, ExcelWriter],
761+
engine=None,
762+
date_format=None,
763+
datetime_format=None,
764+
mode: str = "w",
765+
storage_options: StorageOptions = None,
766+
engine_kwargs: Optional[Dict] = None,
767+
**kwargs,
768+
):
769+
if kwargs:
770+
if engine_kwargs is not None:
771+
raise ValueError("Cannot use both engine_kwargs and **kwargs")
772+
warnings.warn(
773+
"Use of **kwargs is deprecated, use engine_kwargs instead.",
774+
FutureWarning,
775+
stacklevel=2,
776+
)
777+
749778
# only switch class if generic(ExcelWriter)
750779

751780
if cls is ExcelWriter:
@@ -835,7 +864,8 @@ def __init__(
835864
datetime_format=None,
836865
mode: str = "w",
837866
storage_options: StorageOptions = None,
838-
**engine_kwargs,
867+
engine_kwargs: Optional[Dict] = None,
868+
**kwargs,
839869
):
840870
# validate that this engine can handle the extension
841871
if isinstance(path, str):

pandas/io/excel/_odswriter.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@ def __init__(
2626
self,
2727
path: str,
2828
engine: Optional[str] = None,
29+
date_format=None,
30+
datetime_format=None,
2931
mode: str = "w",
3032
storage_options: StorageOptions = None,
31-
**engine_kwargs,
33+
engine_kwargs: Optional[Dict[str, Any]] = None,
3234
):
3335
from odf.opendocument import OpenDocumentSpreadsheet
3436

35-
engine_kwargs["engine"] = engine
36-
3737
if mode == "a":
3838
raise ValueError("Append mode is not supported with odf!")
3939

4040
super().__init__(
41-
path, mode=mode, storage_options=storage_options, **engine_kwargs
41+
path,
42+
mode=mode,
43+
storage_options=storage_options,
44+
engine_kwargs=engine_kwargs,
4245
)
4346

4447
self.book = OpenDocumentSpreadsheet()

pandas/io/excel/_openpyxl.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import mmap
44
from typing import (
55
TYPE_CHECKING,
6+
Any,
67
Dict,
78
List,
89
Optional,
@@ -35,15 +36,20 @@ def __init__(
3536
self,
3637
path,
3738
engine=None,
39+
date_format=None,
40+
datetime_format=None,
3841
mode: str = "w",
3942
storage_options: StorageOptions = None,
40-
**engine_kwargs,
43+
engine_kwargs: Optional[Dict[str, Any]] = None,
4144
):
4245
# Use the openpyxl module as the Excel writer.
4346
from openpyxl.workbook import Workbook
4447

4548
super().__init__(
46-
path, mode=mode, storage_options=storage_options, **engine_kwargs
49+
path,
50+
mode=mode,
51+
storage_options=storage_options,
52+
engine_kwargs=engine_kwargs,
4753
)
4854

4955
# ExcelWriter replaced "a" by "r+" to allow us to first read the excel file from

pandas/io/excel/_xlsxwriter.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import (
2+
Any,
23
Dict,
34
List,
5+
Optional,
46
Tuple,
57
)
68

@@ -175,11 +177,13 @@ def __init__(
175177
datetime_format=None,
176178
mode: str = "w",
177179
storage_options: StorageOptions = None,
178-
**engine_kwargs,
180+
engine_kwargs: Optional[Dict[str, Any]] = None,
179181
):
180182
# Use the xlsxwriter module as the Excel writer.
181183
from xlsxwriter import Workbook
182184

185+
engine_kwargs = engine_kwargs or {}
186+
183187
if mode == "a":
184188
raise ValueError("Append mode is not supported with xlsxwriter!")
185189

@@ -190,7 +194,7 @@ def __init__(
190194
datetime_format=datetime_format,
191195
mode=mode,
192196
storage_options=storage_options,
193-
**engine_kwargs,
197+
engine_kwargs=engine_kwargs,
194198
)
195199

196200
self.book = Workbook(self.handles.handle, **engine_kwargs)

pandas/io/excel/_xlwt.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import (
22
TYPE_CHECKING,
3+
Any,
34
Dict,
5+
Optional,
46
)
57

68
import pandas._libs.json as json
@@ -21,21 +23,24 @@ def __init__(
2123
self,
2224
path,
2325
engine=None,
26+
date_format=None,
27+
datetime_format=None,
2428
encoding=None,
2529
mode: str = "w",
2630
storage_options: StorageOptions = None,
27-
**engine_kwargs,
31+
engine_kwargs: Optional[Dict[str, Any]] = None,
2832
):
2933
# Use the xlwt module as the Excel writer.
3034
import xlwt
3135

32-
engine_kwargs["engine"] = engine
33-
3436
if mode == "a":
3537
raise ValueError("Append mode is not supported with xlwt!")
3638

3739
super().__init__(
38-
path, mode=mode, storage_options=storage_options, **engine_kwargs
40+
path,
41+
mode=mode,
42+
storage_options=storage_options,
43+
engine_kwargs=engine_kwargs,
3944
)
4045

4146
if encoding is None:

pandas/tests/io/excel/test_writers.py

+35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from functools import partial
77
from io import BytesIO
88
import os
9+
import re
910

1011
import numpy as np
1112
import pytest
@@ -1382,6 +1383,40 @@ def check_called(func):
13821383
with tm.ensure_clean("something.xls") as filepath:
13831384
check_called(lambda: df.to_excel(filepath, engine="dummy"))
13841385

1386+
@pytest.mark.parametrize(
1387+
"ext",
1388+
[
1389+
pytest.param(".xlsx", marks=td.skip_if_no("xlsxwriter")),
1390+
pytest.param(".xlsx", marks=td.skip_if_no("openpyxl")),
1391+
pytest.param(".ods", marks=td.skip_if_no("odf")),
1392+
],
1393+
)
1394+
def test_kwargs_deprecated(self, ext):
1395+
# GH 40430
1396+
msg = re.escape("Use of **kwargs is deprecated")
1397+
with tm.assert_produces_warning(FutureWarning, match=msg):
1398+
with tm.ensure_clean(ext) as path:
1399+
try:
1400+
with ExcelWriter(path, kwarg=1):
1401+
pass
1402+
except TypeError:
1403+
pass
1404+
1405+
@pytest.mark.parametrize(
1406+
"ext",
1407+
[
1408+
pytest.param(".xlsx", marks=td.skip_if_no("xlsxwriter")),
1409+
pytest.param(".xlsx", marks=td.skip_if_no("openpyxl")),
1410+
pytest.param(".ods", marks=td.skip_if_no("odf")),
1411+
],
1412+
)
1413+
def test_engine_kwargs_and_kwargs_raises(self, ext):
1414+
# GH 40430
1415+
msg = re.escape("Cannot use both engine_kwargs and **kwargs")
1416+
with pytest.raises(ValueError, match=msg):
1417+
with ExcelWriter("", engine_kwargs={"a": 1}, b=2):
1418+
pass
1419+
13851420

13861421
@td.skip_if_no("xlrd")
13871422
@td.skip_if_no("openpyxl")

0 commit comments

Comments
 (0)