Ranny
Ranny

Reputation: 315

Calling pandas from within class

My code goes like this:

import pandas as pd

class some_class():

    def __init__(self,date):   
        self.start = pd.to_datetime(date)
        print self.start


day = '2017-07-11'
some_class(day)

everything works fine. But when I saved >some_class< as 'mod.py' and tried:

import pandas as pd
from mod import *

day = '2017-07-11'
some_class(day)

then I get error: global name 'pd' is not defined. Why is that? And how can I fix it? (I tried different possible solutions and none seems to be working.)

Upvotes: 1

Views: 1435

Answers (1)

Ranny
Ranny

Reputation: 315

Well, no answer so far. I needed it badly (or close to badly) and used this workaround: just imported pandas to my mod.py

class some_class():

def __init__(self, date):
    import pandas as pd
    self.start = pd.to_datetime(date)
    print self.start

Not ideal but works.

So I suspect there exists two different namespaces. If so, this solution unnecessary load pandas to mod.py's namespace. I will post more when I look more into this problem .

Upvotes: 1

Related Questions