def test_ensure_unique_column(): df = pd.DataFrame({"a": [0, 1, 0], "b": [1.0, 0.0, 1.0]}) out = ensure_unique_column("a", df) assert out == "_a_" out = ensure_unique_column("c", df) assert out == "c" out = ensure_unique_column("a", df, "=") assert out == "=a=" df["_a_"] = -1 out = ensure_unique_column("a", df) assert out == "__a__"
def _minimize_multiindex(df: DataFrame) -> DataFrame: index_cols = list(df.index.names) orig_names = index_cols[:] for i, col in enumerate(index_cols): col = ensure_unique_column(col, df) index_cols[i] = col df.index.names = index_cols df = df.reset_index() df = df.set_index(index_cols) df.index.names = orig_names return df