Welcome to pyreadr’s documentation!¶
@author: Otto Fajardo
- pyreadr.pyreadr.download_file(url, destination_path)¶
Downloads a file from a web url to destination_path.
- Parameters:
url (str) – url of the file
destination_path (str) – path to write the file to disk
- Returns:
it gives back the path to where the file was written.
- Return type:
str
- pyreadr.pyreadr.list_objects(path)¶
Read an R RData or Rds file and lists objects and their column names. Not all objects are readable, and also it is not always possible to read the column names without parsing the whole file, in those cases this method will return Nones instead of column names.
- Parameters:
path (str) – path to the file. The string is assumed to be utf-8 encoded.
- Returns:
result – a list of dictionaries, where each dictionary has a key “object_name” with the name of the object and columns with a list of columns.
- Return type:
list
- pyreadr.pyreadr.read_r(path, use_objects=None, timezone=None, output_format='pandas')¶
Read an R RData or Rds file into pandas or polars data frames.
- Parameters:
path (str, bytes, Path, or file-like object) – path to the file, or a file-like object with read() and seek() methods (e.g. io.BytesIO). String paths are assumed to be utf-8 encoded.
use_objects (list, optional) – a list with object names to read from the file. Only those objects will be imported. Case sensitive!
timezone (str, optional) – timezone to localize datetimes, UTC otherwise. R datetimes (POSIXct and POSIXlt) are stored as UTC, but converted to some timezone (explicitly if set by the user or implicitly to local zone) when displaying it in R. librdata cannot recover that timezone information therefore timestamps are displayed in UTC, unless this parameter is set.
output_format (str, optional) – format of the output data frames. “pandas” (default) or “polars”.
- Returns:
result – object name as key and pandas or polars data frame as value.
- Return type:
OrderedDict
- pyreadr.pyreadr.write_rdata(path, df, df_name='dataset', dateformat='%Y-%m-%d', datetimeformat='%Y-%m-%d %H:%M:%S', compress=None, compresslevel=9)¶
Write a single pandas or polars data frame to a RData file.
- Parameters:
path (str, bytes, or Path) – path to the file. The string is assumed to be utf-8 encoded.
df (pandas or polars DataFrame) – the data frame to write.
df_name (str) – name for the R dataframe object, cannot be empty string. If not supplied will default to “dataset”.
dateformat (str) – string to format datetime.date objects. By default “%Y-%m-%d”.
datetimeformat (str) – string to format datetime like objects. By default “%Y-%m-%d %H:%M:%S”.
compress (str, optional) – compression to use, defaults to no compression. Only “gzip” supported.
compresslevel (int, optional) – compression level for gzip (1-9), default 9.
- pyreadr.pyreadr.write_rds(path, df, dateformat='%Y-%m-%d', datetimeformat='%Y-%m-%d %H:%M:%S', compress=None, compresslevel=9)¶
Write a single pandas or polars data frame to a Rds file.
- Parameters:
path (str, bytes, or Path) – path to the file. The string is assumed to be utf-8 encoded.
df (pandas or polars DataFrame) – the data frame to write.
dateformat (str) – string to format datetime.date objects. By default “%Y-%m-%d”.
datetimeformat (str) – string to format datetime like objects. By default “%Y-%m-%d %H:%M:%S”.
compress (str, optional) – compression to use, defaults to no compression. Only “gzip” supported.
compresslevel (int, optional) – compression level for gzip (1-9), default 9.