How to ensure a numeric matrix is retrieved from R

Viewed 29

Within a local conversion context established with the default_converter and pandas2ri.converter local converters, I am obtaining a rpy2.robjects.vectors.BoolMatrix object (assigned to a variable res):

<rpy2.robjects.vectors.BoolMatrix object at 0x7fa48d531040> [RTYPES.LGLSXP]
R classes: ('matrix', 'array')
[NA, NA, NA]

I need to build a pandas dataframe from this object. However, the conversion gives:

pd.DataFrame(res)
            0
0 -2147483648
1 -2147483648
2 -2147483648

How can I ensure this kind of matrix is converted to nan values?

1 Answers

This is likely a conversion buglet on the rpy2 side. The recommended way to proceed is to 1) open an issue on the rpy2 tracker on Github and 2) have the following workaround until it is fixed:

import math
import rpy2.robjects as ro

# Let dataf_pd be the pandas DataFrame resulting from the conversion from
# an R data.frame and "x" be the name of the column of booleans with NAs.
dataf_pd.loc[dataf_pd.x == ro.NA_Logical] = math.nan
Related