JuliaPy / PyCall.jl

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

Using a CategoricalArray in PyCall results in a <PyCall.jlwrap `obj`> object #789

Open sylvaticus opened 4 years ago

sylvaticus commented 4 years ago

Hi there. I am the maintainer of OdsIO.jl, a package to export matrices and dataframes to the OpenDocument Spreadsheet format, using ezodf, a Python module.

The problem is that with categorical arrays (now used also in DataFrames) the object is exported, e.g. as <PyCall.jlwrap 'A'> instead that just A.

I don't know if this can be resolved upstream here or in the CategoricalArray package.

For reference, the code I am using is:

if ismissing(v[r,c]) || v[r,c]==nothing
  emptyCell = ezodf.Cell()
  dcell = get(sheet,(r2-1,c2-1)) 
  dcell = emptyCell
else
   dcell = get(sheet,(r2-1,c2-1))
   dcell.set_value(v[r,c]) # --> here I (may) pass to PyCall a categorical object 
end

You can reproduce this problem with:

using OdsIO, CategoricalArrays
cat = CategoricalArray([`A`,`B`,`C`], ordered=true)
val = [1,2,3]
m = [cat val]
ods_write("test",Dict(("m",1,1)=>m))

Crosslink to the same issue in CategoricalArrays: https://github.com/JuliaData/CategoricalArrays.jl/issues/277

stevengj commented 4 years ago

What kind of Python object would you want a CategoricalArray to be converted to?

nalimilan commented 4 years ago

Pandas has a categorical type, but I guess PyCall cannot depend on Pandas. Maybe use Pandas.jl?