Skip to content Skip to sidebar Skip to footer

Time Series Normalization By Event

Suppose I've a python dict as follows, for each product, key is timestamp and value is price of product at that timestamp. data_dict = { 'product_1' : {1: 415, 2: 550, 3: 0, 4: 5

Solution 1:

You may use .apply method, but that tends to be in-efficient if you have many columns;

So starting with this frame:

>>> df
   product_1  product_2  product_3  product_4
1        415        400        500          0
2        550        300        400        200
3          0        300          0        200
4        550          0        500        300
5        600        300        500        300

you define a synchronizing function as in:

>>> def sync(ts):
...     vals = ts.values
...     n, k = len(vals), np.where(vals == 0)[0][0]
...     return Series(vals, np.arange(-k, n - k))

and apply it column-wise:

>>> df.apply(sync).T
            -3   -2   -1   0    1    2    3    4
product_1  NaN  415  550   0  550  600  NaN  NaN
product_2  400  300  300   0  300  NaN  NaN  NaN
product_3  NaN  500  400   0  500  500  NaN  NaN
product_4  NaN  NaN  NaN   0  200  200  300  300

.T at the end for transpose.


Post a Comment for "Time Series Normalization By Event"