问题
I currently have a bunch of times in a column written like "27:32:18", meaning someone was waiting for 27 hours, 32 minutes, and 18 seconds. I keep getting "ValueError: hour must be in 0..23" whenever I try to parse these values.
How should I go about parsing those values or converting them to a more standard format? I tried the following as a test on a single value:
time1 = "56:42:12"
time2 = time1.split(':')
time2 = [int(n) for n in time2]
time2.insert(0, time2[0] // 24)
time2[1] %= 24
At that point, time2 is a list consisting of [2, 8, 42, 12], which is equivalent to 2 days, 8 hours, 42 minutes, and 12 seconds. How would I go about converting that to a Python datetime representation in days, hours, minutes, and seconds in a way that will allow Python to parse it? Note that I will eventually be doing unsupervised clustering on these time values, which represent waiting times.
回答1:
You don't have a date, you have a time duration. That may be related to dates and timestamps, but only in that the same units of time are involved and are displayed similarly to timestamps.
As such, you cannot use dateutil
for parsing such values. It is easy enough to split out and parse yourself:
hours, minutes, seconds = map(int, time1.split(':'))
You can then use a datetime.timedelta()
object to represent the duration:
td = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
This'll then track the delta in terms of days, seconds and microseconds:
>>> import datetime
>>> time1 = "56:42:12"
>>> hours, minutes, seconds = map(int, time1.split(':'))
>>> datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
datetime.timedelta(2, 31332)
来源:https://stackoverflow.com/questions/24432607/how-do-i-parse-dates-with-more-than-24-hours-in-dateutils-parser-in-python-3