map_file_path()

audformat.utils.map_file_path(index, func)[source]

Apply callable to file path in index.

Relies on pandas.Index.map(), which can be slow. If speed is crucial, consider to change the index directly. In the following example we prefix every file with a folder and add a new extension, compare also audformat.utils.expand_file_path() and audformat.utils.replace_file_extension():

root = "/root/"
ext = ".new"
if table.is_filewise:
    table.df.index = root + table.df.index + ext
    table.df.index.name = audformat.define.IndexField.FILE
elif len(table.df.index) > 0:
    table.df.index = table.df.index.set_levels(
        root + table.df.index.levels[0] + ext,
        level=audformat.define.IndexField.FILE,
    )
Parameters
Return type

Index

Returns

index modified by func

Raises

ValueError – if index is not conform to table specifications

Examples

>>> index = filewise_index(["a/f1", "a/f2"])
>>> index
Index(['a/f1', 'a/f2'], dtype='string', name='file')
>>> map_file_path(index, lambda x: x.replace("a", "b"))
Index(['b/f1', 'b/f2'], dtype='string', name='file')