PolarsΒΆ

polars is a data frame implementation in Rust using Arrow with Python bindings. Passing polars.DataFrame: object between Python and R within the same process can therefore also use rpy2_arrow.

Warning

Conversion rules for polars are optional. If you plan to use them, install rpy2_arrow using one of the two following way to ensure that Python package dependencies are fetched.

or

The R package polars is also required by several conversion rules. This dependency cannot be resolved by pip. It has to be installed for the R your are planning to use with rpy2.

The conversion rules in rpy2_arrow.polars.converter should be all that is needed for most use-cases:

import polars
import rpy2.robjects
import rpy2_arrow.polars as rpy2polars

# Polars DataFrame to show conversions.
podataf = polars.DataFrame({'a': [1, 2], 'b': [3, 4]})
print('Python polars.DataFrame:')
print(podataf)

with pypolars.converter.context() as cv_ctx:
    # Calls to rpy2 within this block are using conversion
    # rules for polars.

    # Create a symbol 'r_podataf' in R's ".GlobalEnv" (the top-level
    # namespace in R), assigning to it our Python polars.DataFrame
    # instance. The assignment uses whatever conversion rules are
    # active.
    rpy2.robjects.globalenv['r_podataf'] = podataf

print('R polars::pl$DataFrame:')
rpy2.robjects.r('print(r_podataf)')