将时间舍入到最接近的秒数-Python [英] Rounding time off to the nearest second - Python
问题描述
我有一个大型数据集,日期超过50万看起来像这样的时间戳:
日期时间
2017-06-25 00:31:53.993
2017-06-25 00:32:31.224
2017-06-25 00:33:11.223
2017-06-25 00:33:53.876
2017-06-25 00: 34:31.219
2017-06-25 00:35:12.634
我如何取整
我的代码如下:
readcsv = pd.read_csv(文件名)
log_date = readcsv.date
log_time = readcsv.time
readcsv ['date'] = pd.to_datetime(readcsv [ 'date'])。dt.date
readcsv ['time'] = pd.to_datetime(readcsv ['time'])。dt.time
timestamp = [datetime.datetime.combine(log_date [ i],log_time [i])for i in range(len(log_date))]
现在我将日期和时间组合到了一个看起来像这样的 datetime.datetime
对象的列表中:
datetime.datetime(2017,6,25,00,31,53,993000)
datetime.datetime(2017,6,25,00,3 2,31,224000)
datetime.datetime(2017,6,25,00,33,11,223000)
datetime.datetime(2017,6,25,00,33,53,876000)
datetime.datetime(2017,6,25,00,34,31,219000)
datetime.datetime(2017,6,25,00,35,12,634000)
我从这里去哪里?
df.timestamp.dt.round( 1s)
函数似乎不起作用吗?
同样在使用 .split()
时,当秒数和分钟数超过59时,我也遇到了问题
使用 for循环
和 str.split()
:
dts = ['2017-06-25 00:31: 53.993',
'2017-06-25 00:32:31.224',
'2017-06-25 00:33:11.223',
'2017-06-25 00:33 :53.876',
'2017-06-25 00:34:31.219',
'2017-06-25 00:35:12.634']
用于dts中的项目:
date = item.split()[0]
h,m,s = [item.split()[1] .split(':')[0],
项。 split()[1] .split(':')[1],
str(round(float(item.split()[1] .split(':')[-1])))]]
打印(日期+''+ h +':'+ m +':'+ s)
2017-06-25 00:31:54
2017-06-25 00:32:31
2017-06-25 00:33:11
2017-06-25 00:33:54
2017-06-25 00: 34:31
2017-06-25 00:35:13
>>
您可以将其转换为函数:
def round_seconds(dts):
结果= []
用于dts中的项目:
日期= item.split()[0]
h,m,s = [item.split()[1] .split(':')[0],
item.split()[1] .split(':')[1],
str(round(float(item.split()[1] .split(':')[-1]))))]
result.append(date +''+ h +': '+ m +':'+ s)
返回结果
测试函数:
dts = ['2017-06-25 00:31:53.993',
'2017- 06-25 00:32:31.224',
'2017-06-25 00:33:11.223',
'2017-06-25 00:33:53.876',
'2017 -06-25 00:34:31.219',
'2017-06-25 00:35:12.634']
从pprint导入pprint
pprint( round_seconds(dts))
['2017-06-25 00:31:54',
'2017-06-25 00:32:31',
'2017 -06-25 00:33:11',
'2017-06-25 00:33:54',
'20 17-06-25 00:34:31’,
‘2017-06-25 00:35:13’]
>>>
由于您似乎正在使用Python 2.7,因此要删除任何结尾的零,您可能需要更改:
str(round(float(item.split()[1] .split(':')[-1])))
至
str(round(float(item .split()[1] .split(':')[-1])))。rstrip('0')。rstrip('。')
我刚刚在 repl.it 中使用Python 2.7尝试了该功能,并且它按预期运行。
I have a large dataset with more than 500 000 date & time stamps that look like this:
date time
2017-06-25 00:31:53.993
2017-06-25 00:32:31.224
2017-06-25 00:33:11.223
2017-06-25 00:33:53.876
2017-06-25 00:34:31.219
2017-06-25 00:35:12.634
How do I round these timestamps off to the nearest second?
My code looks like this:
readcsv = pd.read_csv(filename)
log_date = readcsv.date
log_time = readcsv.time
readcsv['date'] = pd.to_datetime(readcsv['date']).dt.date
readcsv['time'] = pd.to_datetime(readcsv['time']).dt.time
timestamp = [datetime.datetime.combine(log_date[i],log_time[i]) for i in range(len(log_date))]
So now I have combined the dates and times into a list of datetime.datetime
objects that looks like this:
datetime.datetime(2017,6,25,00,31,53,993000)
datetime.datetime(2017,6,25,00,32,31,224000)
datetime.datetime(2017,6,25,00,33,11,223000)
datetime.datetime(2017,6,25,00,33,53,876000)
datetime.datetime(2017,6,25,00,34,31,219000)
datetime.datetime(2017,6,25,00,35,12,634000)
Where do I go from here?
The df.timestamp.dt.round('1s')
function doesn't seem to be working?
Also when using .split()
I was having issues when the seconds and minutes exceeded 59
Many thanks
Using for loop
and str.split()
:
dts = ['2017-06-25 00:31:53.993',
'2017-06-25 00:32:31.224',
'2017-06-25 00:33:11.223',
'2017-06-25 00:33:53.876',
'2017-06-25 00:34:31.219',
'2017-06-25 00:35:12.634']
for item in dts:
date = item.split()[0]
h, m, s = [item.split()[1].split(':')[0],
item.split()[1].split(':')[1],
str(round(float(item.split()[1].split(':')[-1])))]
print(date + ' ' + h + ':' + m + ':' + s)
2017-06-25 00:31:54
2017-06-25 00:32:31
2017-06-25 00:33:11
2017-06-25 00:33:54
2017-06-25 00:34:31
2017-06-25 00:35:13
>>>
You could turn that into a function:
def round_seconds(dts):
result = []
for item in dts:
date = item.split()[0]
h, m, s = [item.split()[1].split(':')[0],
item.split()[1].split(':')[1],
str(round(float(item.split()[1].split(':')[-1])))]
result.append(date + ' ' + h + ':' + m + ':' + s)
return result
Testing the function:
dts = ['2017-06-25 00:31:53.993',
'2017-06-25 00:32:31.224',
'2017-06-25 00:33:11.223',
'2017-06-25 00:33:53.876',
'2017-06-25 00:34:31.219',
'2017-06-25 00:35:12.634']
from pprint import pprint
pprint(round_seconds(dts))
['2017-06-25 00:31:54',
'2017-06-25 00:32:31',
'2017-06-25 00:33:11',
'2017-06-25 00:33:54',
'2017-06-25 00:34:31',
'2017-06-25 00:35:13']
>>>
Since you seem to be using Python 2.7, to drop any trailing zeros, you may need to change:
str(round(float(item.split()[1].split(':')[-1])))
to
str(round(float(item.split()[1].split(':')[-1]))).rstrip('0').rstrip('.')
I've just tried the function with Python 2.7 at repl.it and it ran as expected.
这篇关于将时间舍入到最接近的秒数-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!