Pandas not being imported? 'NameError: global name' pandas 'is not defined'

I am getting a few errors here, but I think it has to do with pandas not being imported as it is greyed out. If this is a problem, how would you fix it?

C: \ Anaconda \ python.exe C: /Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py ​​Traceback (most last call last): File "C: / Users / nickd / Documents / SKLEARN-STOCKS /stock-mach.py ​​", line 38, to Key_Stats () file" C: /Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py ​​", line 12, to Key_Stats df = pandas.DataFrame (columns = ['Date', 'Unix', 'Ticker', 'DE Ratio']) NameError: global name 'pandas' is undefined

Process ended with exit code 1

import pandas as pd
import os
import time
from datetime import datetime

#location of the data files
path = 'C:\Users\nickd\Documents\SKLEARN-STOCKS'
#what specific field do you want to grab and in all files in that directory
def Key_Stats(gather="Total Debt/Equity (mrq) "):
    statspath = path+'/_KeyStats'
    stock_list = [x[0] for x in os.walk(statspath)]
    df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])

    for each_dir in stock_list[1:5]:
        each_file = os.listdir(each_dir)
        ticker = each_dir.split("//")[1]
        if len(each_file) >0:
            for file in each_file:

                date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html')
                #unix_time = time.mktime(date_stamp.timetuple())
                print(date_stamp,unix_time)
                full_file_path = each_file+'/'+file
                #print(full_file_path)
                source = open(full_file_path, 'r').read()
                #print(source)
                try:
                    value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0])
                    df = df.append({'Date':date_stamp,'Unix':unix_time,'Ticker':ticker,'DE Ratio':value,}, ignore_index = True)
                except Exception as e:
                    pass

                #print(ticker+":",value)

    save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv')
    print(save)
    df.to_csv(save)

                #time.sleep(15)

Key_Stats()

      

+3


source to share


2 answers


You imported it as

import pandas as pd

      

and call

#pandas
df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])

      

You can either change



  • import pandas as pd

    before import pandas

    or
  • df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])

    before df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])

    .

change

Mistake

associated with the absence )

save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv'))

      

+3


source


You have imported pandas as pd

. Either reference it as such, or remove it as pd

from the import.



(Plus, it never ever does except Exception... pass

. You will swallow all sorts of errors and never know what they are. If you're not going to handle them, don't get them at all.)

+1


source







All Articles