Re: [Tutor] How to convert string to date time format?
Thanks a lot Steven. The %f is what I was missing. The "-08:00" is the UTC timezone, which is California, USA, which I believe is %z. Thanks! On Sat, Jul 20, 2019 at 7:50 PM Steven D'Aprano wrote: > 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. > > > > 02015-07-01 00:01:44.538420-08:00 > > 12015-07-01 00:27:58.717530-08:00 > > 22017-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 "", line 1, in > 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 "", line 1, in > 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 > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert string to date time format?
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. > > 02015-07-01 00:01:44.538420-08:00 > 12015-07-01 00:27:58.717530-08:00 > 22017-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 "", line 1, in 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 "", line 1, in 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert string to date time format?
Try python-dateutil - I found it to be one of the better python modules for dealing with dates. You may have to replace or strip some of the string. Johnf On 7/19/19 7:44 PM, C W wrote: Hello all, I have a date time string that looks like the following. 02015-07-01 00:01:44.538420-08:00 12015-07-01 00:27:58.717530-08:00 22017-07-01 07:07:48.391376-08:00 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? Also, does it matter if there are decimals after seconds? Thanks a lot! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to convert string to date time format?
Hello all, I have a date time string that looks like the following. 02015-07-01 00:01:44.538420-08:00 12015-07-01 00:27:58.717530-08:00 22017-07-01 07:07:48.391376-08:00 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? Also, does it matter if there are decimals after seconds? Thanks a lot! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor