{"id":10127,"library":"pyreadr","title":"pyreadr","description":"pyreadr is a Python library designed to read and write R RData and Rds files, seamlessly converting them to and from pandas DataFrames. The current version is 0.5.6. Releases are generally infrequent, often focusing on build system improvements, dependency compatibility, or minor feature additions.","status":"active","version":"0.5.6","language":"en","source_language":"en","source_url":"https://github.com/ofajardo/pyreadr","tags":["r","rdata","rds","pandas","data-io","data-frames"],"install":[{"cmd":"pip install pyreadr","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core data structure for RData/Rds conversion.","package":"pandas"}],"imports":[{"symbol":"read_rdata","correct":"import pyreadr\nresult = pyreadr.read_rdata('file.RData')"},{"symbol":"write_rdata","correct":"import pyreadr\npyreadr.write_rdata('file.RData', {'df': df_obj})"},{"symbol":"read_rds","correct":"import pyreadr\nresult = pyreadr.read_rds('file.rds')"},{"symbol":"write_rds","correct":"import pyreadr\npyreadr.write_rds(df_obj, 'file.rds')"}],"quickstart":{"code":"import pyreadr\nimport pandas as pd\nimport numpy as np\nimport os\n\n# Create a dummy RData file for testing\ndata_for_r = {'df': pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']}),\n              'vec': np.array([10, 20, 30])}\npyreadr.write_rdata(\"dummy.RData\", data_for_r)\n\n# Read RData file\nresult_rdata = pyreadr.read_rdata(\"dummy.RData\")\n# result_rdata is a dictionary where keys are R object names\ndf_from_r = result_rdata['df']\nvec_from_r = result_rdata['vec']\n\n# Create a dummy Rds file for testing (single object)\ndf_to_rds = pd.DataFrame({'colA': [10, 20], 'colB': ['x', 'y']})\npyreadr.write_rds(df_to_rds, \"dummy.rds\")\n\n# Read Rds file\nresult_rds = pyreadr.read_rds(\"dummy.rds\")\n# result_rds is the pandas DataFrame directly\ndf_from_rds = result_rds\n\nprint(\"DataFrame from RData:\\n\", df_from_r)\nprint(\"Vector from RData:\\n\", vec_from_r)\nprint(\"DataFrame from Rds:\\n\", df_from_rds)\n\n# Clean up dummy files\nos.remove(\"dummy.RData\")\nos.remove(\"dummy.rds\")","lang":"python","description":"This quickstart demonstrates how to write Python data (pandas DataFrame, NumPy array) into RData and Rds files, and then read them back. For RData, the result is a dictionary mapping R object names to Python objects. For Rds (single object), the result is directly the Python object."},"warnings":[{"fix":"Try setting the environment variable `PYREADR_LINK_ICONV=1` before installing: `PYREADR_LINK_ICONV=1 pip install pyreadr`. Ensure `libiconv-dev` (or equivalent for your distro) is installed: e.g., `sudo apt-get install libiconv-hook-dev`.","message":"On some Linux distributions, `pyreadr`'s underlying C libraries (`librdata`, `libiconv`) might fail to link during installation, leading to `ImportError`. This often happens when `libiconv` development headers are not correctly found.","severity":"gotcha","affected_versions":"All versions, particularly on Linux."},{"fix":"Upgrade `pyreadr` to version 0.5.5 or newer: `pip install --upgrade pyreadr`. If using an older pandas version, ensure it's within the range supported by your `pyreadr` version.","message":"`pyreadr` versions older than 0.5.5 may not be compatible with pandas 3.0 or newer due to internal changes in pandas. This could lead to various `TypeError` or `AttributeError` exceptions.","severity":"breaking","affected_versions":"<0.5.5"},{"fix":"If encountering issues with specific R files, try simplifying the R objects before saving them in R. Check the `librdata` documentation for supported R types. For debugging, inspect the `pyreadr.read_rdata(file_path).keys()` to see what objects are successfully parsed.","message":"The underlying `librdata` library may not support all possible RData/Rds file versions or complex R object types (e.g., S4 objects, environments, specific user-defined types). Attempting to read unsupported structures might result in errors or incomplete data.","severity":"gotcha","affected_versions":"All versions."}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"On Linux, ensure development headers for `librdata` and `libiconv` are installed (e.g., `sudo apt-get install librdata-dev libiconv-hook-dev`). Try reinstalling with `PYREADR_LINK_ICONV=1 pip install pyreadr`. On Windows, ensure you are using pre-built wheels, or have a compatible build environment set up.","cause":"The C libraries that `pyreadr` relies on (like `librdata` or `libiconv`) were not correctly compiled, linked, or found at runtime.","error":"ImportError: DLL load failed while importing _readr (Windows) or ImportError: librdata.so: cannot open shared object file: No such file or directory (Linux)"},{"fix":"Inspect the keys returned by `pyreadr.read_rdata(file_path).keys()` to get the actual object names present in the RData file. R object names can sometimes have different conventions or hidden characters.","cause":"The RData file does not contain an object with the exact name specified, or the name has unexpected casing/characters.","error":"KeyError: '<object_name>' when accessing objects from `read_rdata` result."},{"fix":"First, try updating `pyreadr`: `pip install --upgrade pyreadr`. If the error persists, check `pyreadr`'s documentation or release notes for specific pandas version requirements and adjust your pandas installation accordingly (e.g., `pip install pandas==X.Y.Z`).","cause":"Your installed `pyreadr` version has a specific compatibility requirement with pandas, and your pandas version falls outside of this range.","error":"ValueError: Pandas version X.Y.Z is not supported. Please update to a newer version of pandas or pyreadr."}]}