【问题标题】:Mask timeseries outside business hours工作时间以外的掩码时间序列
【发布时间】:2013-05-04 16:14:27
【问题描述】:

您好,如果时间序列索引在工作时间之外,我想屏蔽一个每小时时间序列。

我可以实现我想要的工作日数据,但不能实现每小时数据

import datetime
import pandas as pd
import numpy as np
from pandas.tseries.offsets import *

st = datetime.datetime(2013, 1, 1)
ed = datetime.datetime(2013, 2, 1)
myrange = pd.date_range(st, ed, freq='H')
ts = pd.Series(np.random.randn(len(myrange)), index=myrange)
ts.asfreq(BDay()).asfreq(Day())

我尝试生成 BDay 日期范围,然后将频率更改为每小时,但这不起作用。

newrange = pd.date_range(datetime.datetime(2013, 1, 1), datetime.datetime(2013, 1, 1), freq='B') 
#but adding this doesn't work .asfreq(Hour())
ts[ts.index.isin(newrange)].asfreq(Hour()) #Of course this only gives one value on the day

谢谢

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    要将您的时间限制为工作日,您可以使用:

    ts = ts.ix[ts.index.map(BDay())]
    

    indexer_between_time 限制营业时间:

    ts = ts.ix[ts.index.indexer_between_time(time(7), time(18))]
    

    限制在工作时间内的工作日(以任一顺序应用这些)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      相关资源
      最近更新 更多