def drop_nan_levels(df):
    """Drops all-nan levels in MultiIndexed DataFrame.

    Args:
        df (:class:`pandas.DataFrame`): DataFrame from which to drop all-nan levels.
    Returns:
        :class:`pandas.DataFrame`: DataFrame with all-nan levels dropped.
    """
    try: # df.columns is a MultiIndex
        for name, code in zip(df.columns.names, df.columns.codes):
            if all(c == -1 for c in code): # code == -1 means index is NaN
                df = df.droplevel(name, axis=1)
    except AttributeError: #df.columns is an Index
        pass
    return df