From 6eb5486ce4f8b513fe7dee4a27e41350a87ea924 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Thu, 25 Jan 2024 15:52:14 +0000 Subject: [PATCH] Error on shared column names when writing --- anndata/_io/specs/methods.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/anndata/_io/specs/methods.py b/anndata/_io/specs/methods.py index fde9c0f34..70bd36945 100644 --- a/anndata/_io/specs/methods.py +++ b/anndata/_io/specs/methods.py @@ -663,10 +663,23 @@ def write_dataframe(f, key, df, _writer, dataset_kwargs=MappingProxyType({})): if reserved in df.columns: raise ValueError(f"{reserved!r} is a reserved name for dataframe columns.") group = f.require_group(key) + if not df.columns.is_unique: + duplicates = list(df.columns[df.columns.duplicated()]) + raise ValueError( + f"Found repeated column names: {duplicates}. Column names must be unique." + ) col_names = [check_key(c) for c in df.columns] group.attrs["column-order"] = col_names if df.index.name is not None: + if df.index.name in col_names and not pd.Series( + df.index, index=df.index + ).equals(df[df.index.name]): + raise ValueError( + f"DataFrame.index.name ({df.index.name!r}) is also used by a column " + "whose values are different. This is not supported. Please make sure " + "the values are the same, or use a different name." + ) index_name = df.index.name else: index_name = "_index"