[Python] Can't parse a valid ISO 8601 datetime string pulled from CSV

Discussão em 'Python' iniciado por Stack, Agosto 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have a set of data that I am pulling from an Excel CSV. The column I am using has the timestamps in ISO 8601 format with fractional seconds (YYYY-MM-DDTHH:MM:SS.SSZ)

    I have tried using dateutil, isodate, and datetime to parse these values so I can create a line plot with a time axis. Neither of these libraries are working. The only thing I can think of is that dateutil.parser.parse and isodate.parse_datetime are getting caught on the header value of the column and breaking immediately because the word "time" is at index 0. Using time.values doesn't work either. I've tried a variety of ways to remove the header from the set but it either won't pull the data or just sets the header to zero which doesn't solve the problem.

    I've also double-checked that the column in the CSV file itself is of the text format.

    Code examples (the example list reproduces the same errors as the CSV pull):

    import pandas as pd
    import isodate

    time = ["2025-06-06T14:20:30.01Z", "2025-06-06T14:21:00.01Z", "2025-06-06T14:21:30.01Z"]
    df = pd.DataFrame(time)
    df = df.to_string()
    timeval = isodate.parse_datetime(df)

    >>> Error given: ISO 8601 time designator 'T' missing. Unable to parse datetime string


    import pandas as pd
    from datetime.parser import parse

    time = ["2025-06-06T14:20:30.01Z", "2025-06-06T14:21:00.01Z", "2025-06-06T14:21:30.01Z"]
    df = pd.DataFrame(time)
    df = df.to_string()
    timeval = parse(df)

    >>> Error given: Unknown string format


    I forgot what error this gave me but I tried a variant of this code too:

    def datetime_valid(dt_str):
    try:
    datetime.fromisoformat(dt_str.replace('Z', '+00:00'))
    except:
    return False
    return True


    I'm very new to Python so any help would be appreciated.

    Continue reading...

Compartilhe esta Página