dgarrard
dgarrard

Reputation: 13

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?

Upvotes: 1

Views: 9847

Answers (3)

user29730146
user29730146

Reputation: 1

You can use ignore_tz=True to change the Datetime index format, ignoring the timezone.

data_forex=yf.download(tickers = forex_val,period = '60d', interval = '60m',auto_adjust=False,multi_level_index = False,**ignore_tz=True**)

Output with timezone:

close      high       low      open
Datetime
2025-02-20 **15:00:**00+00:00****  1.046901  1.047230  1.044495  1.044714
2025-02-20 **14:00:00+00:00**  1.044932  1.046134  1.044605  1.044823

Output without timezone:

open      high       low     close
Datetime
2025-02-20 **16:00:00**  1.047559  1.047669  1.046901  1.046901
2025-02-20 **15:00:00**  1.044714  1.047779  1.044495  1.047669

Upvotes: 0

Warren
Warren

Reputation: 13

You can pass a date format to the to_csv function:

df.to_csv(file1, date_format='%Y/%m/%d')

Upvotes: 0

jinx
jinx

Reputation: 313

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'])

Upvotes: 3

Related Questions