【问题标题】:Resample with Pandas a longer period than original time horizon用 Pandas 重新采样比原始时间范围更长的时间
【发布时间】:2018-11-05 23:07:37
【问题描述】:

我有以下每日定价数据:

2017-06-01  15.00
2017-06-02  20.00

我想将其重新采样为超过 35 小时的每小时价格。因此,前 24 小时在每个样本中的值为 15.00,从 24 小时到 35 小时,价格将为 20.00。

2017-06-01 00:00    15.00
2017-06-01 01:00    15.00
2017-06-01 02:00    15.00
…
2017-06-01 23:00    15.00
2017-06-02 00:00    20.00
2017-06-02 01:00    20.00
2017-06-02 02:00    20.00
…
2017-06-02 10:00    20.00

我尝试使用 resample('3600S').pad() 但它不起作用。是否可以单独创建新数据范围并将其用作重采样功能的输入? resample() 在这里似乎不起作用。

【问题讨论】:

    标签: python pandas sampling


    【解决方案1】:

    您可以按小时频率创建自定义日期范围并重新索引

    df.index = pd.to_datetime(df.index)
    rng=pd.date_range(start=df.index.min(), periods=35, freq='H')
    df.reindex(rng).ffill()
    
                        val
    2017-06-01 00:00:00 15.0
    2017-06-01 01:00:00 15.0
    2017-06-01 02:00:00 15.0
    2017-06-01 03:00:00 15.0
    2017-06-01 04:00:00 15.0
    2017-06-01 05:00:00 15.0
    2017-06-01 06:00:00 15.0
    2017-06-01 07:00:00 15.0
    2017-06-01 08:00:00 15.0
    2017-06-01 09:00:00 15.0
    2017-06-01 10:00:00 15.0
    2017-06-01 11:00:00 15.0
    2017-06-01 12:00:00 15.0
    2017-06-01 13:00:00 15.0
    2017-06-01 14:00:00 15.0
    2017-06-01 15:00:00 15.0
    2017-06-01 16:00:00 15.0
    2017-06-01 17:00:00 15.0
    2017-06-01 18:00:00 15.0
    2017-06-01 19:00:00 15.0
    2017-06-01 20:00:00 15.0
    2017-06-01 21:00:00 15.0
    2017-06-01 22:00:00 15.0
    2017-06-01 23:00:00 15.0
    2017-06-02 00:00:00 20.0
    2017-06-02 01:00:00 20.0
    2017-06-02 02:00:00 20.0
    2017-06-02 03:00:00 20.0
    2017-06-02 04:00:00 20.0
    2017-06-02 05:00:00 20.0
    2017-06-02 06:00:00 20.0
    2017-06-02 07:00:00 20.0
    2017-06-02 08:00:00 20.0
    2017-06-02 09:00:00 20.0
    2017-06-02 10:00:00 20.0
    

    【讨论】:

      【解决方案2】:

      另一种方法是 (a)resample without aggregation,(b) 计算 row-wise hourly difference,然后 (c) 使用 np.whereconditionally set the value column

      样本数据

      d = {'date':['2017-06-01','2017-06-02', '2017-06-03'], 'value':[15,20,30]}
      df = pd.DataFrame.from_dict(d)
      print(df)
      
               date  value
      0  2017-06-01     15
      1  2017-06-02     20
      2  2017-06-03     30
      

      代码

      from numpy import where, timedelta64
      df['date'] = pd.to_datetime(df['date'])
      df = df.set_index('date').asfreq("H").iloc[:35,:]
      # Get time difference in hours, relative to 1st row
      df['hours'] = ((df.index - df.index[0])/timedelta64(1, 'h')).astype(int)
      # Conditionally set 'value' column, using time difference
      df['value'] = where(df['hours']<35, 15, 20)
      print(df)
      

      输出

                           value  hours
      date                             
      2017-06-01 00:00:00     15      0
      2017-06-01 01:00:00     15      1
      2017-06-01 02:00:00     15      2
      2017-06-01 03:00:00     15      3
      2017-06-01 04:00:00     15      4
      2017-06-01 05:00:00     15      5
      2017-06-01 06:00:00     15      6
      2017-06-01 07:00:00     15      7
      2017-06-01 08:00:00     15      8
      2017-06-01 09:00:00     15      9
      2017-06-01 10:00:00     15     10
      2017-06-01 11:00:00     15     11
      2017-06-01 12:00:00     15     12
      2017-06-01 13:00:00     15     13
      2017-06-01 14:00:00     15     14
      2017-06-01 15:00:00     15     15
      2017-06-01 16:00:00     15     16
      2017-06-01 17:00:00     15     17
      2017-06-01 18:00:00     15     18
      2017-06-01 19:00:00     15     19
      2017-06-01 20:00:00     15     20
      2017-06-01 21:00:00     15     21
      2017-06-01 22:00:00     15     22
      2017-06-01 23:00:00     15     23
      2017-06-02 00:00:00     15     24
      2017-06-02 01:00:00     15     25
      2017-06-02 02:00:00     15     26
      2017-06-02 03:00:00     15     27
      2017-06-02 04:00:00     15     28
      2017-06-02 05:00:00     15     29
      2017-06-02 06:00:00     15     30
      2017-06-02 07:00:00     15     31
      2017-06-02 08:00:00     15     32
      2017-06-02 09:00:00     15     33
      2017-06-02 10:00:00     15     34
      2017-06-02 11:00:00     20     35
      

      编辑

      代替

      df = df.set_index('date').asfreq("H").iloc[:35,:]
      

      你也可以使用

      df = df.set_index('date').asfreq("H")
      df = df.loc[pd.date_range(start=df.index[0], periods=35, freq='H'),['value']]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-28
        • 1970-01-01
        • 1970-01-01
        • 2015-11-07
        • 2014-07-24
        • 2019-01-18
        • 2018-02-05
        • 1970-01-01
        相关资源
        最近更新 更多