Python AttributeError:“str”对象没有属性“DataFrame”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34339218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:50:33  来源:igfitidea点击:

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

pythonpandasattributes

提问by Tyler Cowan

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 是一个 250000 个值的列表,它一直工作没有问题,直到我在上面添加了几行代码,现在这一行返回

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]

采纳答案by Karl Knechtel

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 pdto 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

回答by xio

You are reassign pd

你被重新分配 pd

import pandas as pd

to

pd = PDays[j]