Sometimes, we want to add missing dates to a Python Pandas dataframe, we call reindex.
In this article, we’ll look at how to add missing dates to a Python Pandas dataframe, we call reindex..
How to add missing dates to a Python Pandas dataframe?
To add missing dates to a Python Pandas dataframe, we call reindex.
For instance, we write
import pandas as pd
idx = pd.date_range('09-01-2013', '09-30-2013')
s = pd.Series({'09-02-2013': 2,
'09-03-2013': 10,
'09-06-2013': 5,
'09-07-2013': 1})
s.index = pd.DatetimeIndex(s.index)
s = s.reindex(idx, fill_value=0)
to create a series s.
And then we call s.reindex with idx to fill the series s with the date range values.
And any missing values in the series are filled with 0 by setting the fill_value argument to 0.