从课堂内调用 pandas [英] Calling pandas from within class

查看:47
本文介绍了从课堂内调用 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样的:

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)

一切正常.但是当我保存 >some_class<作为'mod.py'并尝试:

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)

然后我收到错误:全局名称pd"未定义.这是为什么?我该如何解决?(我尝试了不同的可能解决方案,但似乎没有一个有效.)

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.)

推荐答案

好吧,到目前为止还没有答案.我非常需要(或几乎非常需要)并使用了以下解决方法:刚刚将熊猫导入到我的 mod.py

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():

class some_class():

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

不理想但有效.

所以我怀疑存在两个不同的命名空间.如果是这样,这个解决方案不必要地将熊猫加载到 mod.py 的命名空间.当我更多地研究这个问题时,我会发布更多.

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 .

这篇关于从课堂内调用 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆