2014-03-31 2 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을 호출해야합니다. pdf.DataFrame (d)가 어떻게 설정되어 있는지 확인하고 df.type(), df를 인쇄하여 실제로 가지고있는 것을 확인하기 위해 필자가 지적한 설명서의 예제를보십시오.

관련 문제