我正在处理带有日期列的熊猫数据框。我已使用pandas pd.to_datetime将此列的dtype从object转换为datetime:
输入:
0 30-11-2019
1 31-12-2019
2 31-12-2019
3 31-12-2019
4 31-12-2019
5 21-01-2020
6 27-01-2020
7 01-02-2020
8 01-02-2020
9 03-02-2020
10 15-02-2020
11 12-03-2020
12 13-03-2020
13 31-03-2020
14 31-03-2020
15 04-04-2020
16 04-04-2020
17 04-04-2020
ta['transaction_date'] = pd.to_datetime(ta['transaction_date'])
输出:
0 2019-11-30
1 2019-12-31
2 2019-12-31
3 2019-12-31
4 2019-12-31
5 2020-01-21
6 2020-01-27
7 2020-01-02
8 2020-01-02
9 2020-03-02
10 2020-02-15
11 2020-12-03
12 2020-03-13
13 2020-03-31
14 2020-03-31
15 2020-04-04
16 2020-04-04
17 2020-04-04
如您所见,将其转换为datetime后的第11个输出错误的月份被替换为day,这影响了我的进一步分析。我该如何解决这个问题。
答案 0 :(得分:3)
使用dayfirst=True
参数或指定格式,因为默认情况下,熊猫会首先匹配月份,
a['transaction_date'] = pd.to_datetime(ta['transaction_date'], dayfirst=True)
或者:
a['transaction_date'] = pd.to_datetime(ta['transaction_date'], format='%d-%m-%Y')
答案 1 :(得分:1)