Home > OS >  Pandas `replace` * in signature
Pandas `replace` * in signature

Time:12-23

Following is the signature for the replace method in Pandas API (https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html)

DataFrame.replace(to_replace=None, value=_NoDefault.no_default, *, 
     inplace=False, limit=None, regex=False, method=_NoDefault.no_default)

While I understand the API method, I do not understand what does that * refer to in the arguments string. My guess would be that it is something related to *args. Perhaps *-like notation can be used after the second argument, but I wanted some clarity on this

CodePudding user response:

This is to make all arguments after it keyword only. The * catches the third and more unnamed parameters.

Thus in this method, only to_replace and value can be passed directly:

df.replace('a', 'b')

had been transitioning from classical ordered parameter to keyword-only for many methods lately. This improves on usability/readability for many cases. For instance pivot/pivot_table have so far similar parameters but in a different order, which makes it difficult to remember. In the future, enforcing keyword-only will make it unambiguous.

  • Related