JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.45k stars 186 forks source link

Feature Request: Support indexing into python objects #1041

Open schlichtanders opened 1 year ago

schlichtanders commented 1 year ago

I am using pandas with PyCall.jl and would like to write something like

mysubset = df[df["colname"] == "value"]

looking into the code https://github.com/JuliaPy/PyCall.jl/blob/master/src/PyCall.jl#L349-L361 it seems that this is just not supported currently (because of some legacy where indexing was used instead of dot access)

schlichtanders commented 1 year ago

I can imagine something like

Base.getindex(o::PyObject, s) = py"$o[$s]"
Base.setindex!(o::PyObject, v, s) = py"$o[$s] = $v"
stevengj commented 1 year ago

In the meantime, you can use PythonCall.jl, which supports this.

schlichtanders commented 1 year ago

This is not completely true. PythonCall.jl has indeed difficulties with the mentioned example because of comparisons between python object (series) and julia object ("value"). you need to use the @py macro if you want this to work with PythonCall.jl to the best of my knowledge.

I rather prefer to use native julia syntax, which is indeed the main reason I am going for PyCall.jl when calling Python from Julia.

fabiensatalia commented 8 months ago

Came here to request the same, in fact I would be happy to "just" be able to select rows from a pandas data frame, e.g. df[[1,2]] to select the first two rows