Skip to content

Commit 79ad236

Browse files
authored
fix(array): fix edge case on single boolean index (#28)
1 parent 8c39f51 commit 79ad236

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

docarray/array/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __getitem__(
129129
def __getitem__(
130130
self, index: 'DocumentArrayIndexType'
131131
) -> Union['Document', 'DocumentArray']:
132-
if isinstance(index, (int, np.generic)):
132+
if isinstance(index, (int, np.generic)) and not isinstance(index, bool):
133133
return self._data[int(index)]
134134
elif isinstance(index, str):
135135
if index.startswith('@'):

docs/fundamentals/documentarray/access-elements.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ print(da)
120120
<DocumentArray (length=50) at 4513619088>
121121
```
122122

123+
Note that if the length of the boolean mask is smaller than the length of a DocumentArray, then the remaining part is padded to `False`.
124+
123125
(path-string)=
124126
## Index by nested structure
125127

tests/unit/array/test_advance_indexing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,15 @@ def test_advance_selector_mixed():
219219

220220
assert len(da[:, ('id', 'embedding', 'matches')]) == 3
221221
assert len(da[:, ('id', 'embedding', 'matches')][0]) == 10
222+
223+
224+
def test_single_boolean_and_padding():
225+
from docarray import DocumentArray
226+
227+
da = DocumentArray.empty(3)
228+
229+
with pytest.raises(IndexError):
230+
da[True]
231+
232+
assert len(da[True, False]) == 1
233+
assert len(da[False, False]) == 0

0 commit comments

Comments
 (0)