簡體   English   中英

Python AttributeError:'str'對象沒有屬性'DataFrame'

[英]Python AttributeError: 'str' object has no attribute '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'])

工作正常。 這就是為什么這令人困惑,因為現在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]

錯誤意味着它的內容:

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

在我上面添加幾行代碼之前,一直沒有問題

顯然,在“上面幾行代碼”的某處,你使pd成為一個字符串。 當然,當我們查看這幾行代碼時,我們會發現:

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

你重新分配pd

import pandas as pd

pd = PDays[j]

暫無
暫無

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

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