在Pandas数据框中有两列:
"CREATED ON DATE" (dtype: datetime64[ns]) e.g. 2019-06-16
"CREATED AT TIME" (dtype: object) e.g. 19:46:14
由于类型不匹配,仅添加字段无效。
df["CREATED DATETIME"] = df["CREATED ON DATE"] + df["CREATED AT TIME"]
如何将这两列合并为1个日期时间字段“ CREATED DATETIME”?
答案 0 :(得分:1)
使用to_timedelta
:
df["CREATED DATETIME"] = df["CREATED ON DATE"] + pd.to_timedelta(df["CREATED AT TIME"])
如果对象在列中使用python时间,请将其转换为string
之前的时间:
df["CREATED DATETIME"] = (df["CREATED ON DATE"] +
pd.to_timedelta(df["CREATED AT TIME"].astype(str)))