'Why wont this work? - def (function) not being called from main()

I need to be able to use classes, but trying to just get my simple code to work

import pandas as pd, numpy as np
 
class OutOfCountry7ViewModel():

    def pandas_conversion(self):
        #from csv import readers
        deImport = pd.read_csv("ooc-exceptions.csv")
        d1 = pd.read_csv("CS_Out_Of_Country.csv", encoding='windows-1252', parse_dates=True)
        d2 = pd.read_csv("sccm-devices.csv", encoding='windows-1252')
        d3 = pd.read_csv("CTLDAPRawData.csv", encoding='windows-1252')

        #pandas join magic
        lj_df1 = pd.merge(d1, d2, left_on="ComputerName", right_on="Name", how="left")
        lj_df2 = pd.merge(d2, d3, left_on="PrimaryUser", right_on="Employee Number", how="left")
        #lj_df = plj_df1d.join(lj_df2, lsuffix=)

        df = (lj_df1)
        #print(df)
        df.to_csv('CS_Out_of_country_tabl.csv', index=False,header=df.columns, encoding='utf-8')
        csv = 'CS_Out_of_country_tabl.csv'
        return csv

    def main():
        pandas_conversion(self)

    if __name__ == '__main__':
        main()

i keep getting an error, NameError: name 'pandas_conversion' is not defined



Solution 1:[1]

Are you trying to do something like this? -

import pandas as pd, numpy as np
 

class OutOfCountry7ViewModel():

    def pandas_conversion(self,csv):
        ...

    def main(self):
        self.pandas_conversion(csv)


if __name__ == '__main__':
    some_object = OutOfCountry7ViewModel()
    some_object.main()

Solution 2:[2]

This should work:

a = OutOfCountry7ViewModel()
a.pandas_conversion()

Hope this helped!

Solution 3:[3]

Try to remember the semantics and indentation of python.

  • Unused import numpy
  • Class/Parent Class has no (), Line 3
class OutOfCountry7ViewModel(): #wrong

class OutOfCountry7ViewModel: #right
  • There is no need of ()
df = (lj_df1)
#if you using some func then you miss that func name
  • If you're defining a method in the class you've to add instance self
def main(self):
        pandas_conversion(self) #wrong calling func with parameter

I think your code is wrong because in PyCharm, it says def pandas_conversion(self): may be static. So, your code is incomplete, there is something missing that we can't find.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kaish kugashia
Solution 2 Jack
Solution 3 QuantumX