Pandas - 将时间戳四舍五入到最接近的秒数
Posted
技术标签:
【中文标题】Pandas - 将时间戳四舍五入到最接近的秒数【英文标题】:Pandas - Rounding off timestamps to the nearest second 【发布时间】:2018-06-03 18:45:37 【问题描述】:我正在努力使用 pandas 来四舍五入时间戳。
时间戳如下所示:
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)
如何四舍五入到最接近的秒数?
之前 iv 在这篇文章中尝试了一些建议,但没有奏效: Rounding time off to the nearest second - Python
到目前为止,我的代码如下所示:
import pandas as pd
filename = 'data.csv'
readcsv = pd.read_csv(filename)
根据文件头信息导入数据
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
将日期和时间合并到一个变量中
timestamp = [datetime.datetime.combine(log_date[i],log_time[i]) for i in range(len(log_date))]
创建数据框
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']
我对python还是很陌生,所以请原谅我的无知
【问题讨论】:
如果准确性不是太重要,您可以将毫秒设置为 000 【参考方案1】: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')
为您提供以下内容:
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]
【讨论】:
非常感谢,我必须升级我的包才能看到功能。【参考方案2】:您可以首先使用read_csv
和参数parse_dates
从列date
和time
创建datetime
s,然后使用dt.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
编辑:
如果您还想将日期时间列设置为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
【讨论】:
我收到“AttributeError: 'DatetimeProperties' 对象没有属性 'round'” 2 个问题 -print (df.dtypes)
是什么?你的熊猫版本是什么print (pd.show_versions())
?
df.types
与您拥有的完全相同,并且 Iv got pandas: 0.17.1
嗯,有点老了,可以升级吗?
因为这个功能是在pandas 0.18
版本中实现的(检查this),而pandas的最后一个版本是0.21.1
以上是关于Pandas - 将时间戳四舍五入到最接近的秒数的主要内容,如果未能解决你的问题,请参考以下文章
在 PySpark 中,如何将时间戳值四舍五入到最接近的分钟?
Bash:以人类可读格式将 unix 时间四舍五入到最接近的分钟