Skip to content

Commit

Permalink
Cast object dtype columns to strings in crosswalking
Browse files Browse the repository at this point in the history
  • Loading branch information
axdanbol committed Feb 12, 2024
1 parent b03d7b9 commit 798e9b4
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions containers/crosswalking/context/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def crosswalk(

result = matrix.copy()
result.obs = merged_obs
_fix_obs_columns_dtype(result)

return result


Expand All @@ -129,6 +131,18 @@ def _set_default_match(obs: pd.DataFrame, column: str) -> None:
obs.loc[obs[column].isna(), column] = "skos:exactMatch"


def _fix_obs_columns_dtype(matrix: anndata.AnnData):
"""Converts object and category columns to string to prevent errors when writing h5ad file.
Args:
matrix (AnnData): Matrix to update
"""
for column in matrix.obs.columns:
array = matrix.obs[column]
if array.dtype in ("category", "object"):
matrix.obs[column] = array.astype(str)


def _get_empty_table(args: argparse.Namespace) -> pd.DataFrame:
"""Creates an empty crosswalk table.
Expand Down

0 comments on commit 798e9b4

Please sign in to comment.