pandas 将时间舍入到最接近的秒数 - Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47792242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:54:16  来源:igfitidea点击:

Rounding time off to the nearest second - Python

pythonpython-2.7pandasdatetime

提问by Jetman

I have a large dataset with more than 500 000 date & time stamps that look like this:

我有一个包含超过 500 000 个日期和时间戳的大型数据集,如下所示:

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.datetimeobjects that looks like this:

所以现在我将日期和时间组合成一个datetime.datetime对象列表,如下所示:

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

我从这里去哪里?该df.timestamp.dt.round('1s')功能似乎不起作用?此外,.split()当秒和分钟超过 59 时,我遇到了问题

Many thanks

非常感谢

采纳答案by Srisaila

Using for loopand str.split():

使用for loopstr.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:

由于您似乎在使用 Python 2.7,要删除任何尾随零,您可能需要更改:

str(round(float(item.split()[1].split(':')[-1])))

str(round(float(item.split()[1].split(':')[-1])))

to

str(round(float(item.split()[1].split(':')[-1]))).rstrip('0').rstrip('.')

str(round(float(item.split()[1].split(':')[-1]))).rstrip('0').rstrip('.')

I've just tried the function with Python 2.7 at repl.itand it ran as expected.

我刚刚在repl.it 上使用 Python 2.7 尝试了该函数,它按预期运行。

回答by electrovir

Without any extra packages, a datetime object can be rounded to the nearest second with the following simple function:

没有任何额外的包,日期时间对象可以使用以下简单函数四舍五入到最接近的秒:

import datetime

def roundSeconds(dateTimeObject):
    newDateTime = dateTimeObject

    if newDateTime.microsecond >= 500000:
        newDateTime = newDateTime + datetime.timedelta(seconds=1)

    return newDateTime.replace(microsecond=0)

回答by cs95

If you're using pandas, you can just roundthe data to the nearest second using dt.round-

如果您使用的是Pandas,则可以使用以下命令round将数据精确到最近的秒数dt.round-

df

                timestamp
0 2017-06-25 00:31:53.993
1 2017-06-25 00:32:31.224
2 2017-06-25 00:33:11.223
3 2017-06-25 00:33:53.876
4 2017-06-25 00:34:31.219
5 2017-06-25 00:35:12.634

df.timestamp.dt.round('1s')

0   2017-06-25 00:31:54
1   2017-06-25 00:32:31
2   2017-06-25 00:33:11
3   2017-06-25 00:33:54
4   2017-06-25 00:34:31
5   2017-06-25 00:35:13
Name: timestamp, dtype: datetime64[ns]

If timestampisn't a datetimecolumn, convert it first, using pd.to_datetime-

如果timestamp不是datetime列,请先转换它,使用pd.to_datetime-

df.timestamp = pd.to_datetime(df.timestamp)

Then, dt.roundshould work.

那么,dt.round应该工作。

回答by mike rodent

The question doesn't say howyou want to round. Rounding down would often be appropriate for a time function. This is not statistics.

这个问题没有说明你想如何舍入。向下舍入通常适用于时间函数。这不是统计。

rounded_down_datetime = raw_datetime.replace(microsecond=0) 

回答by Maciej Gumu?ka

If anyone wants to round a single datetime item off to the nearest second, this one works just fine:

如果有人想将单个日期时间项舍入到最近的秒数,那么这个就可以了:

pandas.to_datetime(your_datetime_item).round('1s')

回答by Rudradev Pal

If you are storing dataset into a file you can do like this:

如果您要将数据集存储到文件中,您可以这样做:

with open('../dataset.txt') as fp:
    line = fp.readline()
    cnt = 1
    while line:
        line = fp.readline()
        print "\n" + line.strip()
        sec = line[line.rfind(':') + 1:len(line)]
        rounded_num = int(round(float(sec)))
        print line[0:line.rfind(':') + 1] + str(rounded_num)
        print abs(float(sec) - rounded_num)
        cnt += 1

If you are storing dataset in a list:

如果您将数据集存储在列表中:

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 i in dts:
    line = i
    print "\n" + line.strip()
    sec = line[line.rfind(':') + 1:len(line)]
    rounded_num = int(round(float(sec)))
    print line[0:line.rfind(':') + 1] + str(rounded_num)
    print abs(float(sec) - rounded_num)

回答by Gerardsson

An elegant solution that only requires the standard datetime module.

一个优雅的解决方案,只需要标准的 datetime 模块。

import datetime

            currentimemili = datetime.datetime.now()
            currenttimesecs = currentimemili - \
                datetime.timedelta(microseconds=currentimemili.microsecond)
            print(currenttimesecs)

回答by gerardw

Alternate version of @electrovir 's solution:

@electrovir 解决方案的替代版本:

import datetime

def roundSeconds(dateTimeObject):
    newDateTime = dateTimeObject + datetime.timedelta(seconds=.5)
    return newDateTime.replace(microsecond=0)