Is there any way to get colons passing through correctly to Python here? I’d really prefer natural colons, rather than some ugly call like
np.random.rand(3,5)[pyslice(nothing), 1]
I had claimed that you could just use python modules from Julia with almost the same syntax as in python, but this is a big glaring exception to that claim.
Okay, but I would put that in the same category of unnaturalness and ugliness as
np.random.rand(3,5)[pyslice(nothing), 1]
The point is that it really seems like the Julia object Colon() represented by : in
julia> np.random.rand(3,5)[:, 1]
should just work — maybe by being translated to pyslice(nothing); maybe by some other means. But I’m failing to understand what pygetitem is doing, so I don’t understand why it’s not working. Is this just a bug, a feature that hasn’t been implemented, or a real limitation?
The short answer is that there is no conversion method Py(::Colon) defined for : yet.
It would probably make sense to convert it to pyslice(nothing) as you suggest.
I’ve not thought about it too hard, but was put off a bit because you can’t really emulate Python’s full slicing syntax (start:stop etc) in Julia. Firstly because in Python it is special indexing syntax whereas in Julia it is just a way to get a range. And secondly because the natural conversion for a Julia range is a Python range, which is not a valid index for a python list (which only allows indexing by int and slice). So slicing would be a bit inconsistent.
That said, some other libraries such as numpy do allow indexing by range (I think?) so we’d actually get more consistency there.
Another consideration is what happens over on the Python side in JuliaCall. If you do juliacall.Base.Colon() then currently you’ll get a wrapped : which can be used to index a Julia array from Python. If we add the above conversion rule, you’ll instead get slice(None) which cannot be used as a Julia array index. To fix this we’d also need to add a conversion rule for slice(None) to : which I guess is ok? This consideration will be irrelevant in PythonCall v1 (in the works) which will always return wrapped Julia objects from JuliaCall.