2015-12-17 214 views
2

下面的代碼片段工作正常,直到我添加了幾行代碼引用日期但不追加或更改它之上。與設置Python AttributeError:'str'對象沒有屬性'DataFrame'

date = ['1/1/2001','1/1/2001','1/1/2001','1/1/2001'] 

代碼的簡單情況

import pandas as pd 
ProdDate = ['1/1/2001','1/1/2001','1/1/2001','1/1/2001'] 
df = pd.DataFrame(ProdDate, columns = ['Date']) 

工作正常。這就是爲什麼這是混亂的,因爲現在的日期是,直到我說上面的幾行代碼已經工作沒有問題250000個值的列表,現在這條線返回

AttributeError: 'str' object has no attribute 'DataFrame' 

我不能似乎在複製不管我做什麼都簡單的情況。

編輯

的代碼的幾行

for i in range(0,len(UniqueAPI)): 
    for j in range(0,len(API)): 
     if UniqueAPI[i] == API[j]: 
      index = j 
      pd = PDays[j] 
      g = vG[j] 
      o = vO[j] 
      c = vC[j] 
      lhs, rhs = str(ProdDate[j]).rsplit("/", 1) 
      daycounter = 0 
      start = 365 - int(pd) 
      if clndr.isleap(int(rhs)): 
       calDays = LeapDaysInMonth 
      else: 
       calDays = DaysInMonth 
      break 
    for j in range(0,12): 
     daycounter = daycounter + DaysInMonth[j]       
     if daycounter - start >= 0: 
      m = j 
      break 
    for j in range(0,12): 
     if m == 0: 
      break 
     if j < m: 
      Liq[index+j] = 0 
      Gas[index+j] = 0 
     else: 
      if clndr.isleap(int(rhs)): 
       days = 366 
       Gas[index+j] = (g/days)*LeapDaysInMonth[j] 
       Liq[index+j] = (o/days)*LeapDaysInMonth[j] + (cndval/days)*LeapDaysInMonth[j]       
      else: 
       days = 365 
       Gas[index+j] = (g/days)*DaysInMonth[j] 
       Liq[index+j] = (o/days)*DaysInMonth[j] + (c/days)*DaysInMonth[j] 
+0

「它一直工作,直到我在這上面添加了幾行代碼」;除了您添加的「幾行代碼」之外,您向我們展示了所有內容,這些都是問題的根源。 –

+0

嗯我更curiouse關於如何拋出錯誤及其意義與修復錯誤,但反正附加了代碼。 –

+2

'pd = PDays [j]' - >現在'pd'不再引用'熊貓' – schwobaseggl

回答

7

的錯誤意味着正是它說你是在重新分配:

AttributeError: 'str' object has no attribute 'DataFrame' 
    ^  ^       ^
the kind of error |        | 
     the thing you tried to use  what was missing from it 

它在抱怨線路:

df = pd.DataFrame(date, columns = ['Date']) 
    ^ ^
    | the attribute the error said was missing 
the thing the error said was a string 

has been working no problem until I added a few lines of code above

顯然,在某處 「的上面幾行代碼」,就造成pd是一個字符串。當然,當我們看幾行代碼時,我們發現:

pd = PDays[j] 
^  ^
| the string that you're making it into 
the thing that you're making a string 
+1

很好的解釋在閱讀屬性錯誤文檔時我感到困惑,現在很清楚,謝謝大家 –

1

pd

import pandas as pd 

pd = PDays[j] 
相關問題