Registry / gcp / gspread-pandas

gspread-pandas

JSON →
library3.3.0pypypi✓ verified 84d ago

gspread-pandas is a Python package that simplifies interaction between Pandas DataFrames and Google Spreadsheets. It leverages the `gspread` library for underlying API communication and adds extensive functionality for handling DataFrame-specific operations, multi-level headers, merged cells, and authentication. The library is actively maintained with regular updates and releases.

pip install gspread-pandas
INSTALL
IMPORT
SIG · GSPREAD-PANDAS
G
gspread-pandas
gcppythonv3.3.0
Install
9.8s avg
Import
2287ms
Disk
192MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 2.349s · 191.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 9.8s · import 2.226s · 184MB
192MB installed
● package 192MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Spread
from gspread_pandas import Spread
The primary class for interacting with a Google Spreadsheet via a Pandas DataFrame.
Client
from gspread_pandas import Client
Used for managing Google API client and authentication. Often implicitly handled by `Spread`.

This quickstart demonstrates how to authenticate, open/create a Google Spreadsheet, write a Pandas DataFrame to a specified worksheet, and then read it back. Proper authentication (e.g., setting up a service account JSON file in `~/.config/gspread_pandas/google_secret.json` and sharing your spreadsheet with the service account email) is a prerequisite for running this code.

import pandas as pd import os from gspread_pandas import Spread # IMPORTANT: Ensure your Google client credentials (e.g., service account JSON) # are set up at ~/.config/gspread_pandas/google_secret.json or via environment variables. # See documentation for detailed authentication setup. # Replace with your actual spreadsheet name, or fetch from environment spreadsheet_name = os.environ.get('GSPREAD_PANDAS_SHEET_NAME', 'My Example Spreadsheet') # Create a dummy DataFrame to write to the spreadsheet data = {'col1': [1, 2, 3], 'col2': ['A', 'B', 'C']} df_to_write = pd.DataFrame(data) try: # Open the spreadsheet (or create if it doesn't exist and create_spread=True) # By default, it looks for ~/.config/gspread_pandas/google_secret.json spread = Spread(spreadsheet_name, create_spread=True) # Get a list of existing worksheets print(f"Worksheets in '{spreadsheet_name}': {spread.sheets}") # Write DataFrame to a new worksheet or an existing one # Using 'overwrite=True' to clear existing data, 'sheet=' to specify sheet name sheet_name = 'DataFrame Data' print(f"Writing DataFrame to sheet: '{sheet_name}'") spread.df_to_sheet(df_to_write, sheet=sheet_name, index=False, overwrite=True) # Read data back into a DataFrame df_from_sheet = spread.sheet_to_df(sheet=sheet_name) print(f"\nData read from '{sheet_name}':") print(df_from_sheet.head()) except Exception as e: print(f"An error occurred: {e}") print("Please ensure your Google API credentials are correctly configured and the spreadsheet exists/is accessible.")
Debug
Known issues
breakingVersion 3.0.0 removed support for Python 2.7. Users must migrate to Python 3.x (minimum Python 3.5, though newer versions are recommended).
fix
Upgrade your Python environment to Python 3.x.
affects: >=3.0.0
breakingVersion 3.0.0 upgraded the internal `gspread` dependency to version `5>=`. This might introduce breaking changes if your project relied on specific `gspread` APIs that changed between its major versions.
fix
Review the `gspread` changelog for versions 5.0.0 and above. Specifically, `gspread` v6 swapped arguments for `Worksheet.update` and requires `values` to be a 2D array.
affects: >=3.0.0
deprecatedThe `raw_column_names` parameter was removed in version 3.0.2. Code using this parameter will break.
fix
Remove the `raw_column_names` parameter from your function calls. Adjust your code to handle column names as per current documentation.
affects: >=3.0.2
gotchaWhen uploading large DataFrames, you might encounter Google Sheets API limits (e.g., max number of cells in a worksheet). If `df_to_sheet` adds rows/columns by default, it might exceed limits.
fix
Use `replace=True` in `df_to_sheet` to first resize and clear the worksheet, or manually resize the sheet to `1x1` using `spread.sheet.resize(1, 1)` before calling `df_to_sheet`.
affects: All
gotchaAuthentication requires setting up Google Cloud Project credentials (Service Account or OAuth Client ID) and placing the `google_secret.json` file in `~/.config/gspread_pandas/` (or `%APPDATA%\gspread_pandas` on Windows) by default. Without this, operations will fail.
fix
Follow the detailed authentication steps in the `gspread-pandas` or `gspread` documentation to create and configure your Google Cloud project and credentials. Ensure the service account has editor access to your spreadsheets.
affects: All
gotchaInternal `pandas` operations no longer use `inplace=True` as of version 3.2.1 to avoid `SettingWithCopyWarning`. If your code implicitly relied on previous in-place modifications (which were often ambiguous) or suppressed these warnings, behavior might change.
fix
Review DataFrame assignments and transformations. Prefer explicit copying (`.copy()`) or chained operations that return new DataFrames over methods that modify in-place if you require specific behavior. Pandas 3.0 introduced Copy-on-Write semantics, further emphasizing this.
affects: >=3.2.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'gspread'
The core `gspread` library, which `gspread-pandas` depends on, is not installed in your Python environment. This typically happens if `gspread-pandas` was installed but its dependencies were not automatically handled, or if you are using a different Python environment.
fix
Install the `gspread` library using pip: `pip install gspread`
gspread.exceptions.SpreadsheetNotFound
The Google Spreadsheet specified by name, ID, or URL could not be found or accessed by the authenticated service account or user. Common reasons include incorrect spreadsheet name/ID/URL, the spreadsheet not being shared with the service account's email, or the authenticated user lacking appropriate permissions.
fix
Ensure the spreadsheet name, ID, or URL is correct. For service accounts, explicitly share the Google Sheet with the service account's email address (found in your `client_secret.json` or equivalent credentials file). Verify that the Google Drive API and Google Sheets API are enabled for your project in the Google Cloud Console.
Client authentication issue with gspread library (related to gspread 6.0.0+)
`gspread-pandas` versions prior to major updates may not be compatible with `gspread` version 6.0.0 or higher due to breaking changes in `gspread`'s API, particularly around how the `Client` is initialized with session arguments.
fix
Downgrade `gspread` to a compatible version (e.g., `gspread<6.0.0`, specifically `gspread==5.12.4` is known to work for some users). Alternatively, ensure you are using the latest `gspread-pandas` version that officially supports `gspread` v6, if available.
This action would increase the number of cells in the workbook above the limit of 10000000 cells.
You are attempting to upload a DataFrame to Google Sheets that, when combined with the existing sheet dimensions (even if empty), would exceed Google Sheets' maximum cell limit of 10 million cells per worksheet.
fix
When using `spread.df_to_sheet()`, pass `replace=True` to first resize the worksheet and clear its values before writing, which helps manage cell count. Alternatively, manually resize the sheet to 1x1 using `spread.sheet.resize(1, 1)` before calling `df_to_sheet()`.
AttributeError: 'Worksheet' object has no attribute 'update'
This error typically occurs when trying to use `worksheet.update()` directly on a `gspread-pandas` `Spread` object's underlying `gspread` worksheet, or when migrating code from raw `gspread` to `gspread-pandas`. `gspread-pandas` provides its own high-level methods like `df_to_sheet()` for writing DataFrames.
fix
Instead of directly using `worksheet.update()`, leverage the `gspread-pandas` `Spread` object's `df_to_sheet()` method to write Pandas DataFrames to a worksheet. For example: `spread.df_to_sheet(my_dataframe, sheet='SheetName')`.
Upgrade
Version history
3.3.0latest on PyPI · released Feb 13, 2024
Audit
Dependencies
gspreadrequiredCore dependency for Google Sheets API interaction. Version 3.0.0 dropped Python 2.7 support and required gspread>=5.0.0.
pandasrequiredFundamental for DataFrame operations.
Agent activity
22 hits · last 30 days
node
16
OpenAI (training)
1
Resources
gspread-pandas — pip install gspread-pandas · libregistry