Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Converting PyObject to an Array

Tags:

julia

pyobject

In Julia, I am calling a Python module pandas_datareader to download data from the web:

using PyCall
@pyimport datetime
@pyimport pandas_datareader.data as web
gdp = web.DataReader("GDPCA","fred",start=datetime.datetime(1929,1,1))

The variable gdp is a PyObject object. As such, I cannot manipulate it (take logs for example). How do I convert it to an array? I tried convert(Array{Float64,2},gdp), but it only crashes Julia.

Thanks!

like image 909
Marek Avatar asked Oct 30 '22 11:10

Marek


1 Answers

The @pyimport macro is used to manipulate the Python objects in this case, pandas DataFrame, via the PyObject type. Given o::PyObject, o[:attribute] is equivalent to o.attribute in Python, with automatic type conversion. So the below snippet shows how to obtain a Julia array from a call to Python function,

julia> using PyCall
julia> @pyimport datetime
julia> gdp = web.DataReader("GDPCA","fred",start=datetime.datetime(1929,1,1))
julia> typeof(gdp)
PyCall.PyObject
julia> gdp[:values]
87x1 Array{Float64,2}:
1056.6
966.7
904.8
788.2
778.3
...
like image 62
Abhijith Avatar answered Dec 06 '22 02:12

Abhijith