Search code examples
pythondatabasedeep-learningdata-scienceproject

Nifty50 data science project in python Error occuring KeyError: 'Date'


I am working on Nifty50 dataset as my Data science project but this error occurs when I'm trying to implement datetime. Please help me?

This is my code to change Date which is an object into datetime:

nfty50_data['Date'] = pd.to_datetime(nifty50_data['Date'])

but that error occurs:

KeyError: 'Date'


Solution

  • too long to comment .... all of the field names have a trailing space (excepting the last)'

    #output from nifty50.columns:
    Index(['Date ', 'Open ', 'High ', 'Low ', 'Close ', 'Shares  Traded ','Turnover (₹ Cr)'],dtype='object')
    
    # so using either 
    # (the first throws a warning about format but works fine)
    
    nifty['Date '] = pd.to_datetime(nifty['Date ']) 
    
    #or 
    
    nifty['Date '] = pd.to_datetime(nifty['Date '], format='%d-%b-%Y')
    
    

    hope this helps.