'FutureWarning: Use of **kwargs is deprecated, use engine_kwargs instead' 指的是哪个参数?

Which arguments is 'FutureWarning: Use of **kwargs is deprecated, use engine_kwargs instead' refering to?

我从该代码中得到以下警告:

    file = r'.\changed_activities.xlsx'
    with pd.ExcelWriter(file,
                        engine='openpyxl',
                        mode='a',
                        if_sheet_exists='new') as writer:
        df.to_excel(writer, sheet_name=activity[0:30])


FutureWarning: Use of **kwargs is deprecated, use engine_kwargs instead. with pd.ExcelWriter(file,

我似乎无法从 documentation 中看出我需要替换我的哪些参数? 这些例子是例如这个,但我看不出它是如何转化为我的代码的。

with pd.ExcelWriter(
    "path_to_file.xlsx",
    engine="openpyxl",
    mode="a",
    engine_kwargs={"keep_vba": True}
) as writer:
    df.to_excel(writer, sheet_name="Sheet2") 

也许在一个更开放的问题中:how/where我能在一般情况下理解类似的东西吗?

不幸的是,无法从文档中推断出这一点(至少不能以它们当前的措辞方式),因为它没有解释触发警告的原因。

如果我们查看 pd.ExcelWritersource code

def __new__(
        cls,
        path: FilePath | WriteExcelBuffer | ExcelWriter,
        engine: str | None = None,
        date_format: str | None = None,
        datetime_format: str | None = None,
        mode: str = "w",
        storage_options: StorageOptions = None,
        if_sheet_exists: Literal["error", "new", "replace", "overlay"] | None = None,
        engine_kwargs: dict | None = None,
        **kwargs,
    ):
        if kwargs:
            if engine_kwargs is not None:
                raise ValueError("Cannot use both engine_kwargs and **kwargs")
            warnings.warn(
                "Use of **kwargs is deprecated, use engine_kwargs instead.",
                FutureWarning,
                stacklevel=find_stack_level(),
            )

传递给 pd.ExcelWriter 的任何关键字参数 而不是 指定为 __new__ 的参数将触发 if kwargs 条件。

在你的情况下,你应该将 sheet_name 参数移动到 engine_kwargs 字典。

使用

pd.ExcelWriter('out.xlsx', engine='xlsxwriter', engine_kwargs={'options':{'strings_to_urls': False}})

而不是

pd.ExcelWriter('out.xlsx', engine='xlsxwriter', options={'strings_to_urls': False}})