Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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]
like image 625
Tyler Cowan Avatar asked Dec 17 '15 16:12

Tyler Cowan


People also ask

How do I fix AttributeError str object has no attribute?

The Python "AttributeError: 'str' object has no attribute" occurs when we try to access an attribute that doesn't exist on string objects. To solve the error, make sure the value is of the expected type before accessing the attribute.

How do I fix AttributeError module pandas has no attribute DataFrame?

Conclusion # The Python "AttributeError module 'pandas' has no attribute 'DataFrame'" occurs when we have a local file named pandas.py or misspell DataFrame . To solve the error, make sure to rename any local files named pandas.py .

How do I fix AttributeError str object has no attribute append?

The Python "AttributeError: 'str' object has no attribute 'append'" occurs when we try to call the append() method on a string (e.g. a list element at specific index). To solve the error, call the append method on the list or use the addition (+) operator if concatenating strings.

How do you fix an object that has no attribute?

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.


2 Answers

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 = PDays[j]
^       ^
|    the string that you're making it into
the thing that you're making a string
like image 169
Karl Knechtel Avatar answered Oct 17 '22 00:10

Karl Knechtel


You are reassign pd

import pandas as pd

to

pd = PDays[j]
like image 26
xiº Avatar answered Oct 17 '22 00:10

xiº