Skip to content

ellipses (...) as indices on numpy array: #827

Description

@Alexander-Barth

I am trying to wrap a python code from a colleague. I would like to avoid any changes to the python code that makes the python code less general. I have a problem when the code uses ellipses (...) as indices on numpy array:

Is your feature request related to a problem? Please describe.

Consider the python code (in file test_ellipsis.py) :

def test_ellipsis(X):
    print("sum", X[0,...].sum())

The code is called in julia as:

using PythonCall
pyimport("sys").path.append("/add/file/path/to/python/script/")
pyimport("test_ellipsis").test_ellipsis(randn(2,2,2))

Unfortunately, the produces the error:

ERROR: Python: TypeError: expecting 3 indices, got 2
Python stacktrace:
 [1] __getitem__
   @ ~/.julia/packages/PythonCall/83z4q/src/JlWrap/array.jl:350
 [2] test_ellipsis
   @ test_ellipsis ~/src/NEDAS-test/NEDAS_tutorials/test_ellipsis.py:2

Describe the solution you'd like

Could indexing of ellipses be supported in PythonCall.jl for numpy arrays ?

Describe alternatives you've considered

I could create a np.array from the julia side, for example:

np = pyimport("numpy")
pyimport("test_ellipsis").test_ellipsis(np.asarray(randn(2,2,2)))

Additional context

I am using julia 1.12 on Linux with PythonCall v0.9.31 and Numpy 2.5.3.

Btw, thanks for the great package!!

Activity

  1. cjdoris commented on Sep 24, 2026

    @cjdoris
    Member

    Wrapped Julia arrays intentionally only support some very basic behaviour. If you want more array functionality you should convert it to a specialised array type like a numpy array.

    Your test_ellipsis(X) function implicitly assumes that X is a numpy array, so you should pass one, like your suggested work-around.

    Alternatively the function could be modified to take more general array-like types by converting X itself to a numpy array, but you said that you cannot change the python code.

    Your specific suggestion of supporting ... wouldn't even help because it would then error on .sum() which also isn't implemented for wrapped Julia arrays.

  2. Alexander-Barth commented on Sep 28, 2026

    @Alexander-Barth
    Author

    Ok, Thank you for clarifying that. Actually, in my case, all python functions that I try to wrap essentially assume that arguments are numpy arrays. Is there a way to make the conversation automatic? Or would it be necessary to wrap the arguments manually?

  3. cjdoris commented on Sep 28, 2026

    @cjdoris
    Member

    PythonCall doesn't have any machinery for this directly.

    Easiest is prob to make a little arg-making function like

    makearg(x::AbstractArray) = pyimport("numpy").asarray(x)
    makearg(x) = Py(x)
    callfunc(f, args...) = f(map(makearg, args)...)

    then call like

    callfunc(script.test_ellipsis, randn(2, 2, 2))

    or something along these lines.

  4. Alexander-Barth commented on Sep 30, 2026

    @Alexander-Barth
    Author

    Thanks a lot for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions