2

I'm parsing date time from string in Python. I like the fact that in pandas, I can define 1 single format, but pandas still allows me to be flexible in my input. As follows

>>> format = '%Y%m%d %H:%M:%S.%f'
>>> pd.to_datetime('20200106 23:00', format=format)
Timestamp('2020-01-06 23:00:00')          ## OK
>>> pd.to_datetime('20200106 23:00:00', format=format)
Timestamp('2020-01-06 23:00:00')          ## OK
>>> pd.to_datetime('20200106 23:00:00.000', format=format)
Timestamp('2020-01-06 23:00:00')          ## still ok

With Pandas, I can accept user input that looks like %H:%M or %H:%M:%S, or %H:%M:S.%f. As long as it doesn't violate the original format, it's fine. I like this flexibility.

Does that same behavior exist in datetime?

>>> datetime.strptime('20200106 23:00:00.000000', format)
datetime.datetime(2020, 1, 6, 23, 0)                 ## OK
>>> datetime.strptime('20200106 23:00:00', format)   ## NOT OK
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib64/python3.6/_strptime.py", line 362, in _strptime
     (data_string, format))
ValueError: time data '20200106 23:00:00' does not match format '%Y%m%d %H:%M:%S.%f'

strptime doesn't like it. What am I supposed to do? List all formats that user might put in? And do a bunch of tries?

2
  • 1
    No, this doesn't exist in stdlib datetime. Use a third-party library such as python-dateutil or pendulum. Commented Jan 17, 2020 at 17:47
  • python-dateutil will accept (but ignore) the fractional seconds. Commented Feb 19, 2020 at 6:05

2 Answers 2

2

There's a package(s) for that:

...and others. You can also roll your own regex if you find inputs that these packages don't parse to your liking.

Sign up to request clarification or add additional context in comments.

1 Comment

Here's an article that compares six of them (Dateutil, Arrow, Moment, Maya, Delorean, Freezegun): 6 Python datetime libraries - Opensource. It doesn't address the fractional second issue directly, and a couple of these (e.g., Maya) seem to be meta-libraries that sit on top of some of the other ones. Here's an article that discusses parsing in Maya, dateutil, and Arrow Converting Strings to datetime in Python. Just as an FYI for people looking.
0

dateparser https://dateparser.readthedocs.io/ can take flexible form of datetime input, also have non-English language support

>>> import dateparser
>>> dateparser.parse('12/12/12')
datetime.datetime(2012, 12, 12, 0, 0)
>>> dateparser.parse('Fri, 12 Dec 2014 10:55:50')
datetime.datetime(2014, 12, 12, 10, 55, 50)
>>> dateparser.parse('Martes 21 de Octubre de 2014')  # Spanish (Tuesday 21 October 2014)
datetime.datetime(2014, 10, 21, 0, 0)
>>> dateparser.parse('Le 11 Décembre 2014 à 09:00')  # French (11 December 2014 at 09:00)
datetime.datetime(2014, 12, 11, 9, 0)
>>> dateparser.parse('13 января 2015 г. в 13:34')  # Russian (13 January 2015 at 13:34)
datetime.datetime(2015, 1, 13, 13, 34)
>>> dateparser.parse('1 เดือนตุลาคม 2005, 1:00 AM')  # Thai (1 October 2005, 1:00 AM)
datetime.datetime(2005, 10, 1, 1, 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.