The problem is that when I pass “a” to Pycall, it results in Python list rather than ndarray. I am puzzled how to convert matrix a into regular Array{Float64},2}?
To answer (parts of?) your question, a Transposed matrix can be converted to a regular Matrix by calling the Matrix constructor (or, alternatively, by using copy)
Assuming that your matrix doesn’t contain any missings, to get rid of the Union{Missing, Float64} you can call Matrix{Float64}(at) instead of Matrix(at)
Thanks for the profound answer! For my case, the more important thing seems to be the union type Union{Missing, Float64}, which could be converted as you said. Another option would be to convert already the dataframe:
df = dropmissing(df, disallowmissing=true)
The drawback is that missings are silently deleted without warning.
Well I wouldn’t necessarily call this “silent deletion without warning” - the function is literally called DROPmissing, so it drops rows with missing values.
I think the disallowmissing! function is what you are looking for. See the DataFrames documentation for more information on handling missing data. I wrote a function below that will safely convert your data frame to concrete element types with disallowmissing!, plus some extra error information. Also check out the coalesce function if you want to replace your missing data with another value (maybe NaN or 0) before sending to Python.
using DataFrames
function makeconcrete!(df)
missingrows = .!completecases(df)
if sum(missingrows) > 0
dfm = hcat(DataFrame(row = 1:nrow(df)), df)
println(dfm[missingrows,:])
throw(ErrorException(
"Cannot safely convert DataFrame to concrete type."*
" Missing values detected in the rows above."))
else
disallowmissing!(df)
end
return df
end
df1 = DataFrame(x = [11, 12, 13, 14], y = [21, 22, 23, 24])
df2 = DataFrame(x = [11, missing, 13, 14], y = [21, 22, 23, missing])
allowmissing!(df1)
julia> makeconcrete!(df1)
4×2 DataFrame
Row │ x y
│ Int64 Int64
─────┼──────────────
1 │ 11 21
2 │ 12 22
3 │ 13 23
4 │ 14 24
julia> makeconcrete!(df2)
2×3 DataFrame
Row │ row x y
│ Int64 Int64? Int64?
─────┼─────────────────────────
1 │ 2 missing 22
2 │ 4 14 missing
ERROR: Cannot safely convert DataFrame to concrete type. Missing values detected in the rows above.