使用 pandas 舍入DateTime [英] Round DateTime using Pandas

查看:45
本文介绍了使用 pandas 舍入DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对DateTime功能进行四舍五入,以便将其转换为每10分钟时间间隔的起始值.下面的示例显示了所需的结果:

I want to round a DateTime feature such a way that it gets converted to the start value of every 10 minute time interval. Example below shows the desired result:

     Actual Time              |        Round Time(within 10 min interval)


  2016-01-01 05:47:46                      2016-01-01 05:40:00
  2016-01-01 05:49:58                      2016-01-01 05:40:00
  2016-01-01 05:50:01                      2016-01-01 05:50:00
  2016-01-01 05:59:58                      2016-01-01 05:50:00  

下面的代码将其四舍五入到最接近的10分钟间隔,而不是始终给出下限值.

Code below, rounds it to nearest 10 minute interval instead of always giving the floor value.

df['DateTime'].agg(lambda x : x.round('10min'))

它将日期时间特征"四舍五入到最接近的值.

It rounds the DateTime Feature to the nearest value.

   Actual Time              |        Round Time(within 10 min interval)

  2016-01-01 05:47:46                      2016-01-01 05:50:00
  2016-01-01 05:49:58                      2016-01-01 05:50:00
  2016-01-01 05:50:01                      2016-01-01 05:50:00
  2016-01-01 05:59:58                      2016-01-01 06:00:00  

整车时间接近底值.

推荐答案

使用 查看全文

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