R and pandas data frames

R data.frame and :class:pandas.DataFrame objects share a lot of conceptual similarities, and :mod:pandas chose to use the class name DataFrame after R objects.

In a nutshell, both are sequences of vectors (or arrays) of consistent length or size for the first dimension (the "number of rows"). if coming from the database world, an other way to look at them is column-oriented data tables, or data table API.

rpy2 is providing an interface between Python and R, and a convenience conversion layer between :class:rpy2.robjects.vectors.DataFrame and :class:pandas.DataFrame objects, implemented in :mod:rpy2.robjects.pandas2ri.

From pandas to R

Pandas data frame:

R data frame converted from a pandas data frame:

The conversion is automatically happening when calling R functions. For example, when calling the R function base::summary:

Note that a ContextManager is used to limit the scope of the conversion. Without it, rpy2 will not know how to convert a pandas data frame:

From R to pandas

Starting from an R data frame this time:

It can be converted to a pandas data frame using the same converter:

Date and time objects

The timezone used for conversion is the system's default timezone unless pandas2ri.default_timezone is specified... or unless the time zone is specified in the original time object: