I have a dataframe like this:
A B value
2014-11-14 12:00:00 30.5 356.3 344
2014-11-15 00:00:00 30.5 356.3 347
2014-11-15 12:00:00 30.5 356.3 356
2014-11-16 00:00:00 30.5 356.3 349
...
2017-01-06 00:00:00 30.5 356.3 347
and I want to be sure that from the beginning until the end there're no missing times (i.e., the index goes from 12 to 12 hours with no bigger jumps). If there is a missing date, for instance, if there is a value missing, for instance in 2015-12-12 12:00:00 I would like to add a row like this:
...
2015-12-12 00:00:00 30.5 356.3 323
2015-12-12 12:00:00 30.5 356.3 NaN *<- add this*
2015-12-13 00:00:00 30.5 356.3 347
The question of how to do it was solved here Resampling dataframe in pandas as a checking operation by @ted-petrou. The solution was doing:
df1= df.asfreq('12H')
df1[['A','B']] = df1[['A','B']].fillna(method='ffill')
My question: Can I do it with resample
instead of asfreq
? Doing
df1= df.resample('12H')
df1[['A','B']] = df1[['A','B']].fillna(method='ffill')
I get ValueError: cannot set items on DatetimeIndexResampler
. I don't understand why. Are not the same operations resample
and asfreq
for this particular case? What am I missing? Thank you in advance.