Pandas - 将时间戳四舍五入到最接近的秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47919045/
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:56:15  来源:igfitidea点击:

Pandas - Rounding off timestamps to the nearest second

pythonpython-2.7pandasdatetime

提问by Jetman

I am struggling to round off timestamps using pandas.

我正在努力使用Pandas来完善时间戳。

The timestamps look like this:

时间戳如下所示:

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)

How do I round off to the nearest second?

如何四舍五入到最接近的秒?

Previously iv tried suggestions in this post but they didn't work: Rounding time off to the nearest second - Python

以前 iv 尝试过这篇文章中的建议,但没有奏效: 四舍五入到最接近的秒数 - Python

My code looks like this so far:

到目前为止,我的代码如下所示:

import pandas as pd
filename = 'data.csv'
readcsv = pd.read_csv(filename)

Import data according to file header info

根据文件头信息导入数据

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

Combine date and time into one variable

将日期和时间合并为一个变量

timestamp = [datetime.datetime.combine(log_date[i],log_time[i]) for i in range(len(log_date))]

Create dataframe

创建数据框

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']

I am still quite new to python so please excuse my ignorance

我对 python 还是很陌生,所以请原谅我的无知

采纳答案by jezrael

You can use first read_csvwith parameter parse_datesfor create datetimes from column dateand timeand then dt.roundfor round datetimes:

你可以先使用read_csv带有参数parse_dates的创造datetime从列Sdatetimedt.rounddatetimeS:

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 indexalso:

如果您还想将带有日期时间的列设置为index

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

回答by FatihAkici

dt.round is what you are looking for. I'll just create a smaller version of your DataFrame, please comment if you can't modify it to fully fit your case, I can help with that also.

dt.round 就是你要找的。我只会创建一个较小版本的 DataFrame,如果您无法修改它以完全适合您的情况,请发表评论,我也可以提供帮助。

import datetime
import pandas as pd

ts1 = datetime.datetime(2017,06,25,00,31,53,993000)
ts2 = datetime.datetime(2017,06,25,00,32,31,224000)
ts3 = datetime.datetime(2017,06,25,00,33,11,223000)
df = pd.DataFrame({'timestamp':[ts1, ts2, ts3]})

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

Gives you the following:

为您提供以下内容:

Out[89]: 
0   2017-06-25 00:31:54
1   2017-06-25 00:32:31
2   2017-06-25 00:33:11
Name: timestamp, dtype: datetime64[ns]