[英]How to convert from 'object/string' to 'time' type in pandas.DataFrame?
I'm trying to convert the data-type of the time column in my dataset.我正在尝试转换数据集中时间列的数据类型。
I found the below command using pandas from here only but while executing I'm getting the date as well in the pre-defined format for all the cells.我仅从此处使用熊猫找到了以下命令,但在执行时我也以所有单元格的预定义格式获取日期。
d2['Time of Card Posting']=pd.to_datetime(d2['Time of Card Posting'],format='%H:%M:%S')
PS: I tried the following command as well to extract time from it but it changes the data-type back to object! PS:我也尝试了以下命令来从中提取时间,但它会将数据类型更改回对象!
d2['Time of Card Posting']=pd.to_datetime(d2['Time of Card Posting'],format='%H:%M:%S').dt.time
I would do it this way, if you just want the time:如果你只是想要时间,我会这样做:
import pandas as pd
d2 = pd.date_range(
"2018-01-01", periods=5, freq="H", name="Time of Card Posting"
).to_frame()
d2_new = d2.assign(
time=lambda x: (
pd.to_datetime(x["Time of Card Posting"], format="%H:%M:%S").astype(str)
).str[-8:]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.