JuliaPy / JuliaPy/PythonCall.jl

`np.recarray` is not properly converted

Ouverte
#553 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
bug
Langage dominant
Julia
Étoiles
1.1k
Forks
86
Merge moyen
1 j 22 h
PR mergées (30 j)
3

Description

**Affects:** PythonCall

**Describe the bug**
It seems that `pyconvert(Any, ::)` incorrectly assumes that a `recarray` can be wrapped as a `PyArray`.

```
julia> np = pyimport("numpy");

julia> arr = np.recarray((2,2), dtype = @py([("A", "O"), ("B", "O")]))
Python:
rec.array([[(None, None), (None, None)],
[(None, None), (None, None)]],
dtype=[('A', 'O'), ('B', 'O')])

julia> pyconvert(Any, arr)
2×2 PyArray{NamedTuple{(:A, :B), Tuple{PythonCall.Wrap.UnsafePyObject, PythonCall.Wrap.UnsafePyObject}}, 2}:
(A = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830), B = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830)) (A = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830), B = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830))
(A = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830), B = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830)) (A = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830), B = UnsafePyObject(Ptr{PyObject} @0x00007ffc75ba4830))
```

A field name can not be accessed after conversion.
```
julia> arr.A
Python:
array([[None, None],
[None, None]], dtype=object)

julia> pyconvert(Any, arr).A
ERROR: type PyArray has no field A
Stacktrace:
[1] getproperty(x::PyArray{NamedTuple{(:A, :B), Tuple{PythonCall.Wrap.UnsafePyObject, PythonCall.Wrap.UnsafePyObject}}, 2, true, false, NamedTuple{(:A, :B), Tuple{PythonCall.Wrap.UnsafePyObject, PythonCall.Wrap.UnsafePyObject}}}, f::Symbol)
@ Base .\Base.jl:37
[2] top-level scope
@ REPL[11]:1

```

If indexing before field access, it works, but it does not return a usable wrapper.
```
julia> pyconvert(Any, arr)[1]
(A = PythonCall.Wrap.UnsafePyObject(Ptr{PythonCall.C.PyObject} @0x00007ffc75ba4830), B = PythonCall.Wrap.UnsafePyObject(Ptr{PythonCall.C.PyObject} @0x00007ffc75ba4830))

julia> pyconvert(Any, arr)[1].A
PythonCall.Wrap.UnsafePyObject(Ptr{PythonCall.C.PyObject} @0x00007ffc75ba4830)
```

I think the expected behavior should be `pyconvert(Any, ::)` returning something equivalent to a [`StructArray`](https://github.com/JuliaArrays/StructArrays.jl), i.e. `foo[1].A` and `foo.A[1]` are equivalent.

**Environment:**
Julia v1.9.3
PythonCall v0.9.23

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Start at the pyconvert handling for numpy recarray values and compare it with the reported conversion output. Reproduce the example with np.recarray, then make the converted result support both foo[1].A and foo.A[1] as described, using StructArray as the stated behavioral reference.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
julia, numpy, python
Domaine
data
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.