我有一个使用strptime()生成的datetime对象.
>>> tm
datetime.datetime(2010, 6, 10, 3, 56, 23)
Run Code Online (Sandbox Code Playgroud)
我需要做的是将分钟缩短到最接近的第10分钟.到目前为止我一直在做的是获取分钟值并使用round().
min = round(tm.minute, -1)
Run Code Online (Sandbox Code Playgroud)
但是,与上面的示例一样,当分钟值大于56时,它会给出无效时间.即:3:60
有什么更好的方法呢?datetime支持这个吗?
我正在努力使用熊猫来完善时间戳。
时间戳如下所示:
datetime.datetime(2017,06,25,00,31,53,993000)
datetime.datetime(2017,06,25,00,32,31,224000)
datetime.datetime(2017,06,25,00,33,11,223000)
datetime.datetime(2017,06,25,00,33,53,876000)
datetime.datetime(2017,06,25,00,34,31,219000)
datetime.datetime(2017,06,25,00,35,12,634000)
Run Code Online (Sandbox Code Playgroud)
如何四舍五入到最接近的秒数?
以前的iv尝试了这篇文章中的建议,但没有用:将 时间舍入到最接近的秒-Python
到目前为止,我的代码如下:
import pandas as pd
filename = 'data.csv'
readcsv = pd.read_csv(filename)
Run Code Online (Sandbox Code Playgroud)
根据文件头信息导入数据
log_date = readcsv.date
log_time = readcsv.time
log_lon = readcsv.lon
log_lat = readcsv.lat
log_heading = readcsv.heading
readcsv['date'] = pd.to_datetime(readcsv['date']).dt.date
readcsv['time'] = pd.to_datetime(readcsv['time']).dt.time
Run Code Online (Sandbox Code Playgroud)
将日期和时间合并为一个变量
timestamp = [datetime.datetime.combine(log_date[i],log_time[i]) for i in range(len(log_date))]
Run Code Online (Sandbox Code Playgroud)
创建数据框
data = {'timestamp':timestamp,'log_lon':log_lon,'log_lat':log_lat,'log_heading':log_heading}
log_data = pd.DataFrame(data,columns=['timestamp','log_lon','log_lat','log_heading'])
log_data.index = log_data['timestamp']
Run Code Online (Sandbox Code Playgroud)
我对python还是很陌生,所以请原谅我的无知