Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] Fix casting bug in IntIndexer #3525

Merged
merged 1 commit into from
Jan 7, 2025
Merged
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
8 changes: 4 additions & 4 deletions apis/python/src/tiledbsoma/_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def __init__(

# TODO: the map_locations interface does not accept chunked arrays. It would
# save a copy (reduce memory usage) if they were natively supported.
if isinstance(
data, (pa.Array, pa.ChunkedArray, pd.arrays.IntegerArray, pd.Series)
):
if isinstance(data, (pa.Array, pa.ChunkedArray)):
data = data.to_numpy()
elif isinstance(data, list):
data = np.array(data, dtype=np.int64)
elif isinstance(data, (pd.arrays.IntegerArray, pd.Series)):
data = data.to_numpy(dtype=np.int64, copy=False)

self._reindexer.map_locations(data)

Expand All @@ -95,7 +95,7 @@ def get_indexer(self, target: IndexerDataType) -> npt.NDArray[np.intp]:
return self._reindexer.get_indexer_pyarrow(target)

if isinstance(target, (pd.arrays.IntegerArray, pd.Series)):
target = target.to_numpy()
target = target.to_numpy(dtype=np.int64, copy=False)
elif isinstance(target, list):
target = np.array(target, dtype=np.int64)

Expand Down
11 changes: 8 additions & 3 deletions apis/python/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ def test_indexer(contextual: bool, keys: np.array, lookups: np.array):
num_threads = 10

def target():
indexer = IntIndexer(keys, context=context)
results = indexer.get_indexer(lookups)
all_results.append(results)
try:
indexer = IntIndexer(keys, context=context)
results = indexer.get_indexer(lookups)
all_results.append(results)
except Exception as e:
all_results.append(e)

for t in range(num_threads):
thread = threading.Thread(target=target, args=())
Expand All @@ -113,6 +116,8 @@ def target():
panda_indexer = pd.Index(keys)
panda_results = panda_indexer.get_indexer(lookups)
for i in range(num_threads):
if isinstance(all_results[i], Exception):
raise all_results[i]
np.testing.assert_equal(all_results[i].all(), panda_results.all())


Expand Down