2014-03-31 46 views
0

我想我已經聲明df爲pandas.DataFrame()UnboundLocalError:分配前引用的局部變量'df'

爲什麼代碼提高UnboundLocalError

import pandas as pd 
import statsmodels.api as sm 
import numpy as np 
from math import log 

def half_life(x): 
    df = pd.DataFrame() 
    df['Close'] = x 
    df['ylag'] = df['Close'].shift(1) 
    df['deltaY'] = df['Close'] - df['ylag'] 
    df = df[1:] 
    A = np.vstack([df['ylag'], np.ones(len(df['ylag']))]).T 
    results = sm.OLS(df['deltaY'], A).fit() 
    halflife = -log(2)/results.params[0] 
    return halflife 

請幫忙!

+0

請顯示完整的回溯。 – geoffspear

+0

請你能發佈完整的錯誤,包括錯誤的號碼行。在這個等待中,我可以看到錯誤的確切位置,以便進行調試。 –

回答

0

DataFrame

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object. Like Series, DataFrame accepts many different kinds of input:

Dict of 1D ndarrays, lists, dicts, or Series
2-D numpy.ndarray
Structured or record ndarray
A Series Another DataFrame

Along with the data, you can optionally pass index (row labels) and columns (column labels) arguments. If you pass an index and/or columns, you are guaranteeing the index and/or columns of the resulting DataFrame. Thus, a dict of Series plus a specific index will discard all data not matching up to the passed index.

If axis labels are not passed, they will be constructed from the input data based on common sense rules.

請注意,您正試圖調用數據幀不帶任何參數。根據手冊,您必須擁有一個數據結構並使用特定類型的數據結構參數調用DataFrame以獲取特定類型的結果。看看我指向的手冊中的示例,以瞭解如何設置pd.DataFrame(d)並打印df.type(),df以查看實際擁有的內容。

相關問題