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

Raises an exception on mixed Awkward/NumPy slices. #1598

Merged
merged 2 commits into from
Aug 15, 2022
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
21 changes: 15 additions & 6 deletions src/awkward/_v2/_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ def headtail(oldtail):
def getitem_broadcast(items):
lookup = []
broadcastable = []
awkward_items = 0
for item in items:
if isinstance(item, ak._v2.contents.Content):
awkward_items += 1
if (
isinstance(
item,
(
slice,
list,
list, # of strings
ak._v2.contents.ListOffsetArray,
ak._v2.contents.IndexedOptionArray,
),
Expand All @@ -35,9 +38,17 @@ def getitem_broadcast(items):
):
lookup.append(None)
else:
# this includes integers (which broadcast to arrays)
lookup.append(len(broadcastable))
broadcastable.append(item)

if awkward_items > 1 or (awkward_items == 1 and len(broadcastable) != 0):
raise ak._v2._util.error(
TypeError(
"cannot mix Awkward slicing (using an array with missing or variable-length lists in the slice) with NumPy advanced slicing (using more than one broadcastable array or integer in the slice), though you can perform multiple slices"
)
)

nplike = ak.nplike.of(*broadcastable)

broadcasted = nplike.broadcast_arrays(*broadcastable)
Expand Down Expand Up @@ -354,7 +365,7 @@ def prepare_tuple_bool_to_int(item):
if item.content.content.data.shape[0] > 0:
expanded = item.content.content.data[safeindex]
else:
expanded = item.content.content.data.nplike.ones(
expanded = item.content.content.nplike.ones(
safeindex.shape[0], np.bool_
)

Expand Down Expand Up @@ -423,9 +434,7 @@ def prepare_tuple_bool_to_int(item):
if item.content.data.shape[0] > 0:
expanded = item.content.data[safeindex]
else:
expanded = item.content.data.nplike.ones(
safeindex.shape[0], np.bool_
)
expanded = item.content.nplike.ones(safeindex.shape[0], np.bool_)

# nextcontent does not include missing values
expanded[isnegative] = False
Expand All @@ -448,7 +457,7 @@ def prepare_tuple_bool_to_int(item):
)

return ak._v2.contents.IndexedOptionArray(
ak._v2.index.Index(outindex, item.nplike),
ak._v2.index.Index(outindex, nplike=item.nplike),
ak._v2.contents.NumpyArray(nextcontent, nplike=item.nplike),
)

Expand Down
4 changes: 3 additions & 1 deletion src/awkward/_v2/contents/unionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ def project(self, index):
index,
)
)
nextcarry = ak._v2.index.Index64(tmpcarry.data[: lenout[0]], self._nplike)
nextcarry = ak._v2.index.Index64(
tmpcarry.data[: lenout[0]], nplike=self._nplike
)
return self._contents[index]._carry(nextcarry, False)

@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions src/awkward/_v2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(self, data, metadata=None, nplike=None):
if nplike is None:
nplike = ak.nplike.of(data)
self._nplike = nplike
if metadata is not None and not isinstance(metadata, dict):
raise ak._v2._util.error(TypeError("Index metadata must be None or a dict"))
self._metadata = metadata
self._data = self._nplike.index_nplike.asarray(
data, dtype=self._expected_dtype, order="C"
Expand Down
13 changes: 13 additions & 0 deletions tests/v2/test_1305-mixed-awkward-numpy-slicing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import pytest # noqa: F401
import numpy as np # noqa: F401
import awkward as ak # noqa: F401


def test():
array = ak._v2.Array([[3.14]])
first_slice = ak._v2.Array([True, None])[:1] # type is 1 * ?bool
second_slice = 0
with pytest.raises(TypeError):
array[first_slice, second_slice]