【发布时间】:2023-02-03 10:33:02
【问题描述】:
我有一个财经下载工作正常,但我希望在写入磁盘时日期列为 YYYY/MM/DD 格式。
Date 列是索引,因此我首先删除索引。然后我尝试使用 Pandas 的“to_datetime”和“.str.replace”来获取要在 YYYY/MM/DD 中格式化的列数据。
这是代码:
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()
我怎样才能解决这个问题?
【问题讨论】:
标签: python pandas dataframe yfinance