Default conversions#

This page list the default conversions applied to R objects to convert them to Python objects.

Basic types#

The conversion of basic types is performed directly by the Converter used. Thus, changing the conversion for basic types currently requires creating a custom Converter class. The default SimpleConverter realizes the following conversions:

R object type

Python conversion

builtin function

rdata.conversion.RBuiltin.

bytecode

rdata.conversion.RBytecode.

char (internal)

str or bytes (depending on the encoding flags).

closure

rdata.conversion.RFunction.

complex

numpy.ndarray with 128-bits complex dtype.

numpy.ma.MaskedArray with 128-bits complex dtype if it contains NA values.

xarray.DataArray if it contains labeled dimensions.

environment

rdata.conversion.REnvironment. There are three special cases: the empty, base and global environments, which are all empty by default. The base and global environments may be supplied to the converter.

expression

rdata.conversion.RExpression.

external pointer

rdata.conversion.RExternalPointer.

integer

numpy.ndarray with 32-bits integer dtype.

numpy.ma.MaskedArray with 32-bits integer dtype if it contains NA values.

xarray.DataArray if it contains labeled dimensions.

language

rdata.conversion.RLanguage.

list

list (if untagged).

dict (if tagged). Empty lists are considered tagged.

logical (boolean)

numpy.ndarray with boolean dtype.

numpy.ma.MaskedArray with boolean dtype if it contains NA values.

xarray.DataArray if it contains labeled dimensions.

missing argument

NotImplemented.

NULL

None.

real

numpy.ndarray with 64-bits floating point dtype.

numpy.ma.MaskedArray with 64-bits floating point dtype if it contains NA values.

xarray.DataArray if it contains labeled dimensions.

reference

The referenced value, that is, an object already converted.

S4 object

types.SimpleNamespace.

special function

rdata.conversion.RBuiltin.

string

numpy.ndarray with suitable fixed-length string dtype.

symbol

str.

vector

list (if untagged).

dict (if tagged). Empty lists are considered tagged.

Custom classes#

In addition, objects containing a “class” attribute are passed to a “constructor function”, if one is available. A dictionary of constructor functions can be supplied to the converter, where the key of each element corresponds to the class name. When the “class” attribute contains several class names, these are tried in order. The default constructor dictionary allows to convert the following R classes:

R class

Python conversion

data.frame

pandas.DataFrame.

factor

pandas.Categorical.

ordered

pandas.Categorical (with ordered categories).

srcfile

rdata.conversion.SrcFile.

srcfilecopy

rdata.conversion.SrcFileCopy.

srcref

rdata.conversion.SrcRef.

ts

pandas.Series.