pythonpandassignature

How to get information of a function and its arguments in Python


I ran below to get information about the list of arguments and default values of a function/method.

import pandas as pd
import inspect
inspect.getfullargspec(pd.drop_duplicates)

Results:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pandas' has no attribute 'drop_duplicates'
>>> inspect.getfullargspec(pd.drop_duplicates)

What is the correct way to fetch such information from any function/method?


Solution

  • The issue is that the drop_duplicates method exists as a method on a pandas.DataFrame object, i.e., pandas.DataFrame.drop_duplicates not directly on the pandas module.

    With that being said you might also want to check out inspect.signature as an alternative to inspect.getfullargspec:

    >>> import inspect
    >>> import pandas as pd
    >>> method = pd.DataFrame.drop_duplicates
    >>> inspect.getfullargspec(method)
    FullArgSpec(args=['self', 'subset'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=['keep', 'inplace', 'ignore_index'], kwonlydefaults={'keep': 'first', 'inplace': False, 'ignore_index': False}, annotations={'return': 'DataFrame | None', 'subset': 'Hashable | Sequence[Hashable] | None', 'keep': 'DropKeep', 'inplace': 'bool', 'ignore_index': 'bool'})
    >>> inspect.signature(method)
    <Signature (self, subset: 'Hashable | Sequence[Hashable] | None' = None, *, keep: 'DropKeep' = 'first', inplace: 'bool' = False, ignore_index: 'bool' = False) -> 'DataFrame | None'>