gpt4 book ai didi

python - 我正在尝试将时间列转换为日期时间,但该列看起来像这样 '2020-01-07T22:24:20.000Z'

转载 作者:行者123 更新时间:2023-12-04 07:43:10 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I parse an ISO 8601-formatted date?

(28 个回答)


4 个月前关闭。




我从 Twitter 上抓取了数据,我的日期格式为 2020-01-07T22:24:20.000Z .我需要将其转换为日期时间格式。

最佳答案

我们可以使用 pd.to_datetime 方法将字符串转换为 pandas datetime(Timestamp) 类型。

pd.to_datetime('2020-01-07T22:24:20.000Z')
输出
Timestamp('2020-01-07 22:24:20+0000', tz='UTC')
如果您有一列需要转换为日期时间,则
输入
df = DataFrame({
'time':['2020-01-07T22:24:20.000Z', '2020-01-08T22:24:20.000Z']
})

time
0 2020-01-07T22:24:20.000Z
1 2020-01-08T22:24:20.000Z
转换
df['time'] = pd.to_datetime(df.time)
df
输出
    time
0 2020-01-07 22:24:20+00:00
1 2020-01-08 22:24:20+00:00

关于python - 我正在尝试将时间列转换为日期时间,但该列看起来像这样 '2020-01-07T22:24:20.000Z',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67341753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com