set_index_dtypes()

audformat.utils.set_index_dtypes(index, dtypes)[source]

Set the dtypes of an index for the given level names.

Parameters
  • index (Index) – index object

  • dtypes (Union[str, Dict[str, str]]) – dictionary mapping level names to new dtype. If a single dtype is given, it will be applied to all levels

Raises
Return type

Index

Returns

index with new dtypes

Examples

>>> index1 = pd.Index(["a", "b"])
>>> index1
Index(['a', 'b'], dtype='object')
>>> index2 = set_index_dtypes(index1, "string")
>>> index2
Index(['a', 'b'], dtype='string')
>>> index3 = pd.MultiIndex.from_arrays(
...     [["a", "b"], [1, 2]],
...     names=["level1", "level2"],
... )
>>> index3.dtypes
level1    object
level2     int64
dtype: object
>>> index4 = set_index_dtypes(index3, {"level2": "float"})
>>> index4.dtypes
level1    object
level2   float64
dtype: object
>>> index5 = set_index_dtypes(index3, "string")
>>> index5.dtypes
level1    string[python]
level2    string[python]
dtype: object