[英]pandas to_datetime formats date from same column in different formats
I am using pandas to extract two columns(out of many) from a csv file and store it in a new csv file. 我正在使用pandas从csv文件中提取两列(在很多列中)并将其存储在新的csv文件中。 The issue is, pd.to_datetime formats the date into two different formats.
问题是,pd.to_datetime将日期格式化为两种不同的格式。 It formats the date as YYYY-MM-DD and YYYY-DD-MM.
它将日期格式设置为YYYY-MM-DD和YYYY-DD-MM。 The date in the original file is in the format DD-MM-YYYY.
原始文件中的日期格式为DD-MM-YYYY。
Original Data: 原始数据:
**DATE_TIME** **约会时间**
01/1/2016 21:11:19 2016年1月1日21:11:19
01/1/2016 21:29:58 2016年1月1日21:29:58
01/1/2016 22:49:19 2016年1月1日22:49:19
01/1/2016 22:50:41 2016年1月1日22:50:41
02/1/2016 00:11:41 2016/02/1/00:11:41
02/1/2016 00:19:47 2016/02/1/00:19:47
02/1/2016 00:19:48 2016/02/1/00:19:48
02/1/2016 00:19:50 2016/02/1/00:19:50
02/1/2016 00:19:50 2016/02/1/00:19:50
The weird formatted data: 奇怪的格式化数据:
**DATE_TIME** 2016-01-01 21:11:19 ** DATE_TIME ** 2016-01-01 21:11:19
2016-01-01 21:29:58 2016-01-01 21:29:58
2016-01-01 22:49:19 2016-01-01 22:49:19
2016-01-01 22:50:41 2016-01-01 22:50:41
2016-02-01 00:11:41 2016-02-01 00:11:41
2016-02-01 00:19:47 2016-02-01 00:19:47
2016-02-01 00:19:48 2016-02-01 00:19:48
2016-02-01 00:19:50 2016-02-01 00:19:50
2016-02-01 00:19:50 2016-02-01 00:19:50
Note how the date goes from 1st Jan to 1st Feb. 请注意日期如何从1月1日到2月1日。
series = read_csv("original_sample.csv")
series = series[["DATE_TIME", "REPLY_SIZE"]]
series["DATE_TIME"] = pd.to_datetime(series["DATE_TIME"])
series.to_csv("clean_sample.csv")
Why is this happening? 为什么会这样呢?
Note: I've edited out the REPLY_SIZE
for clarity. 注意:为了清楚起见,我已经编辑了
REPLY_SIZE
。
尝试series["DATE_TIME"] = pd.to_datetime(series["DATE_TIME"], format = "%m/%d/%Y %H:%M:%s")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.