def update_dataframe(df1, df2):
    """Update df1 with the contents of df2

    This operation:

    - returns df1 if df2 is None,
    - adds new columns in df1 (initialized to 0), if columns in
      df2 exist that are not already in df1,
    - overwrites rows in df1 with rows from df2, or adds rows
      in df1 from df2 if the year-month index doesn't exist
    """
    if df2 is None:
        return df1

    for col in df2:
        if col not in df1:
            df1[col] = 0

    for row in df2.itertuples():
        idx = row.Index
        vals = {}
        for k, v in row._asdict().items():
            if k == 'Index':
                continue
            if k[0] == '_':
                k = df2.columns[int(k[1:])-1]
            vals[k] = v
        df1.loc[idx] = vals