user3240688
user3240688

Reputation: 1327

datetime from string with flexible string format

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?

Upvotes: 2

Views: 1689

Answers (2)

peijinAstro
peijinAstro

Reputation: 1

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)

Upvotes: 0

ralex
ralex

Reputation: 388

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.

Upvotes: 2

Related Questions