繁体   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