IndexAdder¶
- class torchdata.datapipes.iter.IndexAdder(source_datapipe: IterDataPipe[Dict], index_name: str = 'index')¶
Adds an index to an existing Iterable DataPipe with (functional name:
add_index
). The row or batch within the DataPipe must have the type Dict; otherwise, a NotImplementedError will be thrown. The index of the data is set to the providedindex_name
.- Parameters:
source_datapipe – Iterable DataPipe being indexed, its row/batch must be of type Dict
index_name – Name of the key to store data index
Example
>>> from torchdata.datapipes.iter import IterableWrapper >>> dp = IterableWrapper([{'a': 1, 'b': 2}, {'c': 3, 'a': 1}]) >>> index_dp = dp.add_index("order") >>> list(index_dp) [{'a': 1, 'b': 2, 'order': 0}, {'c': 3, 'a': 1, 'order': 1}]