Skip to content

Bug when using SQLAlchemy and masters' to_sql for integer-named columns in df #6925

Closed
@JohnSmizz

Description

@JohnSmizz

Hi-
when trying to write a dataframe with non-string named columns via current to_sql the function fails because function _safe_col_name attempts to call the strip method.
NB I do not have master installed I just copied the SQL module and call the v14 functions from it.

df = pd.DataFrame(randn(2,4))
con = sqlalchemy.create_engine("mssql+pyodbc://server/db?driver=SQL Server Native Client 11.0")
import sql
sql.to_sql(df, "TBL_TEMP", con, if_exists="replace")

yields the following error:

Traceback (most recent call last):

  File "<ipython-input-40-36a592728500>", line 4, in <module>
    sql.to_sql(df, "TBL_TEMP", con, if_exists="replace")

  File "N:\Python\sql.py", line 399, in to_sql
    index_label=index_label)

  File "N:\Python\sql.py", line 773, in to_sql
    index_label=index_label)

  File "N:\Python\sql.py", line 479, in __init__
    self.table = self._create_table_statement()

  File "N:\Python\sql.py", line 601, in _create_table_statement
    for name, typ in zip(safe_columns, column_types)]

  File "N:\Python\sql.py", line 600, in <listcomp>
    columns = [Column(name, typ)

  File "N:\Python\sql.py", line 50, in _safe_col_name
    return col_name.strip().replace(' ', '_')

AttributeError: 'numpy.int64' object has no attribute 'strip'

However if i replace the first line with

df = pd.DataFrame(randn(2,4), columns=list("abcd"))

there is no error.

showversions info below

INSTALLED VERSIONS

commit: None
python: 3.3.3.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 44 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_GB

pandas: 0.13.1
Cython: None
numpy: 1.8.0
scipy: 0.13.2
statsmodels: 0.5.0
IPython: 1.1.0
sphinx: 1.2
patsy: 0.2.1
scikits.timeseries: None
dateutil: 2.2
pytz: 2013.8
bottleneck: None
tables: 3.0.0
numexpr: 2.2.2
matplotlib: 1.3.1
openpyxl: 1.6.1
xlrd: 0.9.2
xlwt: None
xlsxwriter: 0.5.2
sqlalchemy: 0.9.0
lxml: None
bs4: None
html5lib: None
bq: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions