有什么建议可以解决这个问题,以使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 npimport pandas as pddef 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 Datadf = pd.DataFrame({'date': pd.date_range('2019-01-01', freq='15S', periods=10)})df['rounded'] = half_up_minute(df.date)输出: date rounded0 2019-01-01 00:00:00 2019-01-01 00:00:001 2019-01-01 00:00:15 2019-01-01 00:00:002 2019-01-01 00:00:30 2019-01-01 00:01:003 2019-01-01 00:00:45 2019-01-01 00:01:004 2019-01-01 00:01:00 2019-01-01 00:01:005 2019-01-01 00:01:15 2019-01-01 00:01:006 2019-01-01 00:01:30 2019-01-01 00:02:007 2019-01-01 00:01:45 2019-01-01 00:02:008 2019-01-01 00:02:00 2019-01-01 00:02:009 2019-01-01 00:02:15 2019-01-01 00:02:00I'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 npimport pandas as pddef 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 Datadf = pd.DataFrame({'date': pd.date_range('2019-01-01', freq='15S', periods=10)})df['rounded'] = half_up_minute(df.date)Output: date rounded0 2019-01-01 00:00:00 2019-01-01 00:00:001 2019-01-01 00:00:15 2019-01-01 00:00:002 2019-01-01 00:00:30 2019-01-01 00:01:003 2019-01-01 00:00:45 2019-01-01 00:01:004 2019-01-01 00:01:00 2019-01-01 00:01:005 2019-01-01 00:01:15 2019-01-01 00:01:006 2019-01-01 00:01:30 2019-01-01 00:02:007 2019-01-01 00:01:45 2019-01-01 00:02:008 2019-01-01 00:02:00 2019-01-01 00:02:009 2019-01-01 00:02:15 2019-01-01 00:02:00 这篇关于 pandas 时间戳不规则地四舍五入30秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 16:09