Python AttributeError:' str'对象没有属性' DataFrame'

时间:2015-12-17 16:11:12

标签: python pandas attributes

以下代码片段工作正常,直到我添加了几行引用日期但不在其上方追加或更改的代码行。简单的设置

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]

2 个答案:

答案 0 :(得分:8)

错误意味着它的内容:

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

答案 1 :(得分:3)

您重新分配pd

import pandas as pd

pd = PDays[j]