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)

Read an R RData or Rds file into pandas data frames

Parameters:
  • path (str) – path to the file. The string is 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 coverted 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.

Returns:

result – object name as key and pandas 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)

Write a single pandas data frame to a rdata file.

Parameters:
  • path (str) – path to the file. The string is assumed to be utf-8 encoded.

  • df (pandas data frame) – the dataframe 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”.

pyreadr.pyreadr.write_rds(path, df, dateformat='%Y-%m-%d', datetimeformat='%Y-%m-%d %H:%M:%S', compress=None)

Write a single pandas data frame to a rds file.

Parameters:
  • path (str) – path to the file. The string is assumed to be utf-8 encoded.

  • df (pandas data frame) – the dataframe 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) – compression to use, defaults to no compression. Only “gzip” supported.

Indices and tables