Skip to content

Commit

Permalink
Add conversion for pandas categorical arrays
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
lazappi committed Apr 6, 2023
1 parent 079ee1e commit 928f952
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(py_to_r,numpy.ndarray)
S3method(py_to_r,pandas.core.arrays.categorical.Categorical)
S3method(py_to_r,pandas.core.arrays.masked.BaseMaskedArray)
export(.AnnDataVersions)
export(AnnData2SCE)
Expand Down
28 changes: 28 additions & 0 deletions R/reticulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' - `pandas.core.arrays.masked.BaseMaskedArray` - Handle conversion of
#' **pandas** arrays (used when by `AnnData` objects when there are missing
#' values)
#' - `pandas.core.arrays.categorical.Categorical` - Handle conversion of
#' **pandas** categorical arrays
#'
#' @author Luke Zappia
#'
Expand Down Expand Up @@ -84,3 +86,29 @@ py_to_r.pandas.core.arrays.masked.BaseMaskedArray <- function(x) {

return(x)
}

#' @export
py_to_r.pandas.core.arrays.categorical.Categorical <- function(x) {
disable_conversion_scope(x)

# Get the category levels
cats <- reticulate::py_to_r(x$categories$to_list())

# Record which values should be NA
is_na <- reticulate::py_to_r(x$isna())

# Fill NA values with a dummy
x <- x$fillna(value = cats[1])

# Convert to list and then to R using default conversion
x <- x$tolist()
x <- reticulate::py_to_r(x)

# Restore the NA values
x[is_na] <- NA

# Convert to factor
x <- factor(x, levels = cats)

return(x)
}
2 changes: 2 additions & 0 deletions man/r-py-conversion.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 928f952

Please sign in to comment.