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 code | Meaning | Example |
---|---|---|
%a | Abbreviated weekday name | Sun, Mon,… |
%A | Full weekday name | Sunday, Monday, … |
%w | Weekday as a decimal number | 0, 1, …, 6 |
%d | Day of the month as a zero-padded decimal | 01, 02, …, 31 |
%-d | Day of the month as a decimal number | 1, 2, …, 30 |
%b | Abbreviated month name | Jan, Feb, …, Dec |
%B | Full month name | January, February, … |
%m | Month as a zero-padded decimal number | 01, 02, …, 12 |
%-m | Month as a decimal number | 1, 2, …, 12 |
%y | Year without century as a zero-padded decimal number | 00, 01, …, 99 |
%-y | Year without century as a decimal number | 0, 1, …, 99 |
%Y | Year with century as a decimal number | 2013, 2014, … |
%H | Hour (24-hour clock) as a zero-padded decimal number | 00, 01, …, 23 |
%-H | Hour (24-hour clock) as a decimal number | 0, 1, …, 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number | 01, 02, …, 12 |
%-I | Hour (12-hour clock) as a zero-padded decimal number | 1, 2, …, 12 |
%p | Locale’s AM or PM | AM, PM |
%M | Minute as a zero-padded decimal number | 00, 01, …, 59 |
%-M | Minute as a decimal number | 0, 1, …, 59 |
%S | Second as a zero-padded number | 00, 01, …, 59 |
%-S | Second as a decimal number | 0, 1, …, 59 |
%f | Microsecond as a decimal number, zero-padded on the left | 000000 – 999999 |
%z | UTC offset in the form +HHMM or -HHMM | |
%Z | Time zone name | |
%j | Day of the year as a zero-padded decimal number | 001, 002, …, 366 |
%-j | Day of the year as a decimal number | 1, 2, …, 366 |
%U | Week 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 0 | 00, 01, …, 53 |
%W | Week 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 0 | 00, 01, …, 53 |
%c | Locale’s appropriate date and time representation | Mon Apr 11 08:56:02 2022 |
%x | Locale’s appropriate date representation | 04/11/22 |
%X | Locale’s appropriate time representation | 08:56:02 |
%% | A Literal ‘%’ character | % |
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!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.