First off this is my second day writing in Python. My manager switched us off SAS and moved us to Python and R which I actually am starting to like the flexibility. I have a few datasets that I work with on a regular basis that require a lot of cleaning before I can work with them and I would rather just call a class instead of copying and pasting code every time. The class below is the most basic one of them all I just read in the extract and pull out all records containing a certain DeviceType.

Python Version: 3.3.2

class ReturnDataFrame:
   import pandas as pd
   from pandas import DataFrame
   def FileName(self,FileName):
       self.name=name
       EFile = pd.read_csv(FileName,delimiter = '~',names=['DateTime','DeviceType','Identifier','ServiceID','Communication Module Serial Number'], header=None)
       EFile = EFile[(EFile.DeviceType == 'X023G2Z')] 
   def returnFile(self):
       return EFile   

Below is where I load in my class and instantiate it.

This is the error I am recieving when I run X.FileName('F:/Python/WinterOnly.csv')
NameError: global name 'pandas' is not defined

import ReturnDataFrame
from ReturnDataFrame import *
X =  returnDataFrame()
X.FileName('F:/Python/Week05052013.csv') 
WeeklyFile = DataFrame()
WeeklyFile = WeeklyFile.append(X.returnFile())

Here is a short explanation about variable visibility

varA = "foo" # a global variable

class Bar: # Bar is another global variable
    varB = "baz" # a class variable

    def method(self, arg): # self and arg are local variables
        varC = "qux" # a local variable
        self.name = "aname" # an instance variable
        print( varA ) # no problem. global variables are visible
        print( Bar.varB ) # varB is an attribute of class Bar
        print( self.varB ) # varB can be accessed from instances as well
        print( varC ) # No problem
        print( varB ) # NameError. varB is not visible (nor global nor local)

Conclusion: import pandas in global namespace, not in class namespace.

Are you saying like this?

Class:

class returnDataFrame:
   def FileName(self,FileName):
       self.name=name
       EFile = pandas.read_csv(FileName,delimiter = '~',names=['DateTime','DeviceType','Identifier','ServiceID','Communication Module Serial Number'], header=None)
       EFile = EFile[(EFile.DeviceType == 'X023G2Z')] 
   def returnFile(self):
       return EFile   

Execute code below:

import pandas
from pandas import DataFrame
import ReturnDataFrame
from ReturnDataFrame import *
X =  returnDataFrame()
X.FileName('F:/Python/Week05052013.csv') 
WeeklyFile = DataFrame()
WeeklyFile = WeeklyFile.append(X.returnFile())

Yes, only

  • There is no variable name in method FileName()
  • Usually, import statements are written at the beginning of the file.
  • Avoid import * as much as possible.
  • Module level code is often written in a block such as

.

if __name__ == '__main__':
    X =  returnDataFrame()
    X.FileName('F:/Python/Week05052013.csv') 
    WeeklyFile = DataFrame()
    WeeklyFile = WeeklyFile.append(X.returnFile())

Got it working. When I write the actuall class code in the terminal I was not running into any issues. However when I was importing my class I was running into issues. I added the Import pandas and from pandas import DataFrame to the top of my returnDataFrame.py and then it worked without any issues.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.