Skip to content

Commit

Permalink
Fixed #333.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Jul 15, 2020
1 parent 216dc41 commit 48aac6a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
33 changes: 31 additions & 2 deletions src/python/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,37 @@ make_IndexOf(const py::handle& m, const std::string& name) {

.def("__repr__", &ak::IndexOf<T>::tostring)
.def("__len__", &ak::IndexOf<T>::length)
.def("__getitem__", &ak::IndexOf<T>::getitem_at)
.def("__getitem__", &ak::IndexOf<T>::getitem_range)
.def("__getitem__", [](const ak::IndexOf<T>& self, py::object& obj) {
if (py::isinstance<py::int_>(obj)) {
T out = self.getitem_at(obj.cast<int64_t>());
return py::cast(out);
}
else if (py::isinstance<py::slice>(obj)) {
py::object pystep = obj.attr("step");
if ((py::isinstance<py::int_>(pystep) && pystep.cast<int64_t>() == 1) ||
pystep.is(py::none())) {
int64_t start = ak::Slice::none();
int64_t stop = ak::Slice::none();
py::object pystart = obj.attr("start");
py::object pystop = obj.attr("stop");
if (!pystart.is(py::none())) {
start = pystart.cast<int64_t>();
}
if (!pystop.is(py::none())) {
stop = pystop.cast<int64_t>();
}
ak::IndexOf<T> out = self.getitem_range(start, stop);
return py::cast(out);
}
else {
throw std::invalid_argument("Index slices cannot contain step != 1");
}
}
else {
throw std::invalid_argument(
"Index can only be sliced by an integer or start:stop slice");
}
})

);
}
Expand Down
8 changes: 6 additions & 2 deletions tests/test_0337-numpy-format-integer-size-issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@

import awkward1

def test():
pass
def test_index_slice():
index = awkward1.layout.Index64(numpy.array(
[0, 100, 200, 300, 400, 500, 600, 700, 800, 900],
numpy.int64))
assert index[4] == 400
assert numpy.asarray(index[3:7]).tolist() == [300, 400, 500, 600]

0 comments on commit 48aac6a

Please sign in to comment.