pythondataframe

Pandas Dataframe set Categories - The `inplace` parameter in pandas.Categorical.set_categories is deprecated


I have following statement in my code:

mcap_summary['cap'].cat.set_categories(['Large','Mid','Small','None'],inplace=True)

Which now generates a warning as: D:\Python\Python39\lib\site-packages\pandas\core\arrays\categorical.py:2630: FutureWarning: The inplace parameter in pandas.Categorical.set_categories is deprecated and will be removed in a future version. Removing unused categories will always return a new Categorical object. res = method(*args, **kwargs)

How am I suppose to write to avoid this warning and future errors?

Thanks in advance


Solution

  • pandas v1.3.0+ deprecated the inplace option.

    Deprecated the inplace parameter of Categorical.remove_categories(), Categorical.add_categories(), Categorical.reorder_categories(), Categorical.rename_categories(), Categorical.set_categories() and will be removed in a future version (GH37643)

    ā€” https://pandas.pydata.org/pandas-docs/version/1.3.0/whatsnew/v1.3.0.html#other-deprecations

    so you can code like below:

    mcap_summary['cap'] = mcap_summary['cap'].cat.set_categories(['Large', 'Mid', 'Small', 'None'])