cellannotation / cap-anndata

BSD 3-Clause "New" or "Revised" License
4 stars 0 forks source link

Assignment destination is read-only during `rename_column` #32

Open siberianisaev opened 2 weeks ago

siberianisaev commented 2 weeks ago

Create anndata manually:

labelset = "LABELSET"
label = "--".join([labelset, "LABEL"])

cap_adata = CapAnnData(None)
n_rows = 3
X = np.random.rand(n_rows, 2)

cap_adata._X = X
df = pd.DataFrame({labelset: ["name1"]*n_rows, label: ["name2"]*n_rows})
df.column_order = np.array([labelset, label]) # order.setflags(write=True) could be set also

cap_adata.obs = CapAnnDataDF.from_df(df)

Trigger rename column and there will be exception:

def rename_column(self, old_name: str, new_name: str) -> None:
i = np.where(self.column_order == old_name)[0]
>       self.column_order[i] = new_name
E       ValueError: assignment destination is read-only

Workaround (set column_order argument):

column_order = np.array([labelset, label])
cap_adata.obs = CapAnnDataDF.from_df(df=df, column_order=column_order)

Also we should revise this ability to set column_order directly instead of taking it from the original dataframe...

rm1113 commented 2 weeks ago

Also need to consider pandas warning:

UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
    df.column_order = order