Search code examples
pythonquandl

Quandl + Python: The date column not "working"


I am trying to get some data through the API from quandl but the date column doesn't seem to work the same level as the other columns. E.g. when I use the following code:

data = quandl.get("WIKI/KO", trim_start = "2000-12-12", trim_end =
"2014-12-30", authtoken=quandl.ApiConfig.api_key) 

print(data['Open'])

I end up with the below result

Date

2000-12-12 57.69

2000-12-13 57.75

2000-12-14 56.00

2000-12-15 55.00

2000-12-18 54.00

E.g. date appearing along with the 'Open' column. And when I try to directly include Date like this:

print(data[['Open','Date']]),

it says Date doesn't exist as a column. So I have two questions: (1) How do I make Date an actual column and (2) How do I select only the 'Open' column (and thus not the dates).

Thanks in advance


Solution

  • Why print(data['Open']) show dates even though Date is not a column:

    quandle.get returns a Pandas DataFrame, whose index is a DatetimeIndex. Thus, to access the dates you would use data.index instead of data['Date'].


    (1) How do I make Date an actual column

    If you wish to make the DatetimeIndex into a column, call reset_index:

    data = data.reset_index()
    print(data[['Open', 'Date']])
    

    (2) How do I select only the 'Open' column (and thus not the dates)

    To obtain a NumPy array of values without the index, use data['Open'].values. (All Pandas Series and DataFrames have Indexs (that's Pandas' raison d'être!), so the only way obtain the values without the index is to convert the Series or DataFrame to a different kind of object, like a NumPy array.)