如何将datetime列结合到最近的四分之一小时 [英] How do I round datetime column to nearest quarter hour

查看:196
本文介绍了如何将datetime列结合到最近的四分之一小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将数据文件加载到Python熊猫数据框中。我有一个日期时间列格式为 2015-07-18 13:53:33.280



我需要做的是创建一个新的列,将其展示到最近的四分之一小时。所以,上述日期将四舍五入为 2015-07-18 13:45:00.000



我在大熊猫这样做?我尝试使用这里,但是得到一个'系列'对象没有属性'年'错误。

解决方案

假设您的系列由 datetime 需要使用 Series.apply 。示例 -

  import datetime 
df ['< column>'] = df ['< column& ] .apply(lambda dt:datetime.datetime(dt.year,dt.month,dt.day,dt.hour,15 *(dt.minute // 15)))


I have loaded a data file into a Python pandas dataframe. I has a datetime column of the format 2015-07-18 13:53:33.280.

What I need to do is create a new column that rounds this out to its nearest quarter hour. So, the date above will be rounded to 2015-07-18 13:45:00.000.

How do I do this in pandas? I tried using the solution from here, but get an 'Series' object has no attribute 'year' error.

解决方案

Assuming that your series is made up of datetime objects, You need to use Series.apply . Example -

import datetime
df['<column>'] = df['<column>'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,15*(dt.minute // 15)))

这篇关于如何将datetime列结合到最近的四分之一小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆