pandas 时间戳不规则地四舍五入30秒

编程入门 行业动态 更新时间:2024-10-24 18:23:07
本文介绍了 pandas 时间戳不规则地四舍五入30秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将熊猫的DatetimeIndex(或Timestamp)四舍五入到最近的分钟,但是Timestamps为30秒时我遇到了问题-有些向上舍入,有些向下舍入(这似乎是交替的).

有什么建议可以解决这个问题,以使30s总是四舍五入?

>>> pd.Timestamp(2019,6,1,6,57,30).round('1T') Timestamp('2019-06-01 06:58:00') >>> pd.Timestamp(2019,6,1,6,58,30).round('1T') Timestamp('2019-06-01 06:58:00')

最好的结果看起来不错,有57m个30s取整为58m,但是我希望最下面的结果可以取整到59m-不小于58m.

解决方案

四舍五入是一致的;接下来的选择是当在两个整数之间的中间时,选择了偶数整数".您需要上半舍入,这将需要您自己实现.

import numpy as np import pandas as pd def half_up_minute(x): m = (x - x.dt.floor('1T')).dt.total_seconds() < 30 # Round True Down, False Up return x.where(m).dt.floor('1T').fillna(x.dt.ceil('1T')) # For indices: def half_up_minute_idx(idx): m = (idx - idx.floor('1T')).total_seconds() < 30 # Round True Down, False Up return pd.Index(np.select([m], [idx.floor('1T')], default=idx.ceil('1T'))) # Sample Data df = pd.DataFrame({'date': pd.date_range('2019-01-01', freq='15S', periods=10)}) df['rounded'] = half_up_minute(df.date)

输出:

date rounded 0 2019-01-01 00:00:00 2019-01-01 00:00:00 1 2019-01-01 00:00:15 2019-01-01 00:00:00 2 2019-01-01 00:00:30 2019-01-01 00:01:00 3 2019-01-01 00:00:45 2019-01-01 00:01:00 4 2019-01-01 00:01:00 2019-01-01 00:01:00 5 2019-01-01 00:01:15 2019-01-01 00:01:00 6 2019-01-01 00:01:30 2019-01-01 00:02:00 7 2019-01-01 00:01:45 2019-01-01 00:02:00 8 2019-01-01 00:02:00 2019-01-01 00:02:00 9 2019-01-01 00:02:15 2019-01-01 00:02:00

I'm trying to round a pandas DatetimeIndex (or Timestamp) to the nearest minute, but I'm having a problem with Timestamps of 30 seconds - some rounding up, some rounding down (this seems to alternate).

Any suggestions to fix this so that 30s always rounds up?

>>> pd.Timestamp(2019,6,1,6,57,30).round('1T') Timestamp('2019-06-01 06:58:00') >>> pd.Timestamp(2019,6,1,6,58,30).round('1T') Timestamp('2019-06-01 06:58:00')

The top result looks fine, with 57m 30s rounding up to 58m, but I'd expect the bottom result to round up to 59m - not down to 58m.

解决方案

The rounding is consistent; the choice followed is, "when halfway between two integers the even integer is chosen." You want half-up rounding, which you will need to implement yourself.

import numpy as np import pandas as pd def half_up_minute(x): m = (x - x.dt.floor('1T')).dt.total_seconds() < 30 # Round True Down, False Up return x.where(m).dt.floor('1T').fillna(x.dt.ceil('1T')) # For indices: def half_up_minute_idx(idx): m = (idx - idx.floor('1T')).total_seconds() < 30 # Round True Down, False Up return pd.Index(np.select([m], [idx.floor('1T')], default=idx.ceil('1T'))) # Sample Data df = pd.DataFrame({'date': pd.date_range('2019-01-01', freq='15S', periods=10)}) df['rounded'] = half_up_minute(df.date)

Output:

date rounded 0 2019-01-01 00:00:00 2019-01-01 00:00:00 1 2019-01-01 00:00:15 2019-01-01 00:00:00 2 2019-01-01 00:00:30 2019-01-01 00:01:00 3 2019-01-01 00:00:45 2019-01-01 00:01:00 4 2019-01-01 00:01:00 2019-01-01 00:01:00 5 2019-01-01 00:01:15 2019-01-01 00:01:00 6 2019-01-01 00:01:30 2019-01-01 00:02:00 7 2019-01-01 00:01:45 2019-01-01 00:02:00 8 2019-01-01 00:02:00 2019-01-01 00:02:00 9 2019-01-01 00:02:15 2019-01-01 00:02:00

更多推荐

pandas 时间戳不规则地四舍五入30秒

本文发布于:2023-10-25 14:15:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不规则   四舍五入   时间   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!