Tyler Cowan
Tyler Cowan

Reputation: 850

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]

Upvotes: 7

Views: 84201

Answers (2)

Karl Knechtel
Karl Knechtel

Reputation: 61478

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

Upvotes: 12

xi&#186;
xi&#186;

Reputation: 4687

You are reassign pd

import pandas as pd

to

pd = PDays[j]

Upvotes: 4

Related Questions