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

时间:2021-10-14 07:59:27

The following code snippet worked fine until I added a couple of lines of code that referenced date but does not append or change it above it. with the simple case of setting

下面的代码片段工作得很好,直到我添加了几行代码,其中引用了日期,但没有在上面添加或更改它。设置的简单情况

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

the code

的代码

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

works fine. which is why this is confusing because now date is a list of 250000 values which has been working no problem until I added a few lines of code above and now this line returns

工作很好。这就是为什么这令人困惑的原因,因为date是一个包含25万个值的列表,在我在上面添加了几行代码之后,现在这行返回

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

which I cant seem to replicate in the simple case no matter what I do.

无论我做什么,在简单的情况下我似乎都无法复制。

EDIT

the few lines of code

几行代码

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 个解决方案

#1


8  

The error means exactly what it says:

这个错误的意思是:

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

The line it's complaining about:

它抱怨的是:

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

在我在上面添加了几行代码之前,一直没有问题吗

Evidently, somewhere in the "few lines of code above", you caused pd to be a string. And sure enough, when we look at those few lines of code, we find:

显然,在“上面几行代码”中,您导致pd是一个字符串。当然,当我们看这几行代码时,我们发现:

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

#2


2  

You are reassign pd

你是重新分配pd

import pandas as pd

to

pd = PDays[j]

#1


8  

The error means exactly what it says:

这个错误的意思是:

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

The line it's complaining about:

它抱怨的是:

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

在我在上面添加了几行代码之前,一直没有问题吗

Evidently, somewhere in the "few lines of code above", you caused pd to be a string. And sure enough, when we look at those few lines of code, we find:

显然,在“上面几行代码”中,您导致pd是一个字符串。当然,当我们看这几行代码时,我们发现:

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

#2


2  

You are reassign pd

你是重新分配pd

import pandas as pd

to

pd = PDays[j]