Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docarray/array/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,16 @@ def __getitem__(
and len(index) == 2
and isinstance(index[0], (slice, Sequence))
):
# edge case where ids are passed as element selector
if isinstance(index[0], str) and all(
i in self._id2offset for i in index
):
return DocumentArray(self._data[self._id2offset[t]] for t in index)

_docs = self[index[0]]
_attrs = index[1]
if isinstance(_attrs, str):
_attrs = (index[1],)

return _docs._get_attributes(*_attrs)
elif isinstance(index[0], bool):
return DocumentArray(itertools.compress(self._data, index))
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/array/test_advance_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,18 @@ def test_single_boolean_and_padding():

assert len(da[True, False]) == 1
assert len(da[False, False]) == 0


def test_ids():
from docarray import DocumentArray

da = DocumentArray([Document(id='1'), Document(id='2'), Document(id='3')])

# da[id, attribute]
assert len(da['1', 'id']) == 1
# da[Sequence[id]]
assert len(da['1', '2']) == 2
assert len(da['1', '2', '3']) == 3
# da[Sequence[id], attribute]
assert len(da[['1', '2'], 'id']) == 2
assert len(da[['1', '2', '3'], 'id']) == 3