簡體   English   中英

Quandl 時間序列與 Pandas 的相關性

[英]Correlation of Quandl Time Series with Pandas

我想從 Quandl 導入兩個時間序列並想找到它們之間的相關性。 我發現了熊貓並嘗試使用 corr 函數,但是我總是得到錯誤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(). 我真的不知道這段代碼有什么問題,我打印了數組,它們看起來很好。

這是我的代碼:

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))

我遇到了同樣的錯誤。 事實證明,問題在於我如何傳遞 API 密鑰。 具體來說,為了避免在我使用的代碼中公開明文 API 密鑰

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

讀取我的 API 密鑰,然后將其傳遞給 Quandl:

Quandl.ApiConfig.api_key = api_key

然后我得到了和你一樣的錯誤:DataFrame 的真值不明確。

當我深入研究時,我意識到 pd.read_csv 是問題所在: type(api_key)給出了一個對象,而不是一個字符串。 因此,我切換到以下設置 API 密鑰:

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

然后type(api_key)返回一個字符串,並且您上面的代碼完全相同,然后沒有錯誤。 希望這會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM