To start off, screw the unconverted data remains error while using strptime in Python 2.x or 3.x. Of the many Python errors I’ve gotten this has to be one of the more vague ones. That being said, I do hate working with times and dates. Regardless, here’s your solution.
Recreating the Problem
Enter this correctly and you should get the unconverted data remains error while using strptime.
from datetime import datetime random_date = '2019-02-02 01:01:01' # This is where the issue occurs datetime.strptime(random_date, '%Y-%m-%d')
I’m guessing, you started off with a string date that you pulled from an API, file, or wherever else. You now need to add a few days to the date, compare it to another date, or one of many other date specific tasks. The first step is to get it the string date back into a date type. This is most likely the reason why you’re trying to use strptime. It takes a string input and uses the second chunk, ”%Y-%m-%d’ in the above example, to infer the formatting and convert the string into a date. You are specifying what piece of information is month, day, year, second, hour, minute.
In our example above you can see the starting time has hour, minute, and second data. We don’t tell strptime to expect this information which is why we are getting the error.
from datetime import datetime random_date = '2019-02-02 01:01:01' # Adding the time part to the following will fix the error datetime.strptime(random_date, '%Y-%m-%d %H:%M:%S')
The error should no longer be getting thrown with the changes made above. That being said, this line will be specific to the date-time you’re pulling in so will probably need some tweaking on your end. Care to continue your you Python adventures? Check out this article. Happy coding!