Skip to content

Commit

Permalink
Update to_df
Browse files Browse the repository at this point in the history
Add argument `obs` to include columns from `.obs`
in generated metadata.
  • Loading branch information
WeilerP committed Mar 6, 2024
1 parent 9452dd5 commit 3fbf837
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions anndata/_core/anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ def transpose(self) -> AnnData:

T = property(transpose)

def to_df(self, layer=None) -> pd.DataFrame:
def to_df(self, layer=None, obs: Optional[Union[str, List[str]]]=None) -> pd.DataFrame:
"""\
Generate shallow :class:`~pandas.DataFrame`.
Expand All @@ -1299,6 +1299,8 @@ def to_df(self, layer=None) -> pd.DataFrame:
------
layer : str
Key for `.layers`.
obs
Column(s) of `.obs` to include in DataFrame.
"""
if layer is not None:
X = self.layers[layer]
Expand All @@ -1308,7 +1310,11 @@ def to_df(self, layer=None) -> pd.DataFrame:
X = self.X
if issparse(X):
X = X.toarray()
return pd.DataFrame(X, index=self.obs_names, columns=self.var_names)

df = pd.DataFrame(X, index=self.obs_names, columns=self.var_names)
if obs is not None:
df = df.merge(self.obs[obs], left_index=True, right_index=True)
return df

def _get_X(self, use_raw=False, layer=None):
"""\
Expand Down

0 comments on commit 3fbf837

Please sign in to comment.