I have a list of strings that may be time objects. Note: NOT datetime objects or timestamp objects. These are times without a reference to epoch or date. For example:
time_strings = ['6:00PM', '7:00pm', '8:00am', '17:00:00', '4PM']
Is there a good parsing utility that can convert these into datetime.time objects as opposed to datetime.datetime objects (which by default input the current date)?
The standard 'datetime.datetime.strptime() both requires that the time be of a known format and yields a datetime.datetime.object, for example:
from datetime import datetime
datetime.strptime('6:00', '%H:%M')
yields: datetime.datetime(1900, 1, 1, 6, 0)
I have tried the following:
from dateutil.parser import parse
parse(time_strings[0])
yields: datetime.datetime(2017, 8, 24, 18, 0)
import pandas as pd
pd.to_datetime(time_strings[0])
yields: Timestamp('2017-08-24 18:00:00')
from pytimeparse import parse as tparse
tparse(time_strings[0])
fails (returns empty value)
import parsedatetime
cal = parsedatetime.Calendar()
cal.parse(time_strings[0])
Yields: (time.struct_time(tm_year=2017, tm_mon=8, tm_mday=24, tm_hour=18, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=236, tm_isdst=1), 2)
For my purposes, it is critical that no date be inferred if not in the data.
Is there a better python utility (python 3) that converts various human readable time expressions into a datetime.time object?