将 UTC 列转换为 python pandas 中的日期时间

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

我想请求一点支持。我这里有一个 python 框架,其中包含以 UTC 格式给出的数据。我想将该列转换为日期格式。

Order Date
15-Feb-2024 UTC
17-Feb-2024 UTC
18-Feb-2024 UTC
02-Apr-2024 UTC
05-Mar-2024 UTC
04-Mar-2024 UTC
11-Apr-2024 UTC
12-Apr-2024 UTC
16-Mar-2024 UTC
04-Apr-2024 UTC
05-Feb-2024 UTC
05-Mar-2024 UTC
14-Apr-2024 UTC


df["Order Date"]=pd.to_datetime(df["Order Date"],utc=True,format='%d-%b-%Y')

应用上面的行会出现以下错误

time data "15-Feb-2024 UTC" doesn't match format "%d-%b-%Y", at position 0. You might want to try:
    - passing `format` if your strings have a consistent format;
    - passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
    - passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.

我尝试了所有选项,但没有成功。 有人能告诉我问题出在哪里吗? 我需要的只是将该列转换为日期列。我将不胜感激任何支持

pandas utc python-datetime
1个回答
0
投票

示例代码

import pandas as pd
data = {'Order Date': ['15-Feb-2024 UTC', '17-Feb-2024 UTC', '18-Feb-2024 UTC', '02-Apr-2024 UTC', '05-Mar-2024 UTC']}
df = pd.DataFrame(data)

df

    Order Date
0   15-Feb-2024 UTC
1   17-Feb-2024 UTC
2   18-Feb-2024 UTC
3   02-Apr-2024 UTC
4   05-Mar-2024 UTC

代码

如果您想转换为

datetime64[ns, UTC]

out = pd.to_datetime(df["Order Date"], utc=True, format='%d-%b-%Y UTC')

输出:

0   2024-02-15 00:00:00+00:00
1   2024-02-17 00:00:00+00:00
2   2024-02-18 00:00:00+00:00
3   2024-04-02 00:00:00+00:00
4   2024-03-05 00:00:00+00:00
Name: Order Date, dtype: datetime64[ns, UTC]

或者如果您想转换为

datetime[ns]

out = pd.to_datetime(df['Order Date'].str.replace(' UTC', ''))

输出:

0   2024-02-15
1   2024-02-17
2   2024-02-18
3   2024-04-02
4   2024-03-05
Name: Order Date, dtype: datetime64[ns]
© www.soinside.com 2019 - 2024. All rights reserved.