Select Page

How to Solve Python ValueError: unconverted data remains

by | Programming, Python, Tips

If you want to format a datetime using datetime.strptime(), the format needs to match the original string. If there is a mismatch between the format and the original string, strptime will not be able to parse the string and will raise the ValueError: unconverted data remains.

To solve this error, ensure that the format you are using matches the string you want to parse.

This tutorial will go through the error in detail and how to solve it with code examples.


Python ValueError: unconverted data remains

In Python, a value is a piece of information stored within a particular object. We will encounter a ValueError in Python when using a built-in operation or function that receives an argument that is the right type but an inappropriate value. In this specific error, the data we pass to the datetime.strptime() method is the correct type, string, but has an incorrect format.

The strptime method is available in the DateTime module and formats a timestamp in string format to a datetime object. The syntax of the method is as follows:

datetime.strptime(date_string, format)

Parameters

  • date_string: Required. String to convert to datetime object.
  • format: Required. Format code

Returns

The method returns a datetime object corresponding to date_string, parsed according to format.

ValueError is raised if time.strptime() cannot parse date_string and format.

Example

Let’s look at an example where we want to convert a string representing a string to a datetime object.

from datetime import datetime

# Define date string

date_string = '19 March, 2021 21:23'

# Convert date string to datetime object

date_object = datetime.strptime(date_string, "%d %B, %Y")

print(date_object)

Let’s run the code to see what happens:

~/opt/anaconda3/lib/python3.8/_strptime.py in _strptime(data_string, format)
    350                          (data_string, format))
    351     if len(data_string) != found.end():
--> 352         raise ValueError("unconverted data remains: %s" %
    353                           data_string[found.end():])
    354 

ValueError: unconverted data remains:  21:23

The error occurs because the part of the string ‘21:23‘ does not match the format_code ‘%d %B, %Y

Solution

To solve this error we need to include hours and minutes in the format code. The format code for hours is %H and for minutes is %M. Let’s look at the revised code:

from datetime import datetime

# Define date string

date_string = '19 March, 2021 21:23'

# Convert date string to datetime object

date_object = datetime.strptime(date_string, "%d %B, %Y %H:%M")

print(date_object)

Let’s run the code to see the result:

2021-03-19 21:23:00

We successfully converted the string to a datetime object using datetime.strptime().

Format Code List

Format codeMeaningExample
%aAbbreviated weekday nameSun, Mon,…
%AFull weekday nameSunday, Monday, …
%wWeekday as a decimal number0, 1, …, 6
%dDay of the month as a zero-padded decimal01, 02, …, 31
%-dDay of the month as a decimal number1, 2, …, 30
%bAbbreviated month nameJan, Feb, …, Dec
%BFull month nameJanuary, February, …
%mMonth as a zero-padded decimal number01, 02, …, 12
%-mMonth as a decimal number1, 2, …, 12
%yYear without century as a zero-padded decimal number00, 01, …, 99
%-yYear without century as a decimal number0, 1, …, 99
%YYear with century as a decimal number2013, 2014, …
%HHour (24-hour clock) as a zero-padded decimal number00, 01, …, 23
%-HHour (24-hour clock) as a decimal number0, 1, …, 23
%IHour (12-hour clock) as a zero-padded decimal number01, 02, …, 12
%-IHour (12-hour clock) as a zero-padded decimal number1, 2, …, 12
%pLocale’s AM or PMAM, PM
%MMinute as a zero-padded decimal number00, 01, …, 59
%-MMinute as a decimal number0, 1, …, 59
%SSecond as a zero-padded number00, 01, …, 59
%-SSecond as a decimal number0, 1, …, 59
%fMicrosecond as a decimal number, zero-padded on the left000000 – 999999
%zUTC offset in the form +HHMM or -HHMM
%ZTime zone name
%jDay of the year as a zero-padded decimal number001, 002, …, 366
%-jDay of the year as a decimal number1, 2, …, 366
%UWeek number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are in week 000, 01, …, 53
%WWeek number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are in week 000, 01, …, 53
%cLocale’s appropriate date and time representationMon Apr 11 08:56:02 2022
%xLocale’s appropriate date representation04/11/22
%XLocale’s appropriate time representation08:56:02
%%A Literal ‘%’ character%
Table of format codes for datetime.strptime

Summary

Congratulations on reading to the end of this tutorial! The ValueError: unconverted data remains occurs when the string has a format that does not match the format code of the datetime.strptime() method.

For further reading on datetime objects, go to the article: How to Solve Python AttributeError: ‘Series’ object has no attribute ‘strftime’

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!