Skip to main content

rdata

build status Documentation Status Coverage Status Pypi version Zenodo DOI

Read R datasets from Python.

The package rdata offers a lightweight way to import R datasets/objects stored in the “.rda” and “.rds” formats into Python. Its main advantages are:

  • It is a pure Python implementation, with no dependencies on the R language or related libraries. Thus, it can be used anywhere where Python is supported, including the web using Pyodide.

  • It attempt to support all R objects that can be meaningfully translated. As opposed to other solutions, you are no limited to import dataframes or data with a particular structure.

  • It allows users to easily customize the conversion of R classes to Python ones. Does your data use custom R classes? Worry no longer, as it is possible to define custom conversions to the Python classes of your choosing.

  • It has a permissive license (MIT). As opposed to other packages that depend on R libraries and thus need to adhere to the GPL license, you can use rdata as a dependency on MIT, BSD or even closed source projects.

Installation

rdata is on PyPi and can be installed using pip:

pip install rdata

It is also available for conda using the conda-forge channel:

conda install -c conda-forge rdata

Documentation

The documentation of rdata is in ReadTheDocs.

Simple usage

Read a R dataset

The common way of reading an R dataset is the following one:

>>> import rdata
>>> parsed = rdata.parser.parse_file(rdata.TESTDATA_PATH / "test_vector.rda")
>>> converted = rdata.conversion.convert(parsed)
>>> converted
{'test_vector': array([1., 2., 3.])}

This consists on two steps:

  1. First, the file is parsed using the function parse_file. This provides a literal description of the file contents as a hierarchy of Python objects representing the basic R objects. This step is unambiguous and always the same.

  2. Then, each object must be converted to an appropriate Python object. In this step there are several choices on which Python type is the most appropriate as the conversion for a given R object. Thus, we provide a default convert routine, which tries to select Python objects that preserve most information of the original R object. For custom R classes, it is also possible to specify conversion routines to Python objects.

Convert custom R classes

The basic convert routine only constructs a SimpleConverter objects and calls its convert method. All arguments of convert are directly passed to the SimpleConverter initialization method.

It is possible, although not trivial, to make a custom Converter object to change the way in which the basic R objects are transformed to Python objects. However, a more common situation is that one does not want to change how basic R objects are converted, but instead wants to provide conversions for specific R classes. This can be done by passing a dictionary to the SimpleConverter initialization method, containing as keys the names of R classes and as values, callables that convert a R object of that class to a Python object. By default, the dictionary used is DEFAULT_CLASS_MAP, which can convert commonly used R classes such as data.frame and factor.

As an example, here is how we would implement a conversion routine for the factor class to bytes objects, instead of the default conversion to Pandas Categorical objects:

>>> import rdata
>>> def factor_constructor(obj, attrs):
...     values = [bytes(attrs['levels'][i - 1], 'utf8')
...               if i >= 0 else None for i in obj]
...
...     return values
>>> new_dict = {
...         **rdata.conversion.DEFAULT_CLASS_MAP,
...         "factor": factor_constructor
...         }
>>> parsed = rdata.parser.parse_file(rdata.TESTDATA_PATH
...                                  / "test_dataframe.rda")
>>> converted = rdata.conversion.convert(parsed, new_dict)
>>> converted
{'test_dataframe':   class  value
    1     b'a'      1
    2     b'b'      2
    3     b'b'      3}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rdata-0.9.1.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rdata-0.9.1-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file rdata-0.9.1.tar.gz.

File metadata

  • Download URL: rdata-0.9.1.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for rdata-0.9.1.tar.gz
Algorithm Hash digest
SHA256 014f0d462c63508620482bde7241a4300337d0c170fd38f157f4b301faaa7f27
MD5 4a952e009ff5c740deaa213372adf6c2
BLAKE2b-256 2cff19ceeb69a448f58fc8b30927aa25e19fee5309530c93deda9fdaf1514d9c

See more details on using hashes here.

File details

Details for the file rdata-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: rdata-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for rdata-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c510aa4b8b1c48b43e77198cdc80c36a1f5f989a24d24e202803395158d06b22
MD5 22a62d2f53f63678cfa2bdd1e67ad9d3
BLAKE2b-256 9a01d4170fcd7bc4302a447d57aaef0e35774fac446cb7ac9335a10a2796e00e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page