7

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]
5
  • 1
    "It worked until I added a few lines of code above this"; and you show us everything except the "few lines of code" you added, which are necessarily the source of the problem. Commented Dec 17, 2015 at 16:19
  • Well Im more curiouse about how to throw the error and what it means vs fixing the error but have appended the code anyways. Commented Dec 17, 2015 at 16:23
  • 2
    pd = PDays[j] -> now pd no longer references pandas Commented Dec 17, 2015 at 16:24
  • wow did not realize I could unreference pandas like this thanks! Commented Dec 17, 2015 at 16:25
  • Somehow I never noticed that the "lines above" were added, even though it apparently happened before I submitted my answer. Clearly, the problem is at pd = PDays[j]. Commented May 22, 2024 at 22:40

2 Answers 2

13

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
Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation I was confused when reading the attribute error documentation and it is all clear now thanks everyone
4

You are reassign pd

import pandas as pd

to

pd = PDays[j]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.