So I have a dataframe like so:
[5232 rows x 2 columns]
0 2
0
2018-02-01 00:00:00 2018-02-01 00:00:00 435.24
2018-02-01 00:30:00 2018-02-01 00:30:00 357.12
2018-02-01 01:00:00 2018-02-01 01:00:00 301.32
2018-02-01 01:30:00 2018-02-01 01:30:00 256.68
2018-02-01 02:00:00 2018-02-01 02:00:00 245.52
2018-02-01 02:30:00 2018-02-01 02:30:00 223.20
2018-02-01 03:00:00 2018-02-01 03:00:00 212.04
2018-02-01 03:30:00 2018-02-01 03:30:00 212.04
2018-02-01 04:00:00 2018-02-01 04:00:00 212.04
2018-02-01 04:30:00 2018-02-01 04:30:00 212.04
2018-02-01 05:00:00 2018-02-01 05:00:00 223.20
2018-02-01 05:30:00 2018-02-01 05:30:00 234.36
And what I can currently do is replace a portion of values (say 10% at random with NaN:
df_missing.loc[df_missing.sample(frac=0.1, random_state=100).index, 2] = np.NaN
What I'd like to be able to do, is do the same thing, but with random blocks of size x, say 10% of the data should be blocked NaN.
For example, if the block size was 4, and and the proportion was 30%, the above dataframe might look like:
[5232 rows x 2 columns]
0 2
0
2018-02-01 00:00:00 2018-02-01 00:00:00 435.24
2018-02-01 00:30:00 2018-02-01 00:30:00 357.12
2018-02-01 01:00:00 2018-02-01 01:00:00 NaN
2018-02-01 01:30:00 2018-02-01 01:30:00 NaN
2018-02-01 02:00:00 2018-02-01 02:00:00 NaN
2018-02-01 02:30:00 2018-02-01 02:30:00 NaN
2018-02-01 03:00:00 2018-02-01 03:00:00 212.04
2018-02-01 03:30:00 2018-02-01 03:30:00 212.04
2018-02-01 04:00:00 2018-02-01 04:00:00 212.04
2018-02-01 04:30:00 2018-02-01 04:30:00 212.04
2018-02-01 05:00:00 2018-02-01 05:00:00 223.20
2018-02-01 05:30:00 2018-02-01 05:30:00 234.36
I've figured out I can get the number of blocks with:
number_of_samples = int((df.shape[0] * proporition) / block_size)
But I can't figure out how to actually create the missing blocks.
I've seen this question, which is helpful but it has two caveats:
- It doesn't modify the original dataframe with NaN values, just returns samples.
- There's no guarantee the samples won't overlap (which I'd ideally like to avoid)
Could someone explain how to convert that answer for those above points (or explain a different solution)?
