[Tutor] How to convert string to date time format?
Steven D'Aprano
steve at pearwood.info
Sat Jul 20 19:43:08 EDT 2019
On Fri, Jul 19, 2019 at 10:44:36PM -0400, C W wrote:
> Hello all,
>
> I have a date time string that looks like the following.
>
> 0 2015-07-01 00:01:44.538420-08:00
> 1 2015-07-01 00:27:58.717530-08:00
> 2 2017-07-01 07:07:48.391376-08:00
I assume that the leading number and spaces "0 " etc are NOT part of
the strings.
> I have tried the following two different methods, both did not work.
> Method one: pandas
> import pandas as pd
> stamp = pd.to_datetime(my_string, format='%Y%m%d %H:%M:%S')
>
> Method two: datetime package
> from datetime import datetime
> datetime.strptime(my_string, '%Y-%m-%d %H:%M:%S')
>
>
> Are both ways suppose to work?
Not unless the string format matches the actual string. You can't expect
to convert a string unless it matches the format.
> Also, does it matter if there are decimals
> after seconds?
Of course it matters.
Did you read the error message? The single most important skill for a
programmer is to READ THE ERROR MESSAGE and pay attention to what it
tells you went wrong:
py> datetime.strptime('2015-07-01 00:01:44.538420-08:00', '%Y-%m-%d %H:%M:%S')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/local/lib/python3.5/_strptime.py", line 346, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: .538420-08:00
See how the error message tells you that it couldn't convert the string
because there is data left over at the end. The first thing to do is
handle the microseconds.
Googling gets the answer: use "%f" as the code for fractional seconds.
https://duckduckgo.com/?q=strptime+seconds+with+decimals
py> datetime.strptime('2015-07-01 00:01:44.538420-08:00', '%Y-%m-%d %H:%M:%S.%f')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/local/lib/python3.5/_strptime.py", line 346, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: -08:00
Now we're making progress! The error message has changed. Now you just
need to decide what the "-08:00" part means, and change the format
string appropriately.
--
Steve
More information about the Tutor
mailing list