Make randoms values zero in time series dataset

Viewed 55

I have a time series dataset that contains date and sales for a particular day. I want to make random (also a sequence of consecutive days if possible) sale values 0 to check how the different models react on missing sale values. What's the best way to do this? Thanks in advance!

2 Answers

You can use a random integer generator to get a pre-defined number of indices and the use it to set those rows of the dataframe to zero

import pandas as pd
import numpy as np
from numpy.random import default_rng

df = pd.DataFrame({'date': pd.date_range(pd.Timestamp(2021,1,1,), freq='D', periods=10),
                   'sales': np.random.randint(1, 100, 10)})
df


date    sales
0   2021-01-01  5
1   2021-01-02  88
2   2021-01-03  8
3   2021-01-04  35
4   2021-01-05  45
5   2021-01-06  24
6   2021-01-07  66
7   2021-01-08  1
8   2021-01-09  10
9   2021-01-10  85

rng = default_rng()
zero_idx = rng.choice(len(df.index), size=5, replace=False)
zero_idx

array([6, 5, 0, 7, 1], dtype=int64)

df.loc[zero_idx,'sales'] = 0
df

    date    sales
0   2021-01-01  0
1   2021-01-02  0
2   2021-01-03  8
3   2021-01-04  35
4   2021-01-05  45
5   2021-01-06  0
6   2021-01-07  0
7   2021-01-08  0
8   2021-01-09  10
9   2021-01-10  85

It seems that amount of data in your example is rather small. Hence I propose you also try that kind of approach;

import numpy as np

sale_for_particular_day=[1,2,3,4,1,9,7]

for i in range(len(sale_for_particular_day)):
    #let's create a random value...here using the normal distribution but you can change...
    random_value=np.random.randn(1)
    if random_value>0:
        #Set the value to zero:
        sale_for_particular_day[i]=0

print(sale_for_particular_day)

...where you can easily try different kinds of randomization procedures in a logical way. Easy to extend, easy to create different strategies for changing the input data using different random value thresholds. With that kind of for-loops you can easily implement state-machine type approaches for the analysis.

Related