Registry / data / pandasql

pandasql

JSON →
library0.7.3pypypi✓ verified 30d ago

Pandasql is a Python library that allows users to query pandas DataFrames using SQL syntax. It functions similarly to `sqldf` in R, leveraging SQLite under the hood to provide a familiar interface for data manipulation and analysis for those comfortable with SQL. It is currently at version 0.7.3 and receives limited updates, with alternatives like DuckDB or Polars SQL often recommended for more active development or performance needs.

pip install pandasql
INSTALL
IMPORT
SIG · PANDASQL
P
pandasql
datapythonv0.7.3
Install
10.2s avg
Import
1468ms
Disk
191MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.7.3 · 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.10–3.95 runs
installs and imports cleanly · install 0.0s · import 1.528s · 191.4MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 10.2s · import 1.408s · 182MB
191MB installed
● package 191MB
Code
Verified usage

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

sqldf
✓ from pandasql import sqldf
This is the primary function used to execute SQL queries on DataFrames.

This quickstart demonstrates how to import `sqldf`, create a pandas DataFrame, define a SQL query as a string referencing the DataFrame by its variable name, and then execute the query to get a new DataFrame as output.

import pandas as pd from pandasql import sqldf # Create a sample pandas DataFrame df = pd.DataFrame({ 'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 22], 'city': ['New York', 'Los Angeles', 'Chicago'] }) # Define an SQL query as a string query = """ SELECT name, age FROM df WHERE age > 23 ORDER BY age DESC """ # Execute the SQL query using sqldf result_df = sqldf(query) print(result_df)
Debug
Known issues
gotchaPandasql uses SQLite as its backend, which has inherent limitations, such as a default limit of around 999 SQL variables/parameters. This can lead to `OperationalError: too many SQL variables` when querying very wide DataFrames (many columns) or complex queries with numerous parameters.
fix
Reduce the number of columns in your query, split complex queries into smaller parts, or consider processing data in chunks. For very large datasets or complex analytical needs, consider using dedicated database engines or more performant alternatives like DuckDB or Polars SQL.
affects: All versions
gotchaFor `sqldf` to correctly identify DataFrames, they must be available in the global or local scope where `sqldf` is called. If `sqldf` is used within a function and the DataFrame is not passed into the function's `locals()` or `globals()`, you might encounter `no such table` errors.
fix
Ensure DataFrames are defined in the same scope where `sqldf` is called, or explicitly pass `globals()` or `locals()` as the second argument to `sqldf`, e.g., `sqldf(query, globals())` or `sqldf(query, locals())`.
affects: All versions
gotchaPandasql only supports Data Query Language (DQL) operations (e.g., SELECT statements). It does not support Data Manipulation Language (DML) like INSERT, UPDATE, DELETE, or Data Definition Language (DDL) like CREATE, ALTER, DROP for modifying DataFrames or their structure.
fix
Perform data modification (updates, inserts, deletes) or schema changes directly using pandas DataFrame methods.
affects: All versions
deprecatedThe `pandasql` library receives limited updates and is less actively maintained compared to other SQL-on-DataFrame solutions. For new projects or performance-critical applications, modern alternatives like `duckdb` (via `duckdb.query_df`), Polars SQL, or even native `pandas.DataFrame.query` are often recommended for better performance and ongoing development.
fix
Evaluate and consider migrating to more actively maintained libraries such as DuckDB or Polars if performance, new features, or long-term support are critical.
affects: All versions (future-looking)
Errors
Common errors & fixes
NameError: global name 'sqldf' is not defined
The `sqldf` function is called without being properly imported or without `globals()`/`locals()` explicitly passed as the environment when used within a specific scope, such as a function.
fix
Ensure `sqldf` is imported using `from pandasql import sqldf`. When calling `sqldf`, always pass `globals()` or `locals()` as the second argument to make DataFrames available in the SQL query's scope (e.g., `sqldf(query, globals())`).
OperationalError: no such table: <dataframe_name>
The DataFrame variable name used in the SQL query string does not exactly match the Python DataFrame variable name, or the DataFrame is not in the scope provided to `sqldf`. This also occurs when attempting DDL/DML operations (like `CREATE`, `INSERT`, `UPDATE`, `ALTER TABLE`) as `pandasql` primarily supports DQL (SELECT statements) on in-memory DataFrames.
fix
Verify that the DataFrame variable name in your Python code precisely matches the 'table' name in your SQL query (it is case-sensitive). For data modification (e.g., `UPDATE`, `INSERT`), use pandas DataFrame methods directly instead of SQL through `pandasql`.
SyntaxError: invalid syntax (pointing to `return {**outer_frame.frame.f_globals, ...}`)
This specific syntax error indicates that a version of `pandasql` (which uses Python 3 dictionary unpacking syntax `**`) is being run in a Python 2 environment where this syntax is not supported.
fix
Upgrade your Python environment to Python 3.x, as `pandasql` versions using this syntax are not compatible with Python 2.x.
OperationalError: too many SQL variables
This error occurs with large queries or very wide DataFrames because SQLite, the underlying database for `pandasql`, has a default limit (typically around 999) on the number of parameters or variables that can be used in a single SQL query.
fix
Reduce the number of columns selected in your query, split complex queries into smaller, more manageable parts, or process data in chunks. For very large datasets, consider using alternatives like DuckDB or Polars SQL, which offer better performance and handle larger query complexities.
Upgrade
Version history
0.7.3latest on PyPI · released Apr 20, 2016
Audit
Dependencies
pandasrequiredCore functionality relies on pandas DataFrames.
Agent activity
9 hits · last 30 days
node
8
Resources
pandasql — pip install pandasql · libregistry