Registry / database / bcpandas

bcpandas

JSON →
library2.7.2pypypi✓ verified 85d ago

bcpandas (version 2.7.2) is a Python library providing a high-level wrapper around the Microsoft SQL Server BCP utility. It enables high-performance data transfers between pandas DataFrames and SQL Server databases without requiring direct knowledge of BCP commands. The library is actively maintained and typically releases updates to support newer Python and pandas versions.

pip install bcpandas
INSTALL
IMPORT
SIG · BCPANDAS
B
bcpandas
databasepythonv2.7.2
Install
9.5s avg
Import
Disk
195MB
Pass rate
9/ 10
Env Coverage9 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.7.2 · 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
glibc
py 3.10
✓ —
✓ 9.45s
py 3.11
✓ —
✓ 8.8s
py 3.12
✕ build_error
✓ 9.38s
py 3.13
✓ —
✓ 8.98s
py 3.9
✓ —
✓ 11.1s
195MB installed
● package 195MB
Code
Verified usage

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

SqlCreds
from bcpandas import SqlCreds
Used to create a credential object for SQL Server connection.
to_sql
from bcpandas import to_sql
The primary function for high-performance DataFrame writes to SQL Server.
read_sql
from bcpandas import read_sql
from bcpandas import read_sql
The `read_sql` function was deprecated in v5.0 and removed in v6.0+. Use `pandas.read_sql_table` or `pandas.read_sql_query` instead for reading data.

This quickstart demonstrates writing a pandas DataFrame to a SQL Server database using `bcpandas.to_sql`. It initializes a `SqlCreds` object with connection details, creates a sample DataFrame, and then uses `to_sql` to perform a bulk insert. Environment variables are used for sensitive credentials for security and flexibility. Note that `bcpandas.read_sql` has been removed, so `pandas.read_sql` functions should be used for reading data.

import pandas as pd import numpy as np import os from bcpandas import SqlCreds, to_sql # Ensure these environment variables are set for your SQL Server connection server = os.environ.get('SQL_SERVER_NAME', 'your_sql_server_name') database = os.environ.get('SQL_DB_NAME', 'your_database_name') username = os.environ.get('SQL_USERNAME', 'your_username') password = os.environ.get('SQL_PASSWORD', 'your_password') creds = SqlCreds( server, database, username, password ) df = pd.DataFrame( data=np.random.randint(0, 100, size=(10, 3)), columns=['col_A', 'col_B', 'col_C'] ) table_name = 'my_test_table_bcpandas' # Write DataFrame to SQL Server (most common use case) to_sql(df, table_name, creds, index=False, if_exists='replace') print(f"DataFrame successfully written to table '{table_name}' on {server}/{database}")
Debug
Known issues
breakingThe `bcpandas.read_sql` function was deprecated in v5.0 and completely removed in v6.0+. Attempts to use it will result in an `AttributeError` or `ImportError` depending on the version.
fix
Migrate to `pandas.read_sql_table` or `pandas.read_sql_query` for reading data from SQL Server.
affects: v6.0+
gotchaIf a DataFrame contains data with all possible delimiter (e.g., `,`, `|`, `\t`) or quote characters (e.g., `'`, `"`, `~`, `` ` ``), `bcpandas` may fail to find unique characters for its internal CSV file creation, leading to an error.
fix
Inspect your data for problematic characters. Replace or remove one of the conflicting delimiter/quote characters in your DataFrame columns before calling `to_sql`.
affects: All versions
gotchaA `NaN` or `Null` value in the *last column* of a DataFrame can cause an error during the BCP write operation due to a known BCP utility issue.
fix
Ensure the last column of your DataFrame does not contain `NaN` or `Null` values, or handle them appropriately (e.g., fill with a default value) before writing.
affects: All versions
gotchaWhen using `if_exists='append'` with `to_sql`, the DataFrame columns must exactly match the SQL table columns by both name and order, otherwise the operation will fail.
fix
Ensure your DataFrame's column names and their order precisely match the target SQL table's columns when appending.
affects: All versions
gotchaSpaces in DataFrame column names can lead to an 'Incorrect host-column number found in BCP format-file' error during write operations.
fix
Rename DataFrame columns to remove spaces (e.g., replace with underscores) before using `to_sql`: `df.columns = df.columns.str.replace(' ', '_')`.
affects: All versions
gotchaEmpty strings (`''`) in a pandas DataFrame will be inserted as `NULL` values into the SQL Server database when using `bcpandas`.
fix
If preserving empty strings is critical, consider pre-processing your DataFrame to replace empty strings with a specific non-NULL placeholder value or use a different writing method.
affects: All versions
Errors
Common errors & fixes
bcpandas.constants.BCPandasValueError: Data contains all of the possible quote characters ('"', "'", '`', '~'), cannot use BCP to import it.
The DataFrame contains values that include all characters bcpandas attempts to use as quote characters for its internal CSV creation, preventing it from finding an unused one.
fix
Modify your DataFrame data to remove at least one of the conflicting quote characters from all relevant columns, or replace it with an alternative character.
bcpandas.constants.BCPandasValueError: Data contains all of the possible delimiter characters (',', '|', '\t'), cannot use BCP to import it.
Similar to the quote character issue, this occurs when all potential delimiter characters are present in your DataFrame's data, preventing bcpandas from selecting a unique delimiter for its intermediate CSV file.
fix
Pre-process your DataFrame to remove or replace one of the problematic delimiter characters from your data. For example, if '|' is a delimiter and present in data, replace it with another character: `df['col'] = df['col'].str.replace('\|', '/')`.
Error = [Microsoft][ODBC Driver 17 for SQL Server]Incorrect host-column number found in BCP format-file.
This error often indicates an issue with column mapping between the DataFrame and the SQL table, commonly caused by spaces in DataFrame column names or a mismatch in column count/order.
fix
Ensure DataFrame column names do not contain spaces (e.g., `df.columns = df.columns.str.replace(' ', '_')`). Also, verify that the DataFrame's column count and order match the target SQL table's schema, especially when using `if_exists='append'`.
AttributeError: 'Engine' object has no attribute 'cursor' in to_sql() -> _sql_item_exists()
This issue has been reported with newer versions of pandas (e.g., 2.2.3) and potentially SQLAlchemy, where internal API changes affect how bcpandas interacts with the SQLAlchemy engine object.
fix
Check for updates to `bcpandas` that address compatibility with newer `pandas` and `SQLAlchemy` versions. If no update is available, consider pinning `pandas` to a known compatible older version (e.g., `<2.2.3`) or `sqlalchemy` to a version lower than that where the attribute change occurred. Refer to the GitHub issues for the latest solutions.
Upgrade
Version history
2.7.2latest on PyPI · released Dec 16, 2024
Audit
Dependencies
pythonrequiredRequired Python version range.
pandasrequiredCore DataFrame manipulation.
sqlalchemyrequiredUsed for database engine creation within SqlCreds.
pyodbcrequiredSupported DBAPI for SQL Server connection.
Microsoft BCP UtilityrequiredExternal command-line tool that bcpandas wraps for bulk data transfer. Must be installed on the system where bcpandas runs.
Microsoft ODBC Driver for SQL ServerrequiredRequired by pyodbc for connecting to SQL Server. Must be installed on the system.
SqlCmd UtilityrequiredRequired for certain database operations.
Agent activity
24 hits · last 30 days
node
22
OpenAI (training)
1
Resources