我想在Pandas中创建两个具有相同日期格式的不同数据帧,原始CSV文件有两种不同的日期格式。我需要在to_datetime函数中使用什么来以相同的日期格式获取这些数据帧?
#DF1 (year, month day, time)
Date Event
2017, February 03, 09:30 (United States) Unemployment Rate
2017, February 03, 09:30 (United States) Nonfarm Payrolls
2017, February 03, 11:00 (United States) ISM Non-Manufacturing PMI
#DF2 (day.month.year time)
Local time Open High Low Close
03.02.2017 08:30:00.000 1.07363 1.0775 1.07134 1.0762
03.02.2017 08:35:00.000 1.07626 1.07669 1.07324 1.07375
03.02.2017 08:40:00.000 1.07372 1.07585 1.0736 1.0758
03.02.2017 08:45:00.000 1.0758 1.07626 1.07481 1.07487
03.02.2017 08:50:00.000 1.07488 1.07519 1.0745 1.07515
03.02.2017 08:55:00.000 1.07518 1.07573 1.07478 1.07562
03.02.2017 09:00:00.000 1.07561 1.07729 1.07559 1.07694
03.02.2017 09:05:00.000 1.07693 1.07784 1.07687 1.07757
03.02.2017 09:10:00.000 1.07756 1.07825 1.07669 1.07688
答案 0 :(得分:0)
这将为您提供相同的日期时间格式,但您没有准确指定它们所需的格式,因此请告诉我们,我可以更新此答案。
import pandas as pd
df = pd.DataFrame({'dates':['2017, February 03, 09:30','2017, February 03, 09:45']})
df2 = pd.DataFrame({'dates':['03.02.2017 08:30:00.00', '03.02.2017 08:35:00.000']})
df['dates'] = pd.to_datetime(df['dates'])
df2['dates'] = pd.to_datetime(df2['dates'])
答案 1 :(得分:0)
我认为首先需要从第二个数据帧中删除最后三个0。然后在数据框中添加您想要的任何一个日期的日期格式。请参考以下代码:
df = pd.DataFrame({'dates':['2017, February 03, 09:30','2017, February 03, 09:45']})
df2 = pd.DataFrame({'dates':['03.02.2017 08:30:00.000', '03.02.2017 08:35:00.000']})
df2['dates'] = df2['dates'].astype(str).str[:-4].astype(str)
df["dates"] = pd.to_datetime(df["dates"])
df2["dates"] = pd.to_datetime(df2["dates"],format='%d.%m.%Y %H:%M:%S')
输出:
>>>df
dates
0 2017-02-03 09:30:00
1 2017-02-03 09:45:00
>>>df2
dates
0 2017-02-03 08:30:00
1 2017-02-03 08:35:00