简体   繁体   English

pandas to_datetime格式来自同一列的日期格式不同

[英]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.

相关问题 如何将 pandas dataframe 列中的两种不同日期格式转换为相同格式? - How to convert two different date formats from a pandas dataframe column into same format? 熊猫 - 同一列中的不同时间格式 - Pandas - Different time formats in the same column 同一列中具有不同格式的熊猫日期时间 - Pandas datetimes with different formats in the same column 为什么 Pandas to_datetime() function 在同一数据系列 (YYYY-DD-MM) 和 (YYYY-MM-DD) 中以两种不同格式返回 (DD-MM-YYYY) 日期时间? - Why is Pandas to_datetime() function returning (DD-MM-YYYY) datetime in two different formats in the same data series (YYYY-DD-MM) and (YYYY-MM-DD)? 将同一列中的多个非日期格式转换为正确的 pandas 日期时间 object - Convert multiple non date formats within same column into a proper pandas datetime object pandas.read_csv() 可以在同一列中应用不同的日期格式! 这是一个已知的错误吗? 如何解决? - pandas.read_csv() can apply different date formats within the same column! Is it a known bug? How to fix it? 加载 csv Pandas 时以不同格式出现的日期列 - Date column coming in in different formats when loading csv Pandas 熊猫优雅地处理具有不同日期时间格式的列 - pandas handle column with different date time formats gracefully 在 Pandas 中将多种日期格式转换为日期时间 - Convert multiple date formats to datetime in pandas 不同的日期时间格式 pandas 可以自动检测 - Different datetime formats pandas can auto detect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM