I am trying to insert missing weekdays into a Pandas time series dataframe. The inserted weekdays must have NaN
values in every data column. When I tried the answers in Insert missing weekdays in pandas dataframe and fill them with NaN, the new rows are filled with 0
instead of NaN
. To illustrate:
import pandas as pd
df = pd.DataFrame({
'date': ['2022-10-06', '2022-10-11'], # Thursday and Tuesday.
'num': [123, 456]
})
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
df = df.resample('B').sum() # Insert Friday and Monday.
However, df
is now:
num
date
2022-10-06 123
2022-10-07 0
2022-10-10 0
2022-10-11 456
Instead of NaN
, I am getting 0
. How do I get NaN
instead? This is what I want:
num
date
2022-10-06 123
2022-10-07 NaN
2022-10-10 NaN
2022-10-11 456
(Pandas version 1.3.2, Python version 3.8.10)