Skip to content

Commit a832ee4

Browse files
committed
Support indexing by pyarrow.Array
1 parent 44c8795 commit a832ee4

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

apis/python/src/tiledbsoma/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
pa.SparseCSCMatrix,
2727
]
2828

29-
SOMADenseCoordinates = Union[int, slice]
29+
SOMADenseCoordinates = Union[int, slice, pa.Array]
3030
SOMADenseNdCoordinates = Tuple[SOMADenseCoordinates, ...]
3131

3232
SOMASparseCoordinates = Union[int, slice, Tuple[int, ...], List[int], pa.IntegerArray]

apis/python/src/tiledbsoma/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import numpy as np
77
import pandas as pd
8+
import pyarrow as pa
89
import scipy.sparse as sp
910

1011
T = TypeVar("T", np.ndarray, pd.Series, pd.DataFrame, sp.spmatrix)
@@ -93,7 +94,7 @@ def uri_joinpath(base: str, path: str) -> str:
9394
return urllib.parse.urlunparse(parts)
9495

9596

96-
def ids_to_list(ids: Union[slice, List[int]]) -> List[int]:
97+
def ids_to_list(ids: Union[slice, List[int], pa.Array]) -> List[int]:
9798
"""
9899
For the interface between ``SOMADataFrame::read`` et al. (Python) and ``SOMAReader`` (C++): the
99100
``ids`` argument to the former can be slice or list; the argument to
@@ -112,4 +113,6 @@ def ids_to_list(ids: Union[slice, List[int]]) -> List[int]:
112113
step = -1
113114
stop = ids.stop + step
114115
return list(range(ids.start, stop, step))
116+
if isinstance(pa.Array):
117+
return [id.as_py() for id in ids]
115118
raise Exception(f"expected ids as slice or list; got {type(ids)}")

apis/python/tests/test_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pyarrow as pa
12
import pytest
23

34
from tiledbsoma.util import ids_to_list, uri_joinpath
@@ -35,8 +36,11 @@ def test_uri_joinpath_tiledb():
3536

3637

3738
def test_ids_to_list():
39+
assert ids_to_list([]) == []
3840
assert ids_to_list([]) == []
3941
assert ids_to_list([1, 2, 3]) == [1, 2, 3]
42+
assert ids_to_list(pa.array([])) == []
43+
assert ids_to_list(pa.array([1, 2, 3])) == [1, 2, 3]
4044
assert ids_to_list(slice(None)) == []
4145
assert ids_to_list(slice(1, 5)) == [1, 2, 3, 4, 5]
4246
assert ids_to_list(slice(5, 1)) == [5, 4, 3, 2, 1]

0 commit comments

Comments
 (0)