Menu Close

How to add missing dates to Python Pandas DataFrame?

Sometimes, we want to add missing dates to Python Pandas DataFrame.

In this article, we’ll look at how to add missing dates to Python Pandas DataFrame.

How to add missing dates to Python Pandas DataFrame?

To add missing dates to Python Pandas DataFrame, we can use the DatetimeIndex instance’s reindex method.

For instance, we write:

import pandas as pd

idx = pd.date_range('09-01-2020', '09-30-2020')

s = pd.Series({
    '09-02-2020': 2,
    '09-03-2020': 1,
    '09-06-2020': 5,
    '09-07-2020': 1
})
s.index = pd.DatetimeIndex(s.index)

s = s.reindex(idx, fill_value=0)
print(s)

We create a date range index with idx = pd.date_range('09-01-2020', '09-30-2020').

Then we create a series with:

s = pd.Series({
    '09-02-2020': 2,
    '09-03-2020': 1,
    '09-06-2020': 5,
    '09-07-2020': 1
})

We set the index of the series with:

s.index = pd.DatetimeIndex(s.index)

Finally we fill in the missing dates between Sep 1, 2020 to Sep 30, 2020 with:

s = s.reindex(idx, fill_value=0)

Therefore, we see:

2020-09-01    0
2020-09-02    2
2020-09-03    1
2020-09-04    0
2020-09-05    0
2020-09-06    5
2020-09-07    1
2020-09-08    0
2020-09-09    0
2020-09-10    0
2020-09-11    0
2020-09-12    0
2020-09-13    0
2020-09-14    0
2020-09-15    0
2020-09-16    0
2020-09-17    0
2020-09-18    0
2020-09-19    0
2020-09-20    0
2020-09-21    0
2020-09-22    0
2020-09-23    0
2020-09-24    0
2020-09-25    0
2020-09-26    0
2020-09-27    0
2020-09-28    0
2020-09-29    0
2020-09-30    0
Freq: D, dtype: int64

printed.

Conclusion

To add missing dates to Python Pandas DataFrame, we can use the DatetimeIndex instance’s reindex method.

Posted in Python, Python Answers