I am attempting to do something similar to the reindexing in Pandas reindex date index by group revisited
except I have one extra level of indexing involved. My data is in the following structure: [Grid Cell, Site, Date, Value], and I want to reindex all sites within a particular grid cell to the same datetimeindex.
import pandas as pd
import numpy as np
data_dict = {'Grid Cell':[1,1,1,1,1,1,1,2,2,2,2,2,2,2],'Site':['A','A','A','A','B','B','B','C','C','C','D','D','D','D'],'Date':['1999-01-01','1999-02-01','1999-03-01','1999-04-01','1999-01-01','1999-02-01','1999-03-01','2000-01-01','2000-02-01','2000-03-01','2000-01-01','2000-02-01','2000-03-01','2000-04-01'],'Value':[-2.45,-3.72,1.34,4.56,0.23,3.26,6.76,-7.45,-6.43,-2.18,-10.72,-8.97,-5.32,-1.73]}
df = pd.DataFrame.from_dict(data_dict)
unique_dates = df.groupby('Grid Cell')['Date'].unique()
df = df.set_index('Date')
idx = pd.MultiIndex.from_product([df.index.unique(), df['Grid Cell'].unique()],names=['Date','Grid Cell'])
new_df = df.set_index("Grid Cell", append=True).reindex(idx, fill_value=np.nan).reset_index(level=1)
I am attempting to achieve a similar result to the linked example, such that all sites in Grid Cell 1, would be reindexed to the following datetimeindex: '1999-01-01', '1999-02-01', '1999-03-01', '1999-04-01', and all sites in Grid Cell 2 would be reindexed to: '2000-01-01', '2000-02-01', '2000-03-01', '2000-04-01', and the resultant dataframe would look like:
data_dict_out = {'Grid Cell':[1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2],'Site':['A','A','A','A','B','B','B','B','C','C','C','D','D','D','D'],'Date':['1999-01-01','1999-02-01','1999-03-01','1999-04-01','1999-01-01','1999-02-01','1999-03-01','1999-04-01','2000-01-01','2000-02-01','2000-03-01','2000-04-01','2000-01-01','2000-02-01','2000-03-01','2000-04-01'],'Value':[-2.45,-3.72,1.34,4.56,0.23,3.26,6.76,np.nan,-7.45,-6.43,-2.18,np.nan,-10.72,-8.97,-5.32,-1.73]}
df_out = pd.DataFrame.from_dict(data_dict_out)
Unfortunately my partial solution currently produces a "ValueError: cannot handle a non-unique multi-index!" error. Any suggestions on what I need to change here?
IIUC, you can do:
Prints: