Search code examples
pythonexcelpandaspandas-datareader

Downloading stock data via Python to excel (missing date column)


I want to download stock data into single excel file but while doing this I have an issue that I struggle to get date column for my stock database.

Goal - Picture example

Can someone help me to find why am I getting such results? And how can I fix this?

import pandas as pd
import pandas_datareader as pdr
import datetime as dt

file = str(input('Enter File Name - '))

download_sourrce = (r"C:\Users\vladt\OneDrive\Рабочий стол\Stock\{}.xlsx".format(file))
writer = pd.ExcelWriter(download_sourrce, engine='xlsxwriter')

should_continue = True

while should_continue: 
    
    x = str(input('Stock? - '))
    
    start = dt.datetime(2020,1,1)
    end = dt.datetime.today()
    
    df = pdr.get_data_yahoo(x,start,end)
    
    df.to_excel(writer, sheet_name=x, index=False)
    
    should_continue = input('Add new stock? [Y/N]').upper() == 'Y'
    
writer.save()

Solution

  • Try the following before saving to Excel:

    df = df.reset_index()
    

    It's common to have stock data feeds from pandas datareader (from yahoo or other companies providing data readers to pandas datareader) or other API/packages provided by other stock brokers, to have the date column set up as row index.

    For this kind date series set as row index, you have to reset index to get its contents into data column.