Skip to content

API: Inconsistent use of apply arguments args and **kwargs #47543

@datapythonista

Description

@datapythonista

The signature of Series.apply (and DataFrame.apply) seems inconsistent and cumbersome (see args and **kwargs:

Series.apply(func, convert_dtype=True, args=(), **kwargs)

I'd expect to have args and kwargs, or *args and **kwargs, not a mix of both. I assume this was implemented to allow convert_dtype to be passed as a positonal argument:

my_series.apply(my_func, this_is_convert_dtype)

But this seems confusing and not very intuitive:

my_series.apply(my_func, this_arg_is_for_apply, this_must_be_a_tuple_with_all_args, this_is_a_single_kwarg=True, this_is_another_kwarg=False)

To me, the API would be much clear if it was implemented as:

Series.apply(func, *, convert_dtype=True, args=(), kwargs={})

# or

Series.apply(func, convert_dtype=True, *, args=(), kwargs={})

And I think this can be easily done in two steps, warning the user first without breaking code. If there is agreement, I'd also do the same for the rest of the methods using this same pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignApplyApply, Aggregate, Transform, MapNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions