Skip to main content

Pandas_nql is an open source Python library that enables natural language queries on Pandas Dataframes using the latest advances in generative AI. Inspired by OpenAI's groundbreaking language models, pandas_nql allows users to analyze data in a more intuitive way - by simply asking questions in plain English instead of writing complex code.

Project description

Pandas Natural Language Query (NQL) Library

Pandas_nql is an open source Python library that enables natural language queries on Pandas Dataframes using the latest advances in generative AI. Inspired by OpenAI's groundbreaking language models, pandas_nql allows users to analyze data in a more intuitive way - by simply asking questions in plain English instead of writing complex code.

This library is perfect for data scientists, analysts, and developers looking to enhance their data analysis workflows. By leveraging the power of GPT and other language models behind the scenes, pandas_nql can understand complex data questions and automatically translate them into sql statements to extract insights from data.

Whether you're a Python expert looking to save time or someone new to data analysis, pandas_nql makes exploring datasets more accessible. It's as simple as pip installing the library and typing a query like "show me average monthly sales by region." You'll feel like you have a personal AI-powered data analyst at your fingertips!

Some key features:

  • Query Dataframes in plain English without writing code

  • Understands complex questions and data relationships

  • Automatically translates questions to SQL statements

  • Open source library for community involvement

Bring natural language queries to your data analysis today with the power of pandas_nql!

Disclaimers

  • So you aware, your data is never sent to the language model for query creation, however, the schema of the data is sent and used.

  • Typical AI warning - AI can make mistakes. Consider checking important information.

Installation


pip install pandas_nql

Prerequisites

  • OPENAI_API_KEY Environment variable must be set with a valid OpenAI Api Key

  • Python3.9+

Get started

How to select data from a Pandas dataframe using natural language:

import pandas as pd

from pands_nql import PandasNQL



# load Dataframe

data = {

    'Name': ['John', 'Jane', 'Bob', 'Jason', 'Mike'],

    'Age': [25, 30, 22, 47, 46],

    'City': ['New York', 'San Francisco', 'Seattle', 'Denver', 'Denver']

}



df = pd.DataFrame(data)



# Instantiate PandasNQL object passing in data to query

pandas_nql = PandasNQL(df)



# Call the query method to select data

results_df = pandas_nql.query("Find the number of people in each City.")



print(results_df)



# ...

Sql Generators

Generates the sql statement used to query the data. The statemens isgenerated using the schema of the data and the given natural language query.

The default sql statement generator is OpenAI. You can override the default with another generator, such as the Hugging Face T5 sql statement generator, or create your own.

While the Hugging Face T5 generator is not not as accurate as OpenAI, it is free.

Use the T5 sql statement generator

import pandas as pd

from pands_nql import PandasNQL, T5SqlGenerator



# load Dataframe

df = ...



# instantiate custom generator

t5_sql_generator = T5SqlGenerator(...)



# Instantiate PandasNQL with data

pandas_nql = PandasNQL(df, generator=t5_sql_generator)



# Call the query method to select data

results_df = pandas_nql.query("Find the number of people in each City.")



print(results_df)

Write your own sql statement generator

from generators import SqlStatementGeneratorBase



# define custom sql stement generator class

class CustomSqlGenerator(SqlGeneratorBase):

    

    def __init__(self, ...):

        super().__init__()        



    # override generate_sql method

    def generate_sql(self, query: str, schema: str, dataset_name: str = TEMP_VIEW_NAME) -> str:

        # generate sql statement

        # return sql statement

Update init.py

from pandas_nql.custom_sql_generator import CustomSqlGenerator

Use the custom sql statement generator

import pandas as pd

from pands_nql import PandasNQL, CustomSqlGenerator



# load Dataframe

df = ...



# instantiate custom generator

custom_generator = CustomSqlGenerator(...)



# Instantiate PandasNQL with data

pandas_nql = PandasNQL(df, generator=custom_generator)



# Call the query method to select data

results_df = pandas_nql.query("Find the number of people in each City.")



print(results_df)

Schema String Builders

Schema String Builders build strings representing the schema of a provided Pandas DataFrame. The default builder is the Sql Schema String Builder. This builder returns the schema string in the format: column_name sql_data_type (i.e.: City varchar(255)). You can override the default with another builder, such as the Pandas schema string builder, or create your own. The Pandas builder uses the format: coulmn_name: pandas_data_type (i.e.: City: object)

Write your own custom schema string builder.

from pandas_nql import SchemaStringBuilderBase



# define custom sql stement generator class

class CustomSchemaStringBuilderBase(SchemaStringBuilderBase):

    

    def __init__(self, ...):

        super().__init__()        



    # override generate_sql method

    def build_schema_string(self, dtypes: pd.Series) -> str:

        # build schema string

        # return schema string

Update init.py

from pandas_nql.custom_schema_string_builder import CustomStringBuilder

Use the custom schema string builder

import pandas as pd

from pands_nql import PandasNQL, CustomSqlGenerator, CustomSchemaStringBuilder



# load Dataframe

df = ...



# instantiate custom generator

custom_generator = CustomSqlGenerator(...)

custom_schema_builder = CustomSchemaStringBuilder(...)



# Instantiate PandasNQL with data

pandas_nql = PandasNQL(df, 

                        generator=custom_generator, 

                        schema_builder=custom_schema_builder)



# Call the query method to select data

results_df = pandas_nql.query("Find the number of people in each City.")



print(results_df)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandas_nql-1.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pandas_nql-1.1.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file pandas_nql-1.1.0.tar.gz.

File metadata

  • Download URL: pandas_nql-1.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pandas_nql-1.1.0.tar.gz
Algorithm Hash digest
SHA256 9c9dd08c8a879646c038b846810e75ab9cf73bd943e416c1f0131513c2a92c28
MD5 fa4f9633f65049e5218216002a932dff
BLAKE2b-256 f1d4fcd74c7578e8eb5296339481448ee826dd3c1f30669d6e18cfc8bea4e6a4

See more details on using hashes here.

File details

Details for the file pandas_nql-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pandas_nql-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pandas_nql-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05fce82a302925a80125be44952c96c4cb8b51a3ddf9dbd0b51f3a3285cb21e4
MD5 4f95d78254cd2fd91044cf84500c840f
BLAKE2b-256 e9f4c57ddf5b33e0cab8f0a903e6e6230b79fb3ebfa045f71ddcf59988b7cd1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page