Juan C
Juan C

Reputation: 6132

Function not recognizing libraries

I have the simplest of questions, but this has never happened to me and can't find an answer. I have the following piece of code:

import datalab as dl
import pandas as pd

Where datalab is a script I wrote, and between other things, it contains this very simple function:

def fecha(vartime, df=df):
    return pd.to_datetime(df[vartiempo])

So, when I try to run the function, like this:

dl.fecha('time_obtained')

I get the following error:

NameError: name 'pd' is not defined

Even though I'm quite sure I imported pandas as pd. Is there something I'm missing when importing user functions? I'm a bit perplexed. Any help is appreciated Thanks in advance !

Upvotes: 0

Views: 572

Answers (1)

Florian Weimer
Florian Weimer

Reputation: 33717

Each Python module has its own global namespace. So if you want to use the pd name in the datalab module, you need to import it there as well, usually at the top of the script, like this:

import pandas as pd
…
def fecha(vartime, df=df):
    …

Upvotes: 3

Related Questions