Effortlessly connect to SQL Server to import data into Polars DataFrames and export data back to SQL Server.
Project description
polars_mssql
polars_mssql is a Python package designed to simplify working with Microsoft SQL Server databases using the high-performance polars DataFrame library. It provides an intuitive and efficient interface for running SQL queries, reading tables, and writing data to SQL Server.
Features
- Seamless SQL Server Integration: Easily connect to SQL Server with options for Windows Authentication or SQL Authentication.
- Query Execution: Execute SQL queries and retrieve results as
polars.DataFrameobjects. - Table Operations: Read and write tables with flexibility and performance.
- Context Management: Supports Python's context manager for automatic connection handling.
- Customizable Options: Configure schema inference, batch sizes, and execution options.
Installation
Install the package using pip:
pip install polars_mssql
Ensure the following dependencies are installed:
polarsfor high-performance DataFrame operations.sqlalchemyfor database connectivity.- An appropriate ODBC driver for SQL Server (e.g., ODBC Driver 17 or 18).
Usage
Here is an example of how to use polars_mssql to connect to SQL Server and perform various operations:
1. Connecting to SQL Server
from polars_mssql import Connection
# Initialize a connection
conn = Connection(
database="my_database",
server="my_server",
username="my_user", # Optional for Windows Authentication
password="my_password", # Optional for Windows Authentication
)
2. Running Queries
Execute a SQL Query and Get Results as a DataFrame
query = "SELECT * FROM my_table WHERE col1 = 'a'"
df = conn.read_query(query)
Read an Entire Table
df = conn.read_table("my_table")
3. Writing Data to SQL Server
Write a Polars DataFrame to a Table
import polars as pl
# Example DataFrame
data = pl.DataFrame({"col1": [1, 2, 3], "col2": ["a", "b", "c"]})
conn.write_table(data, name="my_table", if_exists="replace")
4. Using Context Management
with Connection(database="my_database", server="my_server") as conn:
df = conn.read_query("SELECT * FROM my_table")
print(df)
5. Closing the Connection
conn.close()
API Reference
Connection Class
Constructor
Connection(database: Optional[str] = None, server: Optional[str] = None, driver: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None)
Methods
read_query(query: str, ...) -> pl.DataFrame: Execute a query and return results as a Polars DataFrame.read_table(name: str) -> pl.DataFrame: Read all rows from a table.write_table(df: pl.DataFrame, name: str, ...): Write a Polars DataFrame to a table.close(): Close the database connection.- Context Management (
__enter__and__exit__): Automatically manage connections.
Requirements
- Python 3.8 or higher
- ODBC Driver for SQL Server (17 or 18 recommended)
Installing the ODBC Driver
Windows
Download and install the ODBC Driver from Microsoft's website.
macOS
Install via Homebrew:
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install --no-sandbox msodbcsql18
Linux
Install using the following commands:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt-get update
sudo apt-get install -y mssql-tools unixodbc-dev
Contributing
Contributions are welcome! If you encounter issues or have feature requests, please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
This package integrates the efficiency of polars with the versatility of SQL Server, inspired by real-world data engineering needs. As a data engineer, I often need to pull data from SQL Server into polars and export data from polars back to SQL Server. I created this package to streamline these workflows and make the process more efficient.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_mssql-0.1.0.tar.gz.
File metadata
- Download URL: polars_mssql-0.1.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfcc61255a34fd997c244a388575f0b2f5ef758ef42816ad39e93374b85dfdbc
|
|
| MD5 |
dbf486ae2175a17456871a98a53c00b1
|
|
| BLAKE2b-256 |
ea197bf4421ef5084bb697bb9a2ef929ac45ef26c79940d5ef66a198f342902e
|
File details
Details for the file polars_mssql-0.1.0-py3-none-any.whl.
File metadata
- Download URL: polars_mssql-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a6b6d840ba521ac0e1df7059d2a5f322baf1453bb637f91c15379ca0b372cdb
|
|
| MD5 |
e3ad04ec70479047291305c8ab0aa33c
|
|
| BLAKE2b-256 |
2faeba3a177fec8c54ea11c323061624dcc30cdc8287275c22d362182a432277
|