I have a CSV file whose first column looks like this:
2018-12-10 20:00:25.855
2018-12-10 20:09:26
2018-12-10 20:13:27.31
2018-12-10 20:23:28
These are currently strings that I want to ultimately convert into just time objects (without the date). My first step was to remove the milliseconds but I can't figure out how to do that when some of the strings don't contain milliseconds.
I tried using this line to replace the milliseconds value but I end up with "data remains" error.
strdate = datetime.strptime(column[0], '%Y-%m-%d %H:%M:%S').replace(microsecond=0)
ValueError: unconverted data remains: .855
I have also tried stripping the string after the "." but nothing happens.
column[0].strip('.')
string = '20:00:25.855'
newstr = string[:string.find('.')]
print (newstr)
#20:00:25
Using the above logic outlined:
import pandas as pd
datadict = {
'Time':['2018-12-10 20:00:25.855',
'2018-12-10 20:09:26',
'2018-12-10 20:13:27.31',
'2018-12-10 20:23:28'],
}
df = pd.DataFrame(datadict)
df['Time'] = [row[11:row.find('.')] if '.' in row else row[11:] for row in df['Time']]
print (df)
Time
0 20:00:25
1 20:09:26
2 20:13:27
3 20:23:28
This returns the time portion of the datetime object, which you can then use for whatever calculations you need:
from datetime import datetime
def get_times():
times = ['2018-12-10 20:00:25.855','2018-12-10 20:09:26']
return [datetime.strptime(x[11:19],'%H:%M:%S').time() for x in times]
Output is: [datetime.time(20, 0, 25), datetime.time(20, 9, 26)]
To return a 'readable' form:
def get_times():
times = ['2018-12-10 20:00:25.855','2018-12-10 20:09:26']
dt_objects = [datetime.strptime(x[11:19],'%H:%M:%S').time() for x in times]
return [dt.strftime('%H:%M:%S') for dt in dt_objects]
Output is: ['20:00:25', '20:09:26']
Just in case you want to parse the time including micoseconds, you could conditionally expand the format string:
from datetime import datetime as DT
times =['2018-12-10 20:00:25.855',
'2018-12-10 20:09:26',
'2018-12-10 20:13:27.31',
'2018-12-10 20:23:28']
for t in times:
hasdot = '.' in t
print(DT.strptime(t[11:], '%H:%M:%S' + ('.%f' if hasdot else '' )).time())
#20:00:25.855000
#20:09:26
#20:13:27.310000
#20:23:28
datetime.fromisoformat()
handles both formats, with and without milliseconds.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.