[英]how to format date format in pandas
I use python and pandas to get yahoo stock market date and save to a local csv file.我使用 python 和 pandas 获取雅虎股票市场日期并保存到本地 csv 文件。
DailyPrice = pd.read_csv(file, index_col=0)
print(DailyPrice.tail())
Date
2020-05-27 86.480003 84.370003 86.300003 86.180000 1917600.0
2020-05-28 87.849998 86.059998 86.870003 86.690002 1908700.0
then I update the date.然后我更新日期。
History = pdr.DataReader(ticker, start=Before_yesterday, end=Today, data_source='yahoo')
print(History.tail())
Date
2020-06-29 87.360001 86.110001 86.559998 87.290001 1302500.0 87.290001
2020-06-30 88.379997 87.235001 87.330002 88.269997 236377.0 88.269997
then I append date然后我 append 日期
DailyPrice = DailyPrice.append(History)
print(DailyPrice.tail())
Date
2020-05-27 86.480003 84.370003 86.300003 86.180000 1917600.0
2020-05-28 87.849998 86.059998 86.870003 86.690002 1908700.0
2020-06-29 00:00:00 87.360001 86.110001 86.559998 87.290001 1302500.0
2020-06-30 00:00:00 88.379997 87.235001 87.330002 88.269997 236377.0
so, my question is: how coul I format the column Date just like %Y-%m-%d, without time hh:mm:ss?所以,我的问题是:我如何格式化列 Date 就像 %Y-%m-%d 一样,没有时间 hh:mm:ss?
Looks like you have a DatetimeIndex , you could try to use strftime看起来你有一个DatetimeIndex ,你可以尝试使用strftime
DailyPrice.index = DailyPrice.index.strftime('%Y-%m-%d')
I think I find the problem.我想我找到了问题所在。
Date is not a name of column.日期不是列的名称。
DailyPrice.columns.values.tolist()
['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close']
so, the first column, Date, what's it?那么,第一列,日期,它是什么?
Date is the index.日期是索引。
at last, issue is resolved.终于,问题解决了。
DailyPrice.index = pd.to_datetime(DailyPrice.index, format = '%Y-%m-%d')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.