Home > Mobile >  Which arguments is 'FutureWarning: Use of **kwargs is deprecated, use engine_kwargs instead
Which arguments is 'FutureWarning: Use of **kwargs is deprecated, use engine_kwargs instead

Time:02-17

I got the following waring from that code:

    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,

What I can't seem to read from the documentation is which of my arguments I need to replace? the examples are e.g. this, but I don't see how that translates to my code.

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") 

And maybe in a more open question: how/where can I understand something like that that in a general case?

CodePudding user response:

Unfortunately there is no way to deduce this from the documentation (at least not in the way they are currently worded) since it doesn't explain what triggers the warning.

If we look at the source code of pd.ExcelWriter:

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(),
            )

Any keyword argument that is passed to pd.ExcelWriter which is not specified as an argument of __new__ will trigger the if kwargs condition.

In your case, you should move the sheet_name argument to the engine_kwargs dict.

  • Related