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!
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With