Last updated: Apr 11, 2024
Reading timeยท4 min
try/except
statement to handle the errorThe 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.
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')
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).
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
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.
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
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.
try/except
statement to handle the errorYou can also use a try/except
statement to handle the error.
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 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.
You can also solve the error by removing the excess characters.
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
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.
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
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.
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 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.
You can learn more about the related topics by checking out the following tutorials: