简体   繁体   中英

Correlation of Quandl Time Series with Pandas

I want to import two time series from Quandl and want to find the correlation between them. I found out about the pandas and tried with the corr function, however I always get the error ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I really don't know what is wrong with this code, I printed the arrays and they look fine.

Here is my code:

import pandas as pd
import quandl

quandl.ApiConfig.api_key = "XXX"

series1 = quandl.get("BUNDESBANK/BBK01_WT5511", start_date="2017-01-01")
series2 = quandl.get("FRED/DCOILBRENTEU", start_date="2017-01-01")

print(series1.corr(series2))

I was getting the same error. It turns out the problem was in how I was passing the API Key. Specifically, to avoid having exposed, plaintext API keys in my code I used

api_key = pd.read_csv('API Key File', header=None)

to read in my API Key, and then passed that to Quandl:

Quandl.ApiConfig.api_key = api_key

Then I got the same error as you: the truth value of a DataFrame is ambiguous.

As I dug in, I realized the pd.read_csv was the issue: type(api_key) gives an object, not a string. Thus I switched to the following to set the API Key:

with open('API Key File') as f:
    api_key = f.readline()

Then type(api_key) returns a string, and the exact same code you have above then works without the error. Hopefully this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM