dtoolkit.accessor.dataframe.set_unique_index#

dtoolkit.accessor.dataframe.set_unique_index(df: DataFrame, /, **kwargs) DataFrame[source]#

Set unique index via reset_index().

The index will be reset:

  • If df.index isn’t unique.

  • If df.index isn’t monotonic increasing.

Parameters:
**kwargs

See the documentation for reset_index() for complete details on the keyword arguments.

Returns:
DataFrame

With new index.

Warns:
UserWarning

If the index of the inputting is not unique.

Examples

>>> import dtoolkit
>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=[0, 0, 1])
>>> df
   a  b
0  1  4
0  2  5
1  3  6

If the index of the inputting is not unique, then it will be reset. And give a warning like below:

UserWarning: The index of 'DataFrame' is not unique.
>>> df.set_unique_index(drop=True)
   a  b
0  1  4
1  2  5
2  3  6

The index will be reset if it isn’t monotonic increasing or decreasing.

>>> df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=[3, 1, 2])
>>> df
   a  b
3  1  4
1  2  5
2  3  6
>>> df.set_unique_index(drop=True)
   a  b
0  1  4
1  2  5
2  3  6