Skip to content
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

Update to_df to include metadata #1400

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

T = property(transpose)

def to_df(self, layer=None) -> pd.DataFrame:
def to_df(self, layer=None, obs: str | list[str] | None = None) -> pd.DataFrame:
"""\
Generate shallow :class:`~pandas.DataFrame`.

Expand All @@ -1299,6 +1299,8 @@
------
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 @@
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)

Check warning on line 1316 in anndata/_core/anndata.py

View check run for this annotation

Codecov / codecov/patch

anndata/_core/anndata.py#L1316

Added line #L1316 was not covered by tests
return df

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