Tuesday, May 28, 2019

Find common timespan/ years in multiple time series/ dataframes


#concatenate vertically all dataframes
dfAlldf=pd.concat([df1, df2,df3,df4], axis=1)     

#sort them by the date (datetimeindex)
dfAlldf=dfAlldf.sort_index()    

#group dataframe by years
grps=dfAlldf.groupby(dfAlldf.index.year)           

#empty dataframe for populating with complete years
dfCompl=pd.DataFrame()

# for each group of years                                
for g in grps:
    #if don't have any null values in year, in any column 
    if not any(g[1].isnull().any(axis=1)):           
        #concatenate in dfCompl
        dfCompl=pd.concat([dfCompl, g[1]], axis=0)     

# re-sort by index
dfCompl=dfCompl.sort_index()