[Python] Convert date format from a 'yfinance' download

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página