Skip to main content

How to Solve "ValueError: unconverted data remains" with strptime in Python

The ValueError: unconverted data remains error in Python arises when using the datetime.strptime() method to parse a date/time string, and the provided format string doesn't match the entire input string.

This guide explains the cause of this error and provides practical solutions to ensure correct parsing, covering both complete and partial string matching.

Understanding the Error

The datetime.strptime() function parses a string representing a date and/or time according to a specific format. If the format string you provide doesn't consume the entire input string, Python raises the ValueError: unconverted data remains error.

from datetime import datetime

dt = '2025-07-01 08:30:00'

try:
date_1 = datetime.strptime(dt, '%Y-%m-%d') # Incomplete format string
except ValueError as e:
print(e) # Output: unconverted data remains: 08:30:00
  • The format string '%Y-%m-%d' only specifies the year, month and day, which is not the complete string representation, which also includes time.

Solutions

To resolve this ValueError, use these methods:

Matching the Complete Format String

The most straightforward solution is to provide a format string that matches the entire input string:

from datetime import datetime

dt = '2025-07-01 08:30:00'
date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S') # Correct, complete format string
print(date_1) # Output: 2025-07-01 08:30:00
  • By specifying the format %Y-%m-%d %H:%M:%S the code correctly matches and parses the input, including the time component.

Handling Milliseconds

If your input string includes milliseconds, include .%f in your format string:

from datetime import datetime

dt = '2025-07-01 08:30:00.000123'
date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S.%f')
print(date_1) # Output: 2024-07-01 08:30:00.000123
  • %f matches microseconds (000000-999999), but leading zeros are not required. So, it correctly matches .000123 and .123.

Handling Excess Characters with try-except

If you're unsure of the exact format or if the input might contain extra characters, use a try-except block to handle potential ValueError exceptions:

from datetime import datetime

dt = '2025-07-01 08:30:00.000123 ABC' # Extra characters

try:
date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S.%f')
print(date_1) # This is never reached.
except ValueError:
print('A valueError occurred') # Output: A valueError occurred
date_1 = datetime.today() # Set some other default value

print(date_1)
  • The try/except block allows the rest of the script to run even if a ValueError occurs.

Removing Excess Characters with String Slicing

If you know the length of the valid date/time portion, use string slicing to remove extra characters:

from datetime import datetime

dt = '2025-07-01 08:30:00 ABC 123'
print(dt[:-8]) # Output: 2025-07-01 08:30:00
date_1 = datetime.strptime(dt[:-8], '%Y-%m-%d %H:%M:%S')

print(date_1) # Output: 2025-07-01 08:30:00
  • We are extracting the date/time part of the string using slicing [:-8] (everything up to the last 8 characters).

Removing Excess Characters with split() and join()

If you need to separate different components based on a delimiter you can use the split() method to extract them:

from datetime import datetime

dt = '2025-07-01 08:30:00 ABC 123'

try:
date_1 = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
print(date_1) # Never reached
except ValueError:
print('A valueError occurred') # Gets printed
components = dt.split(' ') # split by space
print(components) # Output: ['2025-07-01', '08:30:00', 'ABC', '123']

dt_components = ' '.join(components[:2]) # Join only necessary elements.
print(dt_components) # Output: 2025-07-01 08:30:00

date_1 = datetime.strptime(dt_components, '%Y-%m-%d %H:%M:%S')

print(date_1) # Output: 2025-07-01 08:30:00
  • dt.split(' ') splits the string into parts based on the space character.
  • The components[:2] gets the date and time components.
  • ' '.join() rejoins the date and time parts into a correctly formatted string.

Conclusion

The ValueError: unconverted data remains error is a common issue when the input string and the format string used in datetime.strptime() don't match.

This guide demonstrated how to correct the format string, how to use a try-except block to provide a default date/time, and how to clean the input string by using slicing, or other string manipulation methods.

By applying the appropriate solutions, you can ensure your Python code handles date and time parsing robustly and avoids unexpected errors.