You can create a new multi-index based on the Cartesian product of the required index levels. Then reindex your data frame with the new index.
(date_index, category_index) = df.index.levels new_index = pd.MultiIndex.from_product([all_dates, category_index]) new_df = df.reindex(new_index)
What is it! The new data frame has all possible index values. Existing data is indexed correctly.
Read on for a more detailed explanation.
Description
Sample Data Setup
import datetime as dt import pandas as pd days= 4
Here, that sample data looks like
value date category 2013-02-10 1 4 2 7 2013-02-11 2 7 2013-02-13 1 2 2 3
Create a new index
Using from_product , we can create a new multi-index. This new index is the Cartesian product of all the values ββthat you pass to the function.
(date_index, category_index) = df.index.levels new_index = pd.MultiIndex.from_product([all_dates, category_index])
Reindex
Use the new index to override the existing data frame.
Now there are all possible combinations. Invalid values: null (NaN).
new_df = df.reindex(new_index)
Now an extended, reindexed data frame looks like this:
value 2013-02-13 1 2.0 2 3.0 2013-02-12 1 NaN 2 NaN 2013-02-11 1 NaN 2 7.0 2013-02-10 1 4.0 2 7.0
Zeros in an integer column
You can see that the data in the new data frame has been converted from ints to float. Pandas cannot have zeros in an integer column . If desired, we can convert all zeros to 0 and return the data back to integers.
new_df = new_df.fillna(0).astype(int)
Result
value 2013-02-13 1 2 2 3 2013-02-12 1 0 2 0 2013-02-11 1 0 2 7 2013-02-10 1 4 2 7