time data does not match format

前端 未结 6 1577
有刺的猬
有刺的猬 2020-12-02 14:24

I get the following error:

time data \'07/28/2014 18:54:55.099000\' does not match format \'%d/%m/%Y %H:%M:%S.%f\'

But I cannot see what pa

相关标签:
6条回答
  • 2020-12-02 14:36

    You have the month and day swapped:

    '%m/%d/%Y %H:%M:%S.%f'
    

    28 will never fit in the range for the %m month parameter otherwise.

    With %m and %d in the correct order parsing works:

    >>> from datetime import datetime
    >>> datetime.strptime('07/28/2014 18:54:55.099000', '%m/%d/%Y %H:%M:%S.%f')
    datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)
    

    You don't need to add '000'; %f can parse shorter numbers correctly:

    >>> datetime.strptime('07/28/2014 18:54:55.099', '%m/%d/%Y %H:%M:%S.%f')
    datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)
    
    0 讨论(0)
  • 2020-12-02 14:36

    While the above answer is 100% helpful and correct, I'd like to add the following since only a combination of the above answer and reading through the pandas doc helped me:

    2-digit / 4-digit year

    It is noteworthy, that in order to parse through a 2-digit year, e.g. '90' rather than '1990', a %y is required instead of a %Y.

    Infer the datetime automatically

    If parsing with a pre-defined format still doesn't work for you, try using the flag infer_datetime_format=True, for example:

    yields_df['Date'] = pd.to_datetime(yields_df['Date'], infer_datetime_format=True)
    

    Be advised that this solution is slower than using a pre-defined format.

    0 讨论(0)
  • 2020-12-02 14:37

    To compare date time, you can try this. Datetime format can be changed

    from datetime import datetime
    
    >>> a = datetime.strptime("10/12/2013", "%m/%d/%Y")
    >>> b = datetime.strptime("10/15/2013", "%m/%d/%Y")
    >>> a>b
    False
    
    0 讨论(0)
  • 2020-12-02 14:43

    I had the exact same error but with slightly different format and root-cause, and since this is the first Q&A that pops up when you search for "time data does not match format", I thought I'd leave the mistake I made for future viewers:

    My initial code:

    start = datetime.strptime('05-SEP-19 00.00.00.000 AM', '%d-%b-%y %I.%M.%S.%f %p')
    

    Where I used %I to parse the hours and %p to parse 'AM/PM'.

    The error:

    ValueError: time data '05-SEP-19 00.00.00.000000 AM' does not match format '%d-%b-%y %I.%M.%S.%f %p'

    I was going through the datetime docs and finally realized in 12-hour format %I, there is no 00... once I changed 00.00.00 to 12.00.00, the problem was resolved.

    So it's either 01-12 using %I with %p, or 00-23 using %H.

    0 讨论(0)
  • 2020-12-02 14:52

    I had a case where solution was hard to figure out. This is not exactly relevant to particular question, but might help someone looking to solve a case with same error message when strptime is fed with timezone information. In my case, the reason for throwing

    ValueError: time data '2016-02-28T08:27:16.000-07:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

    was presence of last colon in the timezone part. While in some locales (Russian one, for example) code was able to execute well, in another (English one) it was failing. Removing the last colon helped remedy my situation.

    0 讨论(0)
  • 2020-12-02 14:56

    No need to use datetime library. Using the dateutil library there is no need of any format:

    >>> from dateutil import parser
    >>> s= '25 April, 2020, 2:50, pm, IST'
    >>> parser.parse(s)
    datetime.datetime(2020, 4, 25, 14, 50)
    
    0 讨论(0)
提交回复
热议问题