pandas 将多个ISO时间列设置为to_datetime [英] Pandas multiple ISO time columns to_datetime
问题描述
我有多个包含ISO数据字符串的列,例如
I have multiple columns with ISO data strings like
CreateDate: 2020-04-30T06:12:29.424Z, ApprovedDate: 2020-04-30T06:21:30.504Z
当我逐列使用
df ['CreateDate'] = pd.to_datetime(df ['CreateDate'],infer_datetime_format = True,utc = True)
工作正常。但是
df [['CreateDate','ApprovedDate']] = pd.to_datetime(df [[''CreateDate','ApprovedDate'] ],infer_datetime_format = True,utc = True)
请给我
ValueError:组装映射至少需要指定[年,月,日]:[天,月,年]缺失
任何想法如何解决?尝试了所有to_datetime选项,但没有成功。
Any ideas how to solve that? Tried all to_datetime options, but no succes.
推荐答案
由于您拥有不错的ISO格式字符串,因此不需要调用 to_datetime
和任何关键字。因此,一个简单的 apply
应该是一个选择:
Since you have nice ISO format strings, you should not be required to call to_datetime
with any keywords. Therefore, a simple apply
should be an option:
import pandas as pd
df = pd.DataFrame({"CreateDate": ["2020-04-30T06:12:29.424Z"],
"ApprovedDate": ["2020-04-30T06:21:30.504Z"]})
df[['CreateDate', 'ApprovedDate']] = df[['CreateDate', 'ApprovedDate']].apply(pd.to_datetime)
# df
# CreateDate ApprovedDate
# 0 2020-04-30 06:12:29.424000+00:00 2020-04-30 06:21:30.504000+00:00
这篇关于 pandas 将多个ISO时间列设置为to_datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!