Skip to content

Can't assign to obs in AnnData view #148

Closed

Description

@joshua-gould
In [1]:
import anndata
import pandas as pd
adata = anndata.AnnData(
    X=np.array([[1,2], [1,2], [1,2]]),
    obs=pd.DataFrame(index=['a','b','c']),
    var=pd.DataFrame(index=['a','b']),
)
s = adata[:, ['b','a']]
df = pd.DataFrame(index=['a', 'b'], data={'c1':[1,2]})
df2 = s.obs.join(df)
df2['c1']
Out[1]:
a    1.0
b    2.0
c    NaN
Name: c1, dtype: float64
In [2]:
s.obs = df2
s.obs['c1']
Out[2]:
KeyError: 'c1'

Activity

  1. ivirshup commented on May 9, 2019

    @ivirshup
    Member

    @joshua-gould, that seems wrong, thanks for the report! (btw: I think you're missing an import numpy as np)

    @falexwolf or @flying-sheep: this should be creating a copy of s then assigning df2 to that, right? The setter looks like it's trying to do the modification in-place:

    https://github.com/theislab/anndata/blob/fc7f3cb06c191aff4fcad6af88f0c3012d7d1a65/anndata/base.py#L1090-L1098

    (ditto with var)

  2. flying-sheep commented on May 9, 2019

    @flying-sheep
    Member

    In the commit that introduced the view functionality, 542edcc, the AnnDataAttributeView and ArrayView classes already had copy-on-write behavior (their __setitem__ methods called adata_view._init_as_actual(…))

    That was also the last commit that touched the last two lines of obs.setter and so on, not introducing COW behavior for those.

    I also think that the only sound behavior would be to be consistent here.

  3. ivirshup commented on May 9, 2019

    @ivirshup
    Member

    Agreed.

    I just keep running into code that's definitely trying to make modifications in-place.

    Aside on the formatting above:

    • I think you might want In[2]:
    • Is that just a table, or is there nice notebook formatting now? I can't look at the source markdown on this repo :(
  4. flying-sheep commented on May 9, 2019

    @flying-sheep
    Member

    Sadly just a table. It was an experiment to see how much work it is, and the answer is: too much (I had to do the HTML markup manually)

    I think this format is probably the most practical to show input and output:

    >>> import numpy as np
    >>> np.array([[1,2], [3,4]])
    array([[1, 2],
           [3, 4]])
    >>> # more code
  5. added a commit that references this issue on May 17, 2019
    24d32e4
  6. added 3 commits that reference this issue on Jun 5, 2019
    1a82969
    0f6af3c
    3cae4fe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions