pandas datetime以转换新的日期格式(时-分-秒)?

问题描述 投票:0回答:3

我想将df['time']转换为新系列df['t']的时分秒格式(例如00:02:58。

1713   2019-09-24 00:02:58
1714   2019-09-24 00:12:11
1715   2019-09-24 00:13:42
……
Name: time, dtype: datetime64[ns]

正在尝试:

pd.to_datetime(df['time'],format='%H:%M:%S')仍然使用相同的格式。

1713   2019-09-24 00:02:58
1714   2019-09-24 00:12:11
1715   2019-09-24 00:13:42
……
Name: time, dtype: datetime64[ns]

更新:

我想要日期时间格式。

pandas
3个回答
0
投票

使用dt.strftime

df["T"] = df["Time"].dt.strftime("%H:%M:%S")

0
投票

尝试dt.time

df['time'] = df['time'].dt.time

0
投票

我希望有timedelta

df['new_time'] = df['time'] - df['time'].dt.normalize()

输出:

                    time new_time
1713 2019-09-24 00:02:58 00:02:58
1714 2019-09-24 00:12:11 00:12:11
1715 2019-09-24 00:13:42 00:13:42

然后,您可以轻松访问其他信息,例如

df.new_time.dt.seconds

给予:

1713    178
1714    731
1715    822
Name: new_time, dtype: int64
© www.soinside.com 2019 - 2024. All rights reserved.