Default conversions#

This page list the default conversions between R and Python objects.

Basic types#

The conversion of basic types is performed directly by the convert() and convert_python_to_r_data() functions (or the underlying Converter and ConverterFromPythonToR). Thus, changing the conversion for basic types currently requires creating a custom converter class. The default converters perform the following conversions:

R type

Python type

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.

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 / vector

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.

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 a suitable fixed-length string dtype.

symbol

str.

Custom classes#

In addition, R objects containing a “class” attribute, or Python objects not listed above, are passed to an R-to-Python or Python-to-R constructor function, respectively. When the “class” attribute contains several class names, these are tried in order. A dictionary of constructor functions can be supplied to the converters as exemplified in Converting between R and Python classes. The default constructor dictionaries perform the following conversions:

R class

Python class

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.