read_rds#
- read_rds(file_or_path, *, expand_altrep=True, altrep_constructor_dict=DEFAULT_ALTREP_MAP, constructor_dict=DEFAULT_CLASS_MAP, default_encoding=None, force_default_encoding=False, global_environment=None, base_environment=None)[source]#
Read an RDS file, containing an R object.
This is a convenience function that wraps
rdata.parser.parse_file()
andrdata.parser.convert()
, as it is the common use case.- Parameters:
file_or_path (AcceptableFile | os.PathLike[Any] | Traversable | str) β File in the RDS format.
expand_altrep (bool) β Whether to translate ALTREPs to normal objects.
altrep_constructor_dict (AltRepConstructorMap) β Dictionary mapping each ALTREP to its constructor.
constructor_dict (ConstructorDict) β
Dictionary mapping names of R classes to constructor functions with the following prototype:
def constructor(obj, attrs): ...
This dictionary can be used to support custom R classes. By default, the dictionary used is
DEFAULT_CLASS_MAP
which has support for several common classes.default_encoding (str | None) β Default encoding used for strings with unknown encoding. If None, the one stored in the file will be used, or ASCII as a fallback.
force_default_encoding (bool) β Use the default encoding even if the strings specify other encoding.
global_environment (MutableMapping[str, Any] | None) β Global environment to use. By default is an empty environment.
base_environment (MutableMapping[str, Any] | None) β Base environment to use. By default is an empty environment.
- Returns:
Contents of the file converted to a Python object.
- Return type:
Any
See also
read_rda()
: Similar function that parses a RDA or RDATA file.Examples
Parse one of the included examples, containing a dataframe
>>> import rdata >>> >>> data = rdata.read_rds( ... rdata.TESTDATA_PATH / "test_dataframe.rds" ... ) >>> data class value 1 a 1 2 b 2 3 b 3