Skip to content

Commit

Permalink
(fix): fix index on obs.to_df()
Browse files Browse the repository at this point in the history
  • Loading branch information
ilan-gold committed Mar 9, 2023
1 parent 515a6d2 commit f4f5c7c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
21 changes: 11 additions & 10 deletions anndata/experimental/read_remote/read_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ def to_df(self) -> pd.DataFrame:
"""Convert to pandas dataframe."""
df = pd.DataFrame(index=self.dim_names)
for key in self.keys():
z = self[key]
if isinstance(z, zarr.Group) and "codes" in z: # catrgoricql
value = pd.Categorical.from_codes(
codes=read_elem(z["codes"]),
categories=read_elem(z["categories"]),
ordered=bool(_read_attr(z.attrs, "ordered")),
)
else:
value = z[()]
df[key] = value
if "index" not in key:
z = self[key]
if isinstance(z, zarr.Group) and "codes" in z: # catrgoricql
value = pd.Categorical.from_codes(
codes=read_elem(z["codes"]),
categories=read_elem(z["categories"]),
ordered=bool(_read_attr(z.attrs, "ordered")),
)
else:
value = z[()]
df[key] = value
return df

@property
Expand Down
12 changes: 12 additions & 0 deletions anndata/tests/test_read_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ def test_read_write_X(tmp_path, mtx_format):
# remote.write_zarr(remote_pth) # need to implement writing!

assert np.all(asarray(orig.X) == asarray(remote.X))
assert (orig.obs == remote.obs.to_df()).all().all()
assert (orig.var == remote.var.to_df()).all().all()


def test_read_write_full(adata, tmp_path):
base_pth = Path(tmp_path)
orig_pth = base_pth / "orig.zarr"
adata.write_zarr(orig_pth)
remote = read_remote(orig_pth)
assert np.all(asarray(adata.X) == asarray(remote.X))
assert (adata.obs == remote.obs.to_df()).all().all()
assert (adata.var == remote.var.to_df()).all().all()

0 comments on commit f4f5c7c

Please sign in to comment.