Skip to content

Write Null values to mssql table #8088

Closed
@klonuo

Description

This issue is covered at several places, but it doesn't seem there is a solution for mssql.
Referencing #4199

Example:

import numpy as np
import pandas as pd
from sqlalchemy import create_engine

pg_engine = create_engine('postgresql://postgres:*pass*@localhost:5432/test')
ms_engine = create_engine('mssql+pyodbc://localhost\\sqlexpress/test')

df = pd.DataFrame(np.random.rand(5,3))
df = df.where(df < .5, None)

df.to_sql('x', con=pg_engine)
df.to_sql('x', con=ms_engine)

This script created successfully postgres table but fails at mssql:

DataError: (DataError) (
    '22018', '[22018] [Microsoft][ODBC SQL Server Driver][SQL Server]
    Operand type clash: float is incompatible with text (206) (SQLExecDirectW); 
    [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]
    Statement(s) could not be prepared. (8180)'
) 
u'INSERT INTO x ([index], [0], [1], [2]) VALUES (?, ?, ?, ?)' (
    (0L, 0.30160831834724133, None, 0.3381303856312915), 
    (1L, 0.15675955874181347, None, None), 
    (2L, None, None, 0.4913946391341001), 
    (3L, 0.47313398459868405, 0.41395730103112083, 0.024404123661447397), 
    (4L, 0.02599871607241755, 0.0038944154716251678, None)
)

Full trace: https://gist.github.com/51dd8360a938dece9f28

pandas: 0.14.1
sqlalchemy: 0.9.7

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions