This is a test version of Biostars. For the public version, visit https://www.biostars.org.
scanpy add metadata

Hello,

I am trying to create anndata in Python, and I want to add metadata to each object.

I want to add a donor_id to my objects. Since one object is an RDS file that includes over 20 subjects and contains metadata called donor_id, I want to create one and be able to distinguish those 20 subjects separately. I have written my code as follows:

p9.obs['donor_id']='p9'

However, when I try to save the file, I receive this error message:

TypeError: Can't implicitly convert non-string objects to strings

How should I revise my code to avoid this error?

Thanks Andy

scanpy metadata

1 answer

I had this issue before, and I would love to learn the correct way of doing it. Instead, I use pickle to save the object.

import pickle

# your preprocess...
p9.obs['donor_id']='p9'

# save to file
with open(path_to_save, "wb") as file:
    pickle.dump(p9, file)

# load from file
with open(path_to_file, "rb") as file:
    p9 = pickle.load(file)

Log in to answer this question.