6

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!

Marek
  • 61
  • 1
  • 4
  • Hi welcome to stack overflow, please try to format your code to make your question more readable: (http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) – terence hill Jan 20 '16 at 20:54
  • Looks like your `gdp` is a pandas `DataFrame`. In general, you can [access PyObject attributes by using the `object[:attribute]` syntax](https://github.com/stevengj/PyCall.jl/blob/master/README.md). – Chris Jan 20 '16 at 21:13
  • Formatted the code. What attribute should I be looking for to obtain the downloaded values (and convert them to an array)? – Marek Jan 20 '16 at 21:37
  • Possibly [`as_matrix`](http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.DataFrame.as_matrix.html)? I'm not sure about the DataFrame-to-array bit with pandas, thus the comment and not answer. – Chris Jan 20 '16 at 21:43
  • gdp[:as_matrix] does not work, but gdp[:values] solves it! Thanks! – Marek Jan 20 '16 at 22:00

1 Answers1

2

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
...
Abhijith
  • 1,158
  • 1
  • 13
  • 27