ValueError:时间数据“2020-04-29T17:18:25.574824Z”与格式“%Y-%m-%dT%H:%M:%S%z”不匹配,位于位置 3 - I正在将对象数据类型更改为日期时间

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

我正在尝试将对象数据类型更改为日期时间

# My Code:

import pandas as pd

# Sample data
data = {'Channel_Started': ['2013-05-18T04:46:00Z', '2018-01-16T15:55:22Z', '2016-12-12T05:00:55Z', '2020-04-29T17:18:25.574824Z', '2023-11-24T21:56:18.571502Z', '2020-06-13T05:20:37.182391Z', '2015-08-22T13:22:51Z', '2016-11-16T14:52:31Z', '2020-05-09T20:35:27.233665Z', '2022-03-16T22:09:57.246468Z', '2023-02-11T05:55:01.369504Z', '2023-03-10T12:18:40.189285Z', '2005-12-16T09:01:28Z', '2013-09-05T01:15:06Z', '2017-07-13T09:30:23Z', '2020-08-05T16:09:28.304314Z']}

# Create DataFrame
df = pd.DataFrame(data)

# Convert to datetime
df['Channel_Started'] = pd.to_datetime(df['Channel_Started'])

# Extract date
df['Channel_Started'] = df['Channel_Started'].dt.date

print(df)

输出:

# ValueError: time data "2020-04-29T17:18:25.574824Z" doesn't match format "%Y-%m-%dT%H:%M:%S%z", at position 3. 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.

根据提供的错误输出,日期时间字符串“2020-04-29T17:18:25.574824Z”的格式似乎存在问题,与预期格式“%Y-%m-%dT%H”不匹配:%M:%S%z"。该代码尝试使用 Python 中的 Pandas 库将此字符串转换为日期时间对象,但遇到 ValueError,因为指定的格式与实际的日期时间字符串不匹配。

我期待这个输出:

   Channel_Started
0       2013-05-18
1       2018-01-16
2       2016-12-12
3       2020-04-29
4       2023-11-24
5       2020-06-13
6       2015-08-22
7       2016-11-16
8       2020-05-09
9       2022-03-16
10      2023-02-11
11      2023-03-10
12      2005-12-16
13      2013-09-05
14      2017-07-13
15      2020-08-05
python pandas valueerror
1个回答
1
投票

您有混合格式。可以推断每个值的实际格式如下:

import pandas as pd

data = {
    "Channel_Started": [
        "2013-05-18T04:46:00Z",
        "2018-01-16T15:55:22Z",
        "2016-12-12T05:00:55Z",
        "2020-04-29T17:18:25.574824Z",
        "2023-11-24T21:56:18.571502Z",
        "2020-06-13T05:20:37.182391Z",
        "2015-08-22T13:22:51Z",
        "2016-11-16T14:52:31Z",
        "2020-05-09T20:35:27.233665Z",
        "2022-03-16T22:09:57.246468Z",
        "2023-02-11T05:55:01.369504Z",
        "2023-03-10T12:18:40.189285Z",
        "2005-12-16T09:01:28Z",
        "2013-09-05T01:15:06Z",
        "2017-07-13T09:30:23Z",
        "2020-08-05T16:09:28.304314Z",
    ]
}

df = pd.DataFrame(data)

df["Channel_Started"] = pd.to_datetime(df["Channel_Started"], format="mixed")

print(df)

输出:

                    Channel_Started
0         2013-05-18 04:46:00+00:00
1         2018-01-16 15:55:22+00:00
2         2016-12-12 05:00:55+00:00
3  2020-04-29 17:18:25.574824+00:00
4  2023-11-24 21:56:18.571502+00:00
5  2020-06-13 05:20:37.182391+00:00
6         2015-08-22 13:22:51+00:00
7         2016-11-16 14:52:31+00:00
8  2020-05-09 20:35:27.233665+00:00
9  2022-03-16 22:09:57.246468+00:00
10 2023-02-11 05:55:01.369504+00:00
11 2023-03-10 12:18:40.189285+00:00
12        2005-12-16 09:01:28+00:00
13        2013-09-05 01:15:06+00:00
14        2017-07-13 09:30:23+00:00
15 2020-08-05 16:09:28.304314+00:00
© www.soinside.com 2019 - 2024. All rights reserved.