-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modifying a subset of AnnData using the .iloc/.loc method does not make a new copy, and the original object is modified #1840
Comments
Hello,
However I wish it to be a proper AnnData object as to save it into h5ad file.
|
@crazyxiaoj as far as I can tell, this behavior is totally expected. A view is just that, a view. So if you edit the view, you'll edit the actual object. It might be worth disallowing this completely, but there are probably cases where the behavior is desirable.
@AlessiaLeclercq If you can't do it directly with the object you have (possible), you certainly can create a copy via import anndata as ad
import numpy as np
adata = ad.AnnData(X=np.array([[1, 2], [3, 4]]))
adata[:1,:].write_h5ad("foo.h5ad") # works, but also `.copy` is fine |
Your explanation is a bit unclear to me. I referred to the content on the following webpage: https://anndata.readthedocs.io/en/stable/generated/anndata.AnnData.html. Here’s the relevant excerpt:
Based on the paragraph above, it appears that modifying properties like obs results in the creation of a new AnnData object. Additionally, I noticed that performing an assignment directly using [], rather than the iloc method, also triggers the creation of a new object. |
Thanks for sharing this. The issue here would be wrapping every single dataframe method. I'm not sure why this wasn't done initially since only
and much more. This might be why this wasn't done. So it's possible we should carve out an exception for |
Thank you for your clarification. I think I'm beginning to understand. Do you still believe it's necessary to open this issue? If you feel it is no longer needed, we can consider closing this issue. |
Well it is certainly an inconsistency so it seems we should either edit the docs or add the feature set. @ivirshup I've asked to weigh in |
Thank you! |
Please make sure these conditions are met
Report
When using the .iloc or .loc methods to modify a subset of an AnnData object, it seems that no new copy is created; instead, the original AnnData object is directly modified.
Code:
As a beginner, I'm not sure if this behavior is a bug or by design. Could someone clarify whether this is intentional, and if so, could you please explain why it functions this way? Thanks for your assistance!
Versions
The text was updated successfully, but these errors were encountered: