Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass a Pandas DataFrame to Julia in 0.9? #181

Closed
mind6 opened this issue Jun 9, 2022 · 1 comment
Closed

How to pass a Pandas DataFrame to Julia in 0.9? #181

mind6 opened this issue Jun 9, 2022 · 1 comment

Comments

@mind6
Copy link

mind6 commented Jun 9, 2022

I have a pyconvert_add_rule in Julia that accepts a Py object wrapping pandas DataFrame, reads its columns, wraps PyArray around each column, add it to Julia DataFrame, all without copying. So I'm able to fetch a pandas DataFrame from inside Julia.

But I'm not able to activate this chain trying to pass from Python to Julia. It converts the pandas DataFrame to a PyObject instead of PyPandasDataFrame. I overrode convert rule to accept PyObject, but it seems to have trouble accessing its columns. I get the following RuntimeError trying to convert it:

In [9]: jl.convert(DataFrames.DataFrame, bm1)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 jl.convert(DataFrames.DataFrame, bm1)

RuntimeError: <PyCall.jlwrap (in a Julia function called from Python)
JULIA: UndefRefError: access to undefined reference
Stacktrace:
  [1] PyType_IsSubtype
    @ C:\Users\kirby\.julia\packages\PythonCall\4eoCM\src\cpython\pointers.jl:297 [inlined]
  [2] PyJuliaValue_New(t::Ptr{PythonCall.C.PyObject}, v::Any)
    @ PythonCall.C C:\Users\kirby\.julia\packages\PythonCall\4eoCM\src\cpython\jlwrap.jl:314
  [3] pyjl
    @ C:\Users\kirby\.julia\packages\PythonCall\4eoCM\src\jlwrap\base.jl:7 [inlined]
  [4] pyjl
    @ C:\Users\kirby\.julia\packages\PythonCall\4eoCM\src\jlwrap\any.jl:321 [inlined]
  [5] Py
    @ C:\Users\kirby\.julia\packages\PythonCall\4eoCM\src\Py.jl:157 [inlined]
  [6] macro expansion
    @ C:\Users\kirby\.julia\packages\PythonCall\4eoCM\src\Py.jl:136 [inlined]
  [7] pyconvert(#unused#::Type{DataFrames.DataFrame}, x::PyObject)
    @ PythonCall C:\Users\kirby\.julia\packages\PythonCall\4eoCM\src\convert.jl:370
  [8] convert(#unused#::Type{DataFrames.DataFrame}, x::PyObject)
    @ Conversions C:\Users\kirby\MyDrive\Documents\thirdwave\julia\Conversions.jl:27
  [9] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\essentials.jl:716
 [10] invokelatest(::Any, ::Any, ::Vararg{Any})
    @ Base .\essentials.jl:714
 [11] _pyjlwrap_call(f::Function, args_::Ptr{PyCall.PyObject_struct}, kw_::Ptr{PyCall.PyObject_struct})
    @ PyCall C:\Users\kirby\.julia\packages\PyCall\7a7w0\src\callback.jl:28
 [12] pyjlwrap_call(self_::Ptr{PyCall.PyObject_struct}, args_::Ptr{PyCall.PyObject_struct}, kw_::Ptr{PyCall.PyObject_struct})
    @ PyCall C:\Users\kirby\.julia\packages\PyCall\7a7w0\src\callback.jl:44>
@mind6
Copy link
Author

mind6 commented Jun 10, 2022

This was caused by confusing from julia import Main as jl with from juliacall import Main as jl. If you're using JuliaCall make sure you're using the second form. The first one is for the older pyjulia which hasn't had updates for two years.

@mind6 mind6 closed this as completed Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant