ERROR: LoadError: Python: IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

I am getting ERROR: LoadError: Python: IndexError: only integers, slices (:), ellipsis (), numpy.newaxis (None) and integer or boolean arrays are valid indices Python stacktrace: none
I am using julia script that calls python code library. My julia code is :point_down:

cd("/home/raman/Pictures/MikiFor2to3/")
using Pkg, Interpolations, TensorOperations, DelimitedFiles
Pkg.activate(".")
Pkg.add(["CondaPkg", "PythonCall"])
using CondaPkg, PythonCall
CondaPkg.add(CondaPkg.PkgSpec.(["numpy", "scipy", "h5py", "matplotlib"]))
const sys = PythonCall.pyimport("sys")
sys.path.append(pwd())
const pyfiles = pyimport("pyfiles")
athena_read = pyimport("pyfiles.athena_read")
datafile ="./home/raman/Pictures/sane00.athinput/"
data = pyfiles.read_data("sane00.prim.01800.athdf", athinput="sane00.athinput")

theta = PyArray(data.metric.theta[1,:])
print(size(theta))

What should i do overcome this indexing error due to theta?

First determine which line of the code produces this error.

1 Like

Posting the Julia code when the error is in your Python code is not that helpful. Best way to find the line causing the error is probably to run the python code in pure python (not called from Julia) and make sure that runs fine

Error is caused by last line defining variable theta.

julia> cd("/home/raman/Pictures/MikiFor2to3/")

julia> using Pkg, Interpolations, TensorOperations, DelimitedFiles

julia> Pkg.activate(".")
  Activating project at `~/Pictures/MikiFor2to3`

julia> Pkg.add(["CondaPkg", "PythonCall"])
   Resolving package versions...
  No Changes to `~/Pictures/MikiFor2to3/Project.toml`
  No Changes to `~/Pictures/MikiFor2to3/Manifest.toml`

julia> using CondaPkg, PythonCall

julia> CondaPkg.add(CondaPkg.PkgSpec.(["numpy", "scipy", "h5py", "matplotlib"]))

    CondaPkg Found dependencies: /home/raman/Pictures/MikiFor2to3/CondaPkg.toml
    CondaPkg Found dependencies: /home/raman/.julia/packages/PythonCall/S5MOg/CondaPkg.toml
    CondaPkg Found dependencies: /home/raman/.julia/environments/v1.10/CondaPkg.toml
    CondaPkg Dependencies already up to date

julia> const sys = PythonCall.pyimport("sys")
Python: <module 'sys' (built-in)>

julia> sys.path.append(pwd())
Python: None

julia> const pyfiles = pyimport("pyfiles")
Python: <module 'pyfiles' from '/home/raman/Pictures/MikiFor2to3/pyfiles/__init__.py'>

julia> athena_read = pyimport("pyfiles.athena_read")
Python: <module 'pyfiles.athena_read' from '/home/raman/Pictures/MikiFor2to3/pyfiles/athena_read.py'>

julia> datafile ="./home/raman/Pictures/sane00.athinput/"
"./home/raman/Pictures/sane00.athinput/"

julia> data = pyfiles.read_data("sane00.prim.01800.athdf", athinput="sane00.athinput")
Python: <pyfiles.read_data.read_data object at 0x7f8ac9a6f740>

julia> theta = PyArray(data.metric.theta[1,:])
ERROR: Python: IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Python stacktrace: none
Stacktrace:
 [1] pythrow()
   @ PythonCall.Core ~/.julia/packages/PythonCall/S5MOg/src/Core/err.jl:92
 [2] errcheck
   @ ~/.julia/packages/PythonCall/S5MOg/src/Core/err.jl:10 [inlined]
 [3] pygetitem(x::Py, k::Tuple{Int64, Colon})
   @ PythonCall.Core ~/.julia/packages/PythonCall/S5MOg/src/Core/builtins.jl:171
 [4] getindex(::Py, ::Int64, ::Function)
   @ PythonCall.Core ~/.julia/packages/PythonCall/S5MOg/src/Core/Py.jl:293
 [5] top-level scope
   @ REPL[13]:1

The Julia : is not converted to anything very useful currently when passed to Python. You can get a Python : with pyslice(nothing).

2 Likes