intersect()¶
- audformat.utils.intersect(objs)[source]¶
Intersect index objects.
If all index objects are conform to table specifications and at least one object is segmented, the output is a segmented index. Otherwise, requires that levels and dtypes of all objects match, see
audformat.utils.is_index_alike()
. Integer dtypes don’t have to match, but the result will always be of dtypeInt64
. When apandas.Index
is intersected with a single-levelpandas.MultiIndex
, the result is apandas.Index
.The order of the resulting index depends on the order of
objs
. If you requireaudformat.utils.intersect()
to be commutative, you have to sort its output.- Parameters
- Return type
- Returns
intersection of index objects
- Raises
ValueError – if level and dtypes of objects do not match
Examples
>>> intersect( ... [ ... pd.Index([1, 2, 3], name="idx"), ... ] ... ) Index([], dtype='Int64', name='idx') >>> intersect( ... [ ... pd.Index([1, np.nan], dtype="Int64", name="idx"), ... pd.Index([1, 2, 3], name="idx"), ... ] ... ) Index([1], dtype='Int64', name='idx') >>> intersect( ... [ ... pd.Index([0, 1], name="idx"), ... pd.MultiIndex.from_arrays([[1, 2]], names=["idx"]), ... ] ... ) Index([1], dtype='Int64', name='idx') >>> intersect( ... [ ... pd.MultiIndex.from_arrays( ... [["a", "b", "c"], [0, 1, 2]], ... names=["idx1", "idx2"], ... ), ... pd.MultiIndex.from_arrays( ... [["b", "c"], [1, 3]], ... names=["idx1", "idx2"], ... ), ... ] ... ) MultiIndex([('b', 1)], names=['idx1', 'idx2']) >>> intersect( ... [ ... filewise_index(["f1", "f2", "f3"]), ... filewise_index(["f2", "f3", "f4"]), ... ] ... ) Index(['f2', 'f3'], dtype='string', name='file') >>> intersect( ... [ ... segmented_index(["f1"], [0], [1]), ... segmented_index(["f1", "f2"], [0, 1], [1, 2]), ... ] ... ) MultiIndex([('f1', '0 days', '0 days 00:00:01')], names=['file', 'start', 'end']) >>> intersect( ... [ ... filewise_index(["f1", "f2"]), ... segmented_index(["f1", "f2"], [0, 0], [pd.NaT, 1]), ... ] ... ) MultiIndex([('f1', '0 days', NaT)], names=['file', 'start', 'end'])