pythonpandasdataframeyfinance

Convert date format from a 'yfinance' download


I have a yfinance download that is working fine, but I want the Date column to be in YYYY/MM/DD format when I write to disk.

The Date column is the Index, so I first remove the index. Then I have tried using Pandas' "to_datetime" and also ".str.replace" to get the column data to be formatted in YYYY/MM/DD.

Here is the code:

import pandas
import yfinance as yf

StartDate_T = '2021-12-20'
EndDate_T = '2022-05-14'

df = yf.download('CSCO', start=StartDate_T, end=EndDate_T, rounding=True)
df.sort_values(by=['Date'], inplace=True, ascending=False)


df.reset_index(inplace=True)  # Make it no longer an Index

df['Date'] = pandas.to_datetime(df['Date'], format="%Y/%m/%d")   # Tried this, but it fails

#df['Date'] = df['Date'].str.replace('-', '/')   # Tried this also - but error re str

file1 = open('test.txt', 'w')
df.to_csv(file1, index=True)
file1.close()

How can I fix this?


Solution

  • Change the format of the date after resetting the index:

    df.reset_index(inplace=True)
    
    df['Date'] = df['Date'].dt.strftime('%Y/%m/%d')
    

    As noted in Convert datetime to another format without changing dtype, you can not change the format and keep the datetime format, due to how datetime stores the dates internally. So I would use the line above before writing to the file (which changes the column to string format) and convert it back to datetime afterwards, to have the datetime properties.

    df['Date'] = pd.to_datetime(df['Date'])