Skip to content

Commit

Permalink
Allow deleting obs/var cols
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
flying-sheep committed Aug 27, 2018
1 parent bbef0bc commit 9b15757
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions anndata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import numpy as np
from numpy import ma
import pandas as pd
from numpy.lib.recfunctions import rec_drop_fields
from pandas.core.index import RangeIndex
from pandas.api.types import is_string_dtype, is_categorical
from scipy import sparse
Expand Down Expand Up @@ -156,6 +157,17 @@ def __setitem__(self, key, arr):
new = BoundRecArr(new, self._parent, self._attr)
setattr(self._parent, self._attr, new)

def __delitem__(self, key):
"""Delete field with name."""
if key not in self.dtype.names:
raise ValueError(
'Currently, can only delete single names from {}.'
.format(self.dtype.names)
)
new_array = rec_drop_fields(self, key)
new = BoundRecArr(new_array, self._parent, self._attr)
setattr(self._parent, self._attr, new)

def to_df(self):
"""Convert to pandas dataframe."""
df = pd.DataFrame(index=RangeIndex(0, self.shape[0], name=None))
Expand Down
9 changes: 9 additions & 0 deletions anndata/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ def test_append_col():
adata.obs['new4'] = 'far too long'.split()


def test_delete_col():
adata = AnnData(np.array([[1, 2, 3], [4, 5, 6]]), dict(o1=[1, 2], o2=[3, 4]))
assert ['o1', 'o2'] == adata.obs_keys()

del adata.obs['o1']
assert ['o2'] == adata.obs_keys()
assert [3, 4] == adata.obs['o2'].tolist()


def test_set_obs():
adata = AnnData(np.array([[1, 2, 3], [4, 5, 6]]))

Expand Down

2 comments on commit 9b15757

@falexwolf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thank you, Philipp! 😄 I thought I restored this already months ago, but quite evidently, I didn't.

@flying-sheep
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem 😄

Please sign in to comment.