leaf_engine.io.gdrive.read

Functions

_download_file(→ io.BytesIO)

Downloads file stored on Google Drive.

_parse_url(→ Tuple[str, str])

Parses a Google Drive spreadsheets or file URL to extract file ID.

_read_drive(→ pandas.DataFrame)

Wrapped by read_drive to allow disk caching.

read_drive(→ pandas.DataFrame)

Reads CSV, Excel, or Google Spreadsheet file from Google Drive either by

Module Contents

leaf_engine.io.gdrive.read._download_file(file_url: str) io.BytesIO

Downloads file stored on Google Drive.

NOTE: The file you want to download needs to be shared with the service account you are using to make the request.

Parameters:

file_url (str) –

Return type:

io.BytesIO

leaf_engine.io.gdrive.read._parse_url(url: str) Tuple[str, str]

Parses a Google Drive spreadsheets or file URL to extract file ID.

Parameters:

url (str) –

Return type:

Tuple[str, str]

leaf_engine.io.gdrive.read._read_drive(url: str | None = None, path: str | None = None, **kwargs) pandas.DataFrame

Wrapped by read_drive to allow disk caching.

See read_drive for documentation.

Parameters:
  • url (Optional[str]) –

  • path (Optional[str]) –

Return type:

pandas.DataFrame

leaf_engine.io.gdrive.read.read_drive(url: str | None = None, path: str | None = None, cache: bool = True, **kwargs) pandas.DataFrame

Reads CSV, Excel, or Google Spreadsheet file from Google Drive either by url or path.

Parameters:
  • url (Optional[str], optional) – File URL. Can be copied from browser navigation bar. Defaults to None.

  • path (Optional[str], optional) – File path. First part needs to be drive name. Defaults to None.

  • cache (bool, optional) – Whether to cache the result to disk. Defaults to True. This makes subsequent calls to read_drive faster.

  • **kwargs – Keyword arguments passed to pandas.read_csv or pandas.read_excel.

Return type:

pandas.DataFrame

Examples: >>> df = read_drive(url=”https://drive.google.com/file/d/XXYYZZ/view?usp=sharing”) >>> df = read_drive(path=”Data Science/folder1/folder2/file.csv”)

Raises:
  • LeafGoogleDriveException – If both url and path are None or both are not None.

  • LeafGoogleDriveException – If file is not found on Google Drive.

  • LeafGoogleDriveException – If unable to download file from Google Drive.

  • ValueError – If neither pd.read_csv nor pd.read_excel can read the file.

Returns:

DataFrame read from file.

Return type:

pd.DataFrame

Parameters:
  • url (Optional[str]) –

  • path (Optional[str]) –

  • cache (bool) –