進口模塊後加入一個數據幀:無法甚至以下的例子
import Quandl
import pandas as pd
from pandas.tools.plotting import df_unique
讀取API密鑰:
api_key = open('quandlapikey.txt','r').read()
目前該函數讀取一個CSV文件,以獲取代碼,但是我打算改變這對於sqllite ..
def stock_list():
#stocks = pd.read_csv('TID.csv'.rstrip())
stocks = open('TID.csv').readlines()
return stocks[0:]
獲取股票代碼quandl這工程治療。
def getStockValues():
stocks = stock_list()
main_df = pd.DataFrame()
for abbrv in stocks:
query = "LSE/" + str(abbrv).strip()
df = Quandl.get(query, authtoken=api_key,start_date='2016-04-05', end_date='2016-04-10')
df = df['Price']
df.columns = [abbrv]
print(query)
print(df)
此聲明由於某種原因導致問題,但循環時無法加入其他股票價格。
#This statement Prints as
print(df.tail(5))
#causes error
if main_df.empty:
main_df = df
else:
main_df = main_df.join(df)
# exit
print('Task done!')
getStockValues()
這是打印語句和連接錯誤的輸出。
Result:
LSE/VOD
Date
2016-04-14 226.80
2016-04-15 229.75
<ETC for all stocks>
Traceback (most recent call last):
File "H:\Workarea\DataB\SkyDriveP\OneDrive\PyProjects\Learning\21 myPprojects\stockPrices.py", line 49, in <module>
getStockValues()
File "H:\Workarea\DataB\SkyDriveP\OneDrive\PyProjects\Learning\21 myPprojects\stockPrices.py", line 43, in getStockValues
main_df = main_df.join(df)
File "H:\APPS\Python35-32\lib\site-packages\pandas\core\generic.py", line 2669, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'join'
進一步的測試表明,這個問題似乎是與大熊貓數據的範圍對象這將導致和問題:
main_df = pd.DataFrame()
for abbrv in stocks:
query = "LSE/" + str(abbrv).strip()
df = Quandl.get(query, authtoken=api_key,start_date='2016-03-05', end_date='2016-04-10')
df = df['Price']
df.columns = [abbrv]
#causes error
if main_df.empty:
main_df = df
else:
main_df = main_df.join(df)
但是這並不會導致錯誤但只返回一個數據集:
for abbrv in stocks:
main_df = pd.DataFrame()
query = "LSE/" + str(abbrv).strip()
df = Quandl.get(query, authtoken=api_key,start_date='2016-03-05', end_date='2016-04-10')
df = df['Price']
df.columns = [abbrv]
if main_df.empty:
main_df = df
else:
main_df = main_df.join(df)
請提供一個可重現的例子。此外,錯誤消息表明'main_df'是一個Series,而不是一個DataFrame – joris
上面的代碼應該像在導致錯誤一樣工作。然而,在您的評論後,我更改了代碼,這似乎是範圍問題。 –
對不起,這沒有奏效,我現在正在嘗試另一種方式來實現同樣的感謝。 –