【问题标题】:Round the time up to the nearest hour pandas Series将时间四舍五入到最接近的小时 pandas 系列
【发布时间】:2020-03-16 02:52:31
【问题描述】:

尝试将观察到的时间四舍五入到最接近的小时

Example Data: 
observed_time = ['2020-02-20T17:54:00Z', '2020-02-20T18:54:00Z']

slice_begin_time=['2020-02-20T17:50:00Z', '2020-02-20T18:50:00Z', '2020-02-20T19:50:00Z', '2020-02-20T20:50:00Z', '2020-02-20T21:50:00Z']
slice_end_time=['2020-02-20T18:05:00Z', '2020-02-20T19:05:00Z', '2020-02-20T20:05:00Z', '2020-02-20T21:05:00Z', '2020-02-20T22:05:00Z']

### LIBS
from datetime import datetime, timedelta
import pandas as pd

s = pd.Series(pd.to_datetime(observed_time))
for i in range(len(slice_begin_time)):
    s[s.between(pd.Timestamp(slice_begin_time[i]),pd.Timestamp(slice_end_time[i]))] == s.round(freq = 'H')
print(s)

我收到错误 Error: round() got an unexpected keyword argument 'freq'

我确实想要一个系列。

【问题讨论】:

  • 我想我可能会使用 rolling() 或 resample() 不确定
  • 我猜你需要改用.dt.round('1H')

标签: python pandas time


【解决方案1】:

希望这会有所帮助:

observed_time = ['2020-02-20T17:54:00Z', '2020-02-20T18:54:00Z']

slice_begin_time=['2020-02-20T17:50:00Z', '2020-02-20T18:50:00Z', '2020-02-20T19:50:00Z', '2020-02-20T20:50:00Z', '2020-02-20T21:50:00Z']
slice_end_time=['2020-02-20T18:05:00Z', '2020-02-20T19:05:00Z', '2020-02-20T20:05:00Z', '2020-02-20T21:05:00Z', '2020-02-20T22:05:00Z']

def match(time):
    for i in range(len(slice_begin_time)):
        if pd.Timestamp(slice_begin_time[i]) <= time <= pd.Timestamp(slice_end_time[i]):
            return time.round('1H')
            break
    return time  # returns the input time in case nothing matches

s = pd.Series(pd.to_datetime(observed_time))
# apply map function match to the series elements    
s = s.map(match)
print(s)

输出:

0   2020-02-20 18:00:00+00:00
1   2020-02-20 19:00:00+00:00
dtype: datetime64[ns, UTC]

如果您只是想将时间序列转换为最近的时间,您可以使用:

s = s.dt.round('1H')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2013-05-02
    相关资源
    最近更新 更多