You can use first read_csv
with parameter parse_dates
for create datetime
s from column date
and time
and then dt.round
for round datetime
s:
import pandas as pd
temp=u"""date,time,lon,lat,heading
2017-06-25,00:31:53.993000,48.1254,17.1458,a
2017-06-25,00:32:31.224000,48.1254,17.1458,a
2017-06-25,00:33:11.223000,48.1254,17.1458,a
2017-06-25,00:33:53.876000,48.1254,17.1458,a
2017-06-25,00:34:31.219000,48.1254,17.1458,a
2017-06-25,00:35:12.634000,48.1254,17.1458,a"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), parse_dates={'timestamp':['date','time']})
print (df)
timestamp lon lat heading
0 2017-06-25 00:31:53.993 48.1254 17.1458 a
1 2017-06-25 00:32:31.224 48.1254 17.1458 a
2 2017-06-25 00:33:11.223 48.1254 17.1458 a
3 2017-06-25 00:33:53.876 48.1254 17.1458 a
4 2017-06-25 00:34:31.219 48.1254 17.1458 a
5 2017-06-25 00:35:12.634 48.1254 17.1458 a
print (df.dtypes)
timestamp datetime64[ns]
lon float64
lat float64
heading object
dtype: object
df['timestamp'] = df['timestamp'].dt.round('1s')
print (df)
timestamp lon lat heading
0 2017-06-25 00:31:54 48.1254 17.1458 a
1 2017-06-25 00:32:31 48.1254 17.1458 a
2 2017-06-25 00:33:11 48.1254 17.1458 a
3 2017-06-25 00:33:54 48.1254 17.1458 a
4 2017-06-25 00:34:31 48.1254 17.1458 a
5 2017-06-25 00:35:13 48.1254 17.1458 a
EDIT:
If you want set column with datetimes to index
also:
import pandas as pd
temp=u"""date,time,lon,lat,heading
2017-06-25,00:31:53.993000,48.1254,17.1458,a
2017-06-25,00:32:31.224000,48.1254,17.1458,a
2017-06-25,00:33:11.223000,48.1254,17.1458,a
2017-06-25,00:33:53.876000,48.1254,17.1458,a
2017-06-25,00:34:31.219000,48.1254,17.1458,a
2017-06-25,00:35:12.634000,48.1254,17.1458,a"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), parse_dates={'timestamp':['date','time']}, index_col=['timestamp'])
print (df)
lon lat heading
timestamp
2017-06-25 00:31:53.993 48.1254 17.1458 a
2017-06-25 00:32:31.224 48.1254 17.1458 a
2017-06-25 00:33:11.223 48.1254 17.1458 a
2017-06-25 00:33:53.876 48.1254 17.1458 a
2017-06-25 00:34:31.219 48.1254 17.1458 a
2017-06-25 00:35:12.634 48.1254 17.1458 a
print (df.index)
DatetimeIndex(['2017-06-25 00:31:53.993000', '2017-06-25 00:32:31.224000',
'2017-06-25 00:33:11.223000', '2017-06-25 00:33:53.876000',
'2017-06-25 00:34:31.219000', '2017-06-25 00:35:12.634000'],
dtype='datetime64[ns]', name='timestamp', freq=None)
df.index = df.index.round('1s')
print (df)
lon lat heading
timestamp
2017-06-25 00:31:54 48.1254 17.1458 a
2017-06-25 00:32:31 48.1254 17.1458 a
2017-06-25 00:33:11 48.1254 17.1458 a
2017-06-25 00:33:54 48.1254 17.1458 a
2017-06-25 00:34:31 48.1254 17.1458 a
2017-06-25 00:35:13 48.1254 17.1458 a