【问题标题】:Efficient way to concatenate and sort seasonal time series?连接和排序季节性时间序列的有效方法?
【发布时间】:2018-08-17 19:04:20
【问题描述】:

我有许多 numpy 数组,每个数组代表一系列季节性数据。频率为年。

  • 第一个数组包含一年中第一周的所有销售额, 从 2014 年到 2018 年。

  • 第二个数组包含一年中第 2 周的所有销售额, 从 2014 年到 2018 年。

  • ...
  • 第 52 个数组包含一年中第 52 周的所有销售额, 从 2014 年到 2018 年。

下面的例子有点棘手,因为它们不是通过计算“一年中的星期”和光滑来严格获得的。相反,它们是根据一周的相对索引到开始时间的 52 的剩余部分来选择的。不过思路差不多……

它们看起来像这样。

arr1 = array([[Timestamp('2014-01-04 00:00:00'), 138.52],
   [Timestamp('2015-01-03 00:00:00'), 25476.179999999997],
   [Timestamp('2016-01-02 00:00:00'), 87318.65000000001],
   [Timestamp('2016-12-31 00:00:00'), 194409.42],
   [Timestamp('2017-12-30 00:00:00'), 299190.06000000023]], dtype=object)

arr2 = array([[Timestamp('2014-01-11 00:00:00'), 0.0],
   [Timestamp('2015-01-10 00:00:00'), 23848.57000000001],
   [Timestamp('2016-01-09 00:00:00'), 86814.12],
   [Timestamp('2017-01-07 00:00:00'), 202016.92],
   [Timestamp('2018-01-06 00:00:00'), 344430.36000000004]], dtype=object)

现在我想将它们加入到一个完整且有序的时间序列中。

  • 所以索引将是[arr1.index[0], arr2.index[0], arr3.index[0], ... arr52.index[0], (here we begin a new year) arr1.index[1], arr2.index[1], ...]
  • 值将是[arr1.value[0], arr2.value[0], arr3.value[0], ... arr52.value[0], (here we begin a new year) arr1.value[1], arr2.value[1], ...]

我能想到的一种方法是首先将所有子系列连接成一个完整系列,然后按索引对系列进行排序。

但是有没有更好的方法来做到这一点?

谢谢!

背景 当我尝试在 Python 中实现stl 分解时出现了问题,其中一个步骤是独立平滑每个季节性子系列,然后将它们组合在一起。

【问题讨论】:

    标签: python pandas numpy time-series


    【解决方案1】:

    我有个主意.. 假设我们所有的子系列都准备好了。

    1. 首先垂直堆叠所有子系列。
    2. 然后循环遍历每一列并将它们一个接一个地连接起来。

    所以由于子系列的结构,我们避免了排序。

    例子:

    >> s1 
    array([Timestamp('2013-12-28 00:00:00'), Timestamp('2014-12-27 00:00:00'),
           Timestamp('2015-12-26 00:00:00'), Timestamp('2016-12-24 00:00:00'),
           Timestamp('2017-12-23 00:00:00')], dtype=object)
    
    >> s2
    array([Timestamp('2014-01-04 00:00:00'), Timestamp('2015-01-03 00:00:00'),
           Timestamp('2016-01-02 00:00:00'), Timestamp('2016-12-31 00:00:00'),
           Timestamp('2017-12-30 00:00:00')], dtype=object)
    
    >> np.vstack([s1, s2]).T.reshape([1, -1], order = 'c')[0]
    array([Timestamp('2013-12-28 00:00:00'), Timestamp('2014-01-04 00:00:00'),
           Timestamp('2014-12-27 00:00:00'), Timestamp('2015-01-03 00:00:00'),
           Timestamp('2015-12-26 00:00:00'), Timestamp('2016-01-02 00:00:00'),
           Timestamp('2016-12-24 00:00:00'), Timestamp('2016-12-31 00:00:00'),
           Timestamp('2017-12-23 00:00:00'), Timestamp('2017-12-30 00:00:00')], dtype=object)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      相关资源
      最近更新 更多