ValueError: unconverted data remains in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. ValueError: unconverted data remains in Python
  2. Using a try/except statement to handle the error
  3. Removing the excess characters to solve the error

# ValueError: unconverted data remains in Python [Solved]

The Python "ValueError: unconverted data remains" occurs when you call the strptime method with a format string that doesn't cover the entire date string.

To solve the error, pass a format string that covers the entire date or date time string to the strptime() method or slice the date string to remove the excess characters.

Here is an example of how the error occurs.

main.py
from datetime import datetime dt = '2024-09-24 08:30:00' # โ›”๏ธ ValueError: unconverted data remains: 08:30:00 date_1 = datetime.strptime(dt, '%Y-%m-%d')

value error unconverted data remains

The datetime.strptime() method returns a datetime object that corresponds to the provided date string, parsed according to the format.

If you have a date string that is formatted in a different way, use this table of the docs to look up the format codes you should pass as the second argument to the strptime() method.

The error occurred because the dt string has a date and time component but we only specified the date component in the format string.

One way to solve the error is to specify the complete format string (one that covers all date and time components).

main.py
from datetime import datetime dt = '2024-09-24 08:30:00' date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S') print(date_1) # ๐Ÿ‘‰๏ธ 2024-09-24 08:30:00

specify complete format string

The code for this article is available on GitHub

Notice that the format string now also covers the time component, so everything works as expected.

If your datetime string also contains the milliseconds, make sure to include them in the format string.

main.py
from datetime import datetime dt = '2024-09-24 08:30:00.000123' date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S.%f') print(date_1) # ๐Ÿ‘‰๏ธ 2024-09-24 08:30:00.000123

include milliseconds in format string

I've written a detailed article on how to use the strptime method in Python.

For a complete list of the format codes that the strptime method supports, check out this table of the official docs.

# Using a try/except statement to handle the error

You can also use a try/except statement to handle the error.

main.py
from datetime import datetime dt = '2024-09-24 08:30:00.000123 ABC' try: date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S.%f') print(date_1) except ValueError: print('A valueError occurred') date_1 = datetime.today() print(date_1) # ๐Ÿ‘‰๏ธ 2023-06-30 10:22:59.772309
The code for this article is available on GitHub

The try block tries to parse the date and time string using the expected format string.

However, the date and time string contains excess characters, so a ValueError is raised and is then handled in the except block.

We set the date_1 variable to the current date and time, however, you can initialize the variable to any other value that suits your use case.

# Removing the excess characters to solve the error

You can also solve the error by removing the excess characters.

main.py
from datetime import datetime dt = '2024-09-24 08:30:00' try: date_1 = datetime.strptime(dt, '%Y-%m-%d') print(date_1) except ValueError: print('A valueError occurred') components = dt.split(' ') print(components) # ๐Ÿ‘‰๏ธ ['2024-09-24', '08:30:00'] date_1 = datetime.strptime(components[0], '%Y-%m-%d') print(date_1) # ๐Ÿ‘‰๏ธ 2024-09-24 00:00:00

removing excess characters to solve the error

The code for this article is available on GitHub

We used the str.split() method to split the datetime string on each space.

The first string in the resulting list contains the date components.

We passed the date components to the datetime.strptime() method and specified a format string that only covers the date components.

In some cases, you might have to take the date and time components into consideration and remove the excess characters.

main.py
from datetime import datetime dt = '2024-09-24 08:30:00 ABC 123' try: date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S') print(date_1) except ValueError: print('A valueError occurred') components = dt.split(' ') # ๐Ÿ‘‡๏ธ ['2024-09-24', '08:30:00', 'ABC', '123'] print(components) dt_components = ' '.join(components[:2]) print(dt_components) # ๐Ÿ‘‰๏ธ 2024-09-24 08:30:00 date_1 = datetime.strptime(dt_components, '%Y-%m-%d %H:%M:%S') print(date_1) # ๐Ÿ‘‰๏ธ 2024-09-24 08:30:00
The code for this article is available on GitHub

We split the date and time string on each space.

The first list item contains the date components and the second contains the time components.

We used list slicing to get the first two elements of the list and joined them into a string with a space as the separator.

The last step is to pass the date and time components as the first argument and a suitable format string as the second argument to strptime().

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

When the start index is omitted, it is considered to be 0.

If you need to remove the last N characters from a String in Python, use a negative stop index.

main.py
from datetime import datetime dt = '2024-09-24 08:30:00 ABC 123' try: date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S') print(date_1) except ValueError: print('A valueError occurred') print(dt[:-8]) # ๐Ÿ‘‰๏ธ 2024-09-24 08:30:00 date_1 = datetime.strptime(dt[:-8], '%Y-%m-%d %H:%M:%S') print(date_1) # ๐Ÿ‘‰๏ธ 2024-09-24 08:30:00
The code for this article is available on GitHub

The code sample removes the last 8 excess characters from the string.

Once the excess characters are removed, we can use the specified format string.

If you have a date string that is formatted in a different way, use this table of the docs to look up the format codes you should pass as the second argument to the strptime() method.

I've written a detailed article on how to use the strptime method in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev